ArcGIS runtime sdk for android 结合mpchartlib进行离线数据统计分析

现在越来越多的GIS项目中需要通过移动端设备进行统计分析,通过ArcGIS runtime sdk for android 结合mpchartlib进行统计分析能得到比较理想的效果,如下图所示:
 
 

那么这个效果是如何实现的呢?首先到mpchartlib的官网下载最新jar包,把它引用到你的android工程当中,然后先开始做基于行政区划面状离线数据的分析,根据行政区划数据的属性信息中的某个字段信息对行政区划数据通过ArcGIS runtime sdk进行渲染,得到不同区域根据不同的数据等级显示不同的颜色并且在不同颜色的区域内加上柱状图符号来增加美观度,具体如下代码所示:


,主要代码如下所示:


String sdPath = "/storage/sdcard1/esridata/al/default.geodatabase";
Geodatabase geo = null;
try {
geo = new Geodatabase(sdPath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GeodatabaseFeatureTable tempTable = null;


for (GeodatabaseFeatureTable gdbFeatureTable : geo
.getGeodatabaseTables()) {
tempTable = gdbFeatureTable;
}
if (tempTable != null && tempTable.hasGeometry()) {
layer = new FeatureLayer(tempTable);
layer.setOpacity(1);
layer.setVisible(true);
layer.setName("offlinepolygon");


QueryParameters query = new QueryParameters();
// query.setWhere("1=1");
query.setOutFields(new String[] { "*" });
query.setReturnGeometry(true);


tempTable.queryFeatures(query,
new CallbackListener<FeatureResult>() {


@Override
public void onError(Throwable e) {
// TODO Auto-generated method stub
}


@Override
public void onCallback(FeatureResult objs) {


GeometryEngine ge = new GeometryEngine();


Feature[] features = GDBUtils.getFeatureList(objs);


for (int i = 0; i < features.length; i++) {


Feature feature = features[i];// (Feature) objs;
Geometry pGeometry = feature.getGeometry();
if (pGeometry.isEmpty()) {
continue;
}
Polygon pPolygon = (Polygon) pGeometry;
int nPointCount = pPolygon.getPointCount();
SpatialReference pSpatialReference = mMapView
.getSpatialReference();
int nID = pSpatialReference.getID();
Point pt = GeometryEngine
.getLabelPointForPolygon(pPolygon,
pSpatialReference);
Drawable image = context.getResources()
.getDrawable(R.drawable.col);


BitmapDrawable bd = (BitmapDrawable) image;
Bitmap bm = bd.getBitmap();
Bitmap okbm = CommTools
.imageScale(bm, 150, 150);
BitmapDrawable okimage = new BitmapDrawable(
okbm);
PictureMarkerSymbol markerSymbol = new PictureMarkerSymbol(
okimage);


fillGraphic = new Graphic(pt, markerSymbol);
graphicsLayer.addGraphic(fillGraphic);
}


renderer = new ClassBreaksRenderer();
renderer.setField("renkou");// 渲染依附的字段
cb1 = new ClassBreak();// 定义第一段渲染范围和样式
cb1.setClassMaxValue(3030298);
cb1.setSymbol(new SimpleFillSymbol(Color.argb(128,
56, 168, 0)));
cb1.setLabel("First class");


cb2 = new ClassBreak();// 定义第二段渲染范围和样式
cb2.setClassMaxValue(5654654);
cb2.setSymbol(new SimpleFillSymbol(Color.argb(128,
139, 209, 0)));
cb2.setLabel("Second class");


cb3 = new ClassBreak();// 定义第三段渲染范围和样式
cb3.setClassMaxValue(Double.MAX_VALUE);
cb3.setSymbol(new SimpleFillSymbol(Color.argb(128,
255, 0, 0)));


renderer.addClassBreak(cb1);
renderer.addClassBreak(cb2);
renderer.addClassBreak(cb3);


layer.setRenderer(renderer);


mMapView.addLayer(layer);
mMapView.addLayer(graphicsLayer);
}}




之后需要调用mpchartlib在点击不同行政区域后弹出饼图、柱状图等统计报表,如下代码所示:

private PieData getPieData(int count, float range) {


ArrayList<String> xValues = new ArrayList<String>(); // xVals用来表示每个饼块上的内容


// for (int i = 0; i < count; i++) {
xValues.add("锡林浩特市"); // 饼块上显示成Quarterly1, Quarterly2, Quarterly3,
// Quarterly4
xValues.add("二连浩特市");
xValues.add("多伦县");
xValues.add("正蓝旗");
xValues.add("镶黄旗");
xValues.add("正镶白旗");
xValues.add("阿巴嘎旗");
xValues.add("太仆寺旗");
xValues.add("苏尼特左旗");
xValues.add("苏尼特右旗");
xValues.add("东乌珠穆沁旗");
xValues.add("西乌珠穆沁旗");
xValues.add("乌拉盖管理区");
// }


ArrayList<Entry> yValues = new ArrayList<Entry>(); // yVals用来表示封装每个饼块的实际数据


// 饼图数据
/**
* 将一个饼形图分成四部分, 四部分的数值比例为14:14:34:38 所以 14代表的百分比就是14%
*/


float quarterly1 = 1;
float quarterly2 = 1;
float quarterly3 = 4;
float quarterly4 = 4;
float quarterly5 = 10;
float quarterly6 = 10;
float quarterly7 = 10;
float quarterly8 = 10;
float quarterly9 = 10;
float quarterly10 = 10;
float quarterly11 = 10;
float quarterly12 = 10;
float quarterly13 = 10;


yValues.add(new Entry(quarterly1, 0));
yValues.add(new Entry(quarterly2, 1));
yValues.add(new Entry(quarterly3, 2));
yValues.add(new Entry(quarterly4, 3));
yValues.add(new Entry(quarterly5, 4));
yValues.add(new Entry(quarterly6, 5));
yValues.add(new Entry(quarterly7, 6));
yValues.add(new Entry(quarterly8, 7));
yValues.add(new Entry(quarterly9, 8));
yValues.add(new Entry(quarterly10, 9));
yValues.add(new Entry(quarterly11, 10));
yValues.add(new Entry(quarterly12, 11));
yValues.add(new Entry(quarterly13, 12));


// y轴的集合
PieDataSet pieDataSet = new PieDataSet(yValues, "图例"/* 显示在比例图上 */);
pieDataSet.setSliceSpace(0f); // 设置个饼状图之间的距离


ArrayList<Integer> colors = new ArrayList<Integer>();


// 饼图颜色
colors.add(Color.rgb(205, 205, 205));
colors.add(Color.rgb(114, 188, 223));
colors.add(Color.rgb(255, 123, 124));
colors.add(Color.rgb(157, 135, 200));
colors.add(Color.rgb(57, 195, 200));
colors.add(Color.rgb(57, 135, 100));
colors.add(Color.rgb(77, 135, 250));
colors.add(Color.rgb(27, 105, 100));
colors.add(Color.rgb(87, 195, 223));
colors.add(Color.rgb(47, 235, 50));
colors.add(Color.rgb(57, 135, 200));
colors.add(Color.rgb(15, 25, 20));
colors.add(Color.rgb(57, 235, 10));


pieDataSet.setColors(colors);


DisplayMetrics metrics = context.getResources().getDisplayMetrics();
float px = 5 * (metrics.densityDpi / 160f);
pieDataSet.setSelectionShift(px); // 选中态多出的长度


PieData pieData = new PieData(xValues, pieDataSet);


return pieData;
}


private void showChart(PieChart pieChart, PieData pieData) {
pieChart.setHoleColorTransparent(true);


pieChart.setHoleRadius(60f); // 半径
pieChart.setTransparentCircleRadius(64f); // 半透明圈
// pieChart.setHoleRadius(0) //实心圆


pieChart.setDescription("各旗县人口数据");


// mChart.setDrawYValues(true);
pieChart.setDrawCenterText(true); // 饼状图中间可以添加文字


pieChart.setDrawHoleEnabled(true);


pieChart.setRotationAngle(90); // 初始旋转角度


// draws the corresponding description value into the slice
// mChart.setDrawXValues(true);


// enable rotation of the chart by touch
pieChart.setRotationEnabled(true); // 可以手动旋转


// display percentage values
pieChart.setUsePercentValues(true); // 显示成百分比
// mChart.setUnit(" €");
// mChart.setDrawUnitsInChart(true);


// add a selection listener
// mChart.setOnChartValueSelectedListener(this);
// mChart.setTouchEnabled(false);


// mChart.setOnAnimationListener(this);


pieChart.setCenterText("各个旗县人口数据"); // 饼状图中间的文字


// 设置数据
pieChart.setData(pieData);


// undo all highlights
// pieChart.highlightValues(null);
// pieChart.invalidate();


Legend mLegend = pieChart.getLegend(); // 设置比例图
mLegend.setPosition(LegendPosition.BELOW_CHART_CENTER); // 最右边显示
// mLegend.setForm(LegendForm.LINE); //设置比例图的形状,默认是方形
mLegend.setXEntrySpace(7f);
mLegend.setYEntrySpace(5f);


pieChart.animateXY(1000, 1000); // 设置动画
// mChart.spin(2000, 0, 360);
}


mMapView.setOnSingleTapListener(new OnSingleTapListener() {


@Override
public void onSingleTap(float x, float y) {
// TODO Auto-generated method stub
if (!mMapView.isLoaded()) {
return;
}
GeodatabaseFeatureTable tempTable2 = null;


String sdPath2 = "/storage/sdcard1/esridata/al/default.geodatabase";
Geodatabase geo2 = null;
try {
geo2 = new Geodatabase(sdPath2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


for (GeodatabaseFeatureTable gdbFeatureTable : geo2
.getGeodatabaseTables()) {
// if(gdbFeatureTable.getTableName().equalsIgnoreCase("nmarea")){
tempTable2 = gdbFeatureTable;
// break;
// }
}


Point identifyPoint = mMapView.toMapPoint(
x, y);
QueryParameters query = new QueryParameters();
query.setGeometry(identifyPoint);
query.setOutFields(new String[] { "*" });
query.setReturnGeometry(true);
tempTable2
.queryFeatures(
query,
new CallbackListener<FeatureResult>() {


@Override
public void onCallback(
FeatureResult arg0) {
// TODO
// Auto-generated
// method stub
Feature[] features = GDBUtils
.getFeatureList(arg0);


Map<String, Object> strtotal = null;
for (int i = 0; i < features.length; i++) {
Feature feature = features[i];
strtotal = feature
.getAttributes();


}
String ptatal = strtotal
.get("renkou")
.toString();
String pname = strtotal
.get("name")
.toString();


ArrayList al = new ArrayList();
al.add("renkou");
al.add(ptatal);
al.add(pname);
Message mes = new Message();
mes.obj = al;
handler.sendMessage(mes);


}


@Override
public void onError(
Throwable arg0) {
// TODO
// Auto-generated
// method stub


}
});


handler = new Handler(
new Handler.Callback() {


@Override
public boolean handleMessage(
Message msg) {
// TODO Auto-generated
// method stub


ArrayList pTemp = (ArrayList) msg.obj;
LayoutInflater inflater = LayoutInflater
.from(context);
View layout = inflater
.inflate(
R.layout.analysis,
null);
TextView pTxtrenkoutotal = (TextView) layout
.findViewById(R.id.txtrenkoutotal);
pTxtrenkoutotal
.setText(pTemp.get(
2)
.toString()
+ "人口总量为:"
+ pTemp.get(
1)
.toString());


View layout2 = inflater
.inflate(
R.layout.analysisibarchart,
null);
TextView pTxtrenkoutotal2 = (TextView) layout2
.findViewById(R.id.txtrenkoutotal);
pTxtrenkoutotal2
.setText(pTemp.get(
2)
.toString()
+ "人口总量为:"
+ pTemp.get(
1)
.toString());


View layout3 = inflater
.inflate(
R.layout.analysislinechart,
null);
TextView pTxtrenkoutotal3 = (TextView) layout3
.findViewById(R.id.txtrenkoutotal);
pTxtrenkoutotal3
.setText(pTemp.get(
2)
.toString()
+ "人口总量为:"
+ pTemp.get(
1)
.toString());


mChart = (PieChart) layout
.findViewById(R.id.spread_pie_chart);
mbarChart = (BarChart) layout2
.findViewById(R.id.spread_Bar_chart);
mLineChart = (LineChart) layout3
.findViewById(R.id.spread_Bar_Linechart);


if (pTemp
.get(2)
.toString()
.equalsIgnoreCase(
"锡林郭勒盟")) {


PieData mPieData = getPieData(
13, 100);


showChart(mChart,
mPieData);


pwDrawpoint = new PopupWindow(
layout);
pwDrawpoint
.setOutsideTouchable(false);
if (pwDrawpoint
.isShowing()) {


pwDrawpoint
.dismiss();// 关闭
}
pwDrawpoint
.setFocusable(true);
layout.measure(
View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
pwDrawpoint
.setWidth(1300);
pwDrawpoint
.setHeight(1400);
pwDrawpoint
.setBackgroundDrawable(context
.getResources()
.getDrawable(
R.drawable.popupbg));// 设置背景图片,不能在布局中设置,要通过代码来设置
pwDrawpoint
.setOutsideTouchable(true);


// pwDrawpoint.setBackgroundDrawable(getResources().getDrawable(R.color.gray));


pwDrawpoint
.showAtLocation(
layout,
Gravity.CENTER,
0, 0);
}
if (pTemp
.get(2)
.toString()
.equalsIgnoreCase(
"兴安盟")) {


BarData mBarData = getBarData(
6, 3030298);


showBarchart(mbarChart,
mBarData);


pwDrawpoint = new PopupWindow(
layout2);
pwDrawpoint
.setOutsideTouchable(false);
if (pwDrawpoint
.isShowing()) {


pwDrawpoint
.dismiss();// 关闭
}
pwDrawpoint
.setFocusable(true);
layout2.measure(
View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
pwDrawpoint
.setWidth(1300);
pwDrawpoint
.setHeight(1400);
pwDrawpoint
.setBackgroundDrawable(context
.getResources()
.getDrawable(
R.drawable.popupbg));// 设置背景图片,不能在布局中设置,要通过代码来设置
pwDrawpoint
.setOutsideTouchable(true);


// pwDrawpoint.setBackgroundDrawable(getResources().getDrawable(R.color.gray));


pwDrawpoint
.showAtLocation(
layout2,
Gravity.CENTER,
0, 0);


}
if (pTemp
.get(2)
.toString()
.equalsIgnoreCase(
"呼伦贝尔市")) {
LineData mLineData = getLineData(
5, 1020102);
showLineChart(
mLineChart,
mLineData,
Color.rgb(114,
188,
223));


pwDrawpoint = new PopupWindow(
layout3);
pwDrawpoint
.setOutsideTouchable(false);
if (pwDrawpoint
.isShowing()) {


pwDrawpoint
.dismiss();// 关闭
}
pwDrawpoint
.setFocusable(true);
layout3.measure(
View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
pwDrawpoint
.setWidth(1300);
pwDrawpoint
.setHeight(1400);
pwDrawpoint
.setBackgroundDrawable(context
.getResources()
.getDrawable(
R.drawable.popupbg));// 设置背景图片,不能在布局中设置,要通过代码来设置
pwDrawpoint
.setOutsideTouchable(true);


// pwDrawpoint.setBackgroundDrawable(getResources().getDrawable(R.color.gray));


pwDrawpoint
.showAtLocation(
layout3,
Gravity.CENTER,
0, 0);


}


return false;
}
});


}


});

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值