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
时间: 2024-10-12 19:47:41