@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_piechart, container, false); mPieChart = (PieChart) rootView.findViewById(R.id.chart_pie); String cateItem = getArguments().getString("cate_item"); String keywordList = getArguments().getString("keyword_list"); mCateItem = (CateItemVo) GsonConverter.fromJson(cateItem, CateItemVo.class); mKeywords = (List<KeywordIcon>) GsonConverter.fromJsonArray(keywordList, KeywordIcon.class); for (int i = 0; i < mKeywords.size(); i++) { KeywordIcon icon = mKeywords.get(i); mKeyIds.add(icon.get_id()); } getPieChartData(mKeyIds); return rootView; }
private void setPieChartData(Map obj, int sum) { Iterator it = obj.keySet().iterator(); int count = obj.keySet().size(); if (count > 0) { float range = sum; float mult = range; ArrayList<Entry> yVals1 = new ArrayList<Entry>(); ArrayList<String> xVals = new ArrayList<String>(); // for (int i = 0; i < count + 1; i++) // xVals.add("A"); int idx = 0; while (it.hasNext()) { String key = it.next().toString(); int value = (int) Float.parseFloat(obj.get(key).toString()); float yVal = (value / mult) * 100; yVals1.add(new Entry((float) yVal, idx)); for (int i = 0; i < mKeywords.size(); i++) { KeywordIcon icon = mKeywords.get(i); if (key.equals(icon.get_id())) { xVals.add(icon.getKeyword()); continue; } } idx++; } PieDataSet dataSet = new PieDataSet(yVals1, ""); dataSet.setSliceSpace(3f); dataSet.setSelectionShift(5f); // add a lot of colors ArrayList<Integer> colors = new ArrayList<Integer>(); for (int c : ColorTemplate.VORDIPLOM_COLORS) colors.add(c); for (int c : ColorTemplate.JOYFUL_COLORS) colors.add(c); for (int c : ColorTemplate.COLORFUL_COLORS) colors.add(c); for (int c : ColorTemplate.LIBERTY_COLORS) colors.add(c); for (int c : ColorTemplate.PASTEL_COLORS) colors.add(c); colors.add(ColorTemplate.getHoloBlue()); dataSet.setColors(colors); PieData data = new PieData(xVals, dataSet); data.setValueFormatter(new PercentFormatter()); data.setValueTextSize(11f); data.setValueTextColor(Color.BLACK); // data.setValueTypeface(tf); mPieChart.setData(data); // undo all highlights mPieChart.highlightValues(null); mPieChart.invalidate(); } }