本文主要是介绍根据数据库数据画饼图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数据库传入数据为 (分类、该分类下消费金额)
效果图:
private HashMap<String,Integer> getMonthData(){HashMap<String,Integer> map = new HashMap<String,Integer>();ArrayList<String>kindlist = (ArrayList<String>) kdao.find();for(int i=0;i<kindlist.size();i++){int kindsum = mdao.findSumOfKind(application.getUser_id(),kindlist.get(i),null,(String) year_pic_time.getText());map.put(kindlist.get(i),kindsum);}yearDataMap = map;return map;}/*** 画饼图*/private void CreatePicBingTu(){year_pic.removeAllViews();CategorySeries dataset=buildCategoryDataset("月统计饼图", getMonthData());ArrayList<Integer>colors = new ArrayList<Integer>();colors.add(Color.BLACK);colors.add(Color.BLUE);colors.add(Color.GRAY);colors.add(Color.DKGRAY);colors.add(Color.RED);colors.add(Color.GREEN);ArrayList<Integer> colorlist= new ArrayList<Integer>();for(int i=0;i<yearDataMap.size();i++){colorlist.add(colors.get(i));}DefaultRenderer renderer=buildCategoryRenderer(colorlist);graphicalView=ChartFactory.getPieChartView(getBaseContext(), dataset, renderer);//饼状图year_pic.addView(graphicalView);}protected CategorySeries buildCategoryDataset(String title, HashMap<String, Integer> map) {CategorySeries series = new CategorySeries(title);Set set = map.entrySet();Iterator it = set.iterator(); Double sum = 0.0;while(it.hasNext()){java.util.Map.Entry entry = (java.util.Map.Entry)it.next();sum+=Double.parseDouble(entry.getValue().toString());}it = set.iterator();while(it.hasNext()){double hundred;java.util.Map.Entry entry = (java.util.Map.Entry)it.next();String key = entry.getKey().toString();double value = Double.parseDouble(entry.getValue().toString());if(sum>0){hundred = value/sum*100;series.add(key+" "+doubleFormat.format(hundred)+"%",value);}else series.add(key+" "+"0%",value);}return series;}protected DefaultRenderer buildCategoryRenderer(ArrayList<Integer> colorlist) {DefaultRenderer renderer = new DefaultRenderer();renderer.setLegendTextSize(23);//设置左下角表注的文字大小renderer.setLabelsTextSize(28.0f);renderer.setMargins(new int[] { 20, 30, 15,0 });//margins - an array containing the margin size values, in this order: top, left, bottom, rightfor (int i=0;i<colorlist.size();i++) {SimpleSeriesRenderer r = new SimpleSeriesRenderer();r.setColor(colorlist.get(i));renderer.addSeriesRenderer(r);}return renderer;}
这篇关于根据数据库数据画饼图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!