码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发系列之AChartEngine

时间:2016-06-14 11:35:08      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

Android图表控件的开发

曾经开发过一个小程序,在Android电视机上面开发一个APP,用于显示一些统计图表的信息。最后找来找去基于Android Native开发有AChartEngine现成的组件可用,于是就直接导入jar包,看api文档进行了开发。当然这只是一个客户端程序,数据的来源需要通过服务端程序获得,项目中通过wcf返回json格式的数据。

代码片段

new Thread(new Runnable() {
    @Override
    public void run() {
        SoapObject soapObject = Util.callWS(nameSpace, methodName2,wsdl, null);
        if (soapObject != null) {
            String result = soapObject.getProperty(0).toString();
            Message msg = new Message();
            msg.what = chart2;
            msg.obj = result;
            handler.sendMessage(msg);
        }
    }
}).start();
/**
     * @param nameSpace
     *            WS的命名空间
     * @param methodName
     *            WS的方法名
     * @param wsdl
     *            WS的wsdl的完整路径名
     * @param params
     *            WS的方法所需要的参数
     * @return SoapObject对象
     */
    public static SoapObject callWS(String nameSpace, String methodName,
            final String wsdl, Map<String, Object> params) {
        final String SOAP_ACTION = nameSpace + "/service/" + methodName;
        SoapObject soapObject = new SoapObject(nameSpace, methodName);
        if ((params != null) && (!params.isEmpty())) {
            Iterator<Entry<String, Object>> it = params.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> e = (Map.Entry<String, Object>) it
                        .next();
                soapObject.addProperty(e.getKey(), e.getValue());
            }
        }

        final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;// 兼容.NET开发的Web Service
        envelope.setOutputSoapObject(soapObject);
        try {
            HttpTransportSE ht = new HttpTransportSE(wsdl);
            ht.call(SOAP_ACTION, envelope);
            if (envelope.getResponse() != null) {
                SoapObject result = (SoapObject) envelope.bodyIn;
                ht = null;
                return result;
            } else {
                return null;
            }
        } catch (Exception e) {
            Log.e("error", e.getMessage());
            return null;
        }
    }
String result2 = (String) msg.obj;
int[] colors2 = new int[] { Color.argb(255, 10, 44, 81),Color.argb(255, 182, 34, 49) };
DefaultRenderer renderer2 = SexChart.buildCategoryRenderer(colors2);

// String result =
// "{\"Result_List\":[ { \"SEX\":\"其它\",\"NUM\":\"1\"}, {\"SEX\":\"男\",\"NUM\":\"37\"}, {\"SEX\":\"女\",\"NUM\":\"2\"}]}";
List<Map<String, Object>> list2 = Util.convertJSON2List(result2, "Result_List", new String[] { "SEX", "NUM" });
CategorySeries categorySeries = new CategorySeries("Vehicles Chart");
for (Map<String, Object> map : list2) {
    categorySeries.add(map.get("SEX").toString(),
    Double.parseDouble(map.get("NUM").toString()));
}
View view2 = ChartFactory.getPieChartView(Main.this,categorySeries, renderer2);
if (view2 != null) {
   layout01.removeAllViews();
   layout01.destroyDrawingCache();
   layout01.addView(view2);
} else {
   Toast.makeText(Main.this, "统计结果获取失败。", Toast.LENGTH_LONG).show();
}
public static List<Map<String, Object>> convertJSON2List(String result,String name, String[] fields) {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    try {
        JSONArray array = new JSONObject(result).getJSONArray(name);
        for (int i = 0; i < array.length(); i++) {
           JSONObject object = (JSONObject) array.opt(i);
           Map<String, Object> map = new HashMap<String, Object>();
               for (String str : fields) {
                   map.put(str, object.get(str));
               }
               list.add(map);
           }
        } catch (JSONException e) {
            Log.e("error", e.getMessage());
        }
    return list;
}

 

效果图

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

参考资料:

http://www.achartengine.org/content/demo.html

Android开发系列之AChartEngine

标签:

原文地址:http://www.cnblogs.com/guwei4037/p/5583353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!