统计图

iMobile支持在地图上的动态层中创建图表,支持饼状图、柱状图、折线图等。

创建图表需要的库包括:com.supermap.data.jar、com.supermap.mapping.jar、chartengine.jar、libimb2d.so。

主要使用的类和接口如下表:

方法
Recordset seekID()、getFieldValue()、getDouble()、getString()
DynamicView addElement()、refresh()
DynPieChart setChartSize()、addChartData()、setChartTile()、setAxesColor()、setShowLegend()、setShowLabels()、addPoint()、setStyle()
DynBarChart setChartSize()、addChartData()、setChartTile()、setAxesColor()、setShowLegend()、setShowLabels()、addPoint()、setStyle()
DynLineChart setChartSize()、addChartData()、setChartTile()、setAxesColor()、setShowLegend()、setShowLabels()、addPoint()、setStyle()
DynamicStyle setBackColor()、setAlpha()

创建图表的一般流程包括:

  1. 获取并添加动态层到地图;
  2. 添加数据到图表;
  3. 刷新图表。

饼状图

/**---------获取并添加动态层到地图---------**/
mDynamicLayer  = new DynamicView(this, mapView.getMapControl().getMap());
mapView.addDynamicView(m_DynamicLayer);
/**---------添加数据到图表---------**/
while (!mRecordset.isEOF()) {
  double pop_0_14 = mRecordset.getDouble("POP_0_14");
  double pop_15_64 = mRecordset.getDouble("POP_15_64");
  double pop_65 = mRecordset.getDouble("POP_65PLUS");
  int color1 = Color.rgb(155, 187, 89);
  int color2 = Color.rgb(79, 129, 189);
  int color3 = Color.rgb(192, 80, 77);
  String[] tiles = {"<15", "15-65", ">65"};
  double[] pieValues = {pop_0_14, pop_15_64/3, pop_65};
  int[] pieColors = {color2, color1, color3};

  DynPieChart pieChart = new DynPieChart();
  pieChart.setChartSize(width, height);//设置图表大小
  pieChart.addChartData(tiles, pieValues, pieColors);//添加图表数据
  pieChart.setChartTile("2012年全省年龄结构", Color.argb(0, 0, 0, 0), 20);//设置图表标题、颜色和字体大小
  pieChart.setShowLegend(false);
  pieChart.setShowLabels(false);
  GeoRegion region= (GeoRegion)mRecordset.getGeometry();
  pieChart.addPoint(region.getInnerPoint());
  DynamicStyle style = new DynamicStyle();
  style.setAlpha(0);
  pieChart.setStyle(style);
  mDynamicLayer.addElement(pieChart);//添加饼状图到动态层
  mRecordset.moveNext();
}
/**---------刷新图表---------**/
mDynamicLayer.refresh();

折线图

/**---------获取并添加动态层到地图---------**/
mDynamicLayer  = new DynamicView(this, mapView.getMapControl().getMap());
mapView.addDynamicView(m_DynamicLayer);
/**---------添加数据到图表---------**/
while (!mRecordset.isEOF()) {
  double n = format(mRecordset.getDouble("N_FERTILIZER"));
  double p = format(mRecordset.getDouble("P_FERTILIZER"));
  double k = format(mRecordset.getDouble("K_FERTILIZER"));
  double compound = format(mRecordset.getDouble("COMPOUNDFERTILIZER"));
  int color = Color.rgb(175, 97, 31);
  String[] tiles = {"氮肥", "磷肥", "钾肥","复合肥"};
  DynLineChart lineChart = new DynLineChart();
  lineChart.setAxesColor(Color.BLACK);
  lineChart.setChartSize(width, height);
  lineChart.setShowGrid(false, false);
  lineChart.setShowLabels(false);
  lineChart.setShowLegend(false);
  lineChart.addChartData("肥料", new double[]{n,p,k,compound}, color);
  lineChart.setYTitle("化肥使用量");
  for(int i=0;i<4;i++){
    lineChart.setXAxisLabel(i+1, tiles[i]);
  }
  lineChart.setBarSpacing(0.5f);
  GeoRegion region= (GeoRegion)mRecordset.getGeometry();
  lineChart.addPoint(region.getInnerPoint());
  DynamicStyle style = new DynamicStyle();
  style.setAlpha(0);
  lineChart.setStyle(style);    //设置风格
  mDynamicLayer.addElement(lineChart);//添加柱状图到动态层
  mRecordset.moveNext();
}
/**---------刷新图表---------**/
mDynamicLayer.refresh();

柱状图

/**---------获取并添加动态层到地图---------**/
mDynamicLayer  = new DynamicView(this, mapView.getMapControl().getMap());
mapView.addDynamicView(m_DynamicLayer);
/**---------添加数据到图表---------**/
while (!mRecordset.isEOF()) {
  double n = format(mRecordset.getDouble("N_FERTILIZER"));
  double p = format(mRecordset.getDouble("P_FERTILIZER"));
  double k = format(mRecordset.getDouble("K_FERTILIZER"));
  double compound = format(mRecordset.getDouble("COMPOUNDFERTILIZER"));
  int color = Color.rgb(175, 97, 31);
  String[] tiles = {"氮肥", "磷肥", "钾肥","复合肥"};
  DynBarChart barChart = new DynBarChart();
  barChart.setAxesColor(Color.BLACK);
  barChart.setChartSize(width, height);
  barChart.setShowGrid(false, false);
  barChart.setShowLabels(false);
  barChart.setShowLegend(false);
  barChart.addChartData("肥料", new double[]{n,p,k,compound}, color);
  barChart.setYTitle("化肥使用量");
  for(int i=0;i<4;i++){
    barChart.setXAxisLabel(i+1, tiles[i]);
  }
  barChart.setBarSpacing(0.5f);
  GeoRegion region= (GeoRegion)mRecordset.getGeometry();
  barChart.addPoint(region.getInnerPoint());
  DynamicStyle style = new DynamicStyle();
  style.setAlpha(0);
  barChart.setStyle(style);    //设置风格
  mDynamicLayer.addElement(barChart);//添加柱状图到动态层
  mRecordset.moveNext();
}
/**---------刷新图表---------**/
mDynamicLayer.refresh();

以上统计图创建流程可参照示例代码themedemo