Android中的MPChart根据自定义值构建饼图

时间:2015-12-03 08:27:19

标签: android charts pie-chart mpandroidchart

这是库setData的方法。我有四个值q1,q2,q3,q4总共4个,所以我的总数应该是10000 q1=1000,q2=500,q3=500,q4=8000。所以我如何在这里传递所有这些值并生成我自己的图表,看看这些数据来自我的API,因此对于每个用户数据,如果不同,并根据该数据值构建图表。

   private void setData(int count, float range){

    float mult = range;

    ArrayList<Entry> yVals1 = new ArrayList<Entry>();

    // IMPORTANT: In a PieChart, no values (Entry) should have the same
    // xIndex (even if from different DataSets), since no values can be
    // drawn above each other.
    for (int i = 0; i < count + 1; i++) {
      //yVals1.add(new Entry((float) (Math.random() * mult) + mult / 5, i));
        yVals1.add(new Entry((float)(dislikeval), i));
    }

    ArrayList<String> xVals = new ArrayList<String>();

    for (int i = 0; i < count + 1; i++)
        xVals.add(mParties[i % mParties.length]);

    PieDataSet dataSet = new PieDataSet(yVals1, "Election Results");
    dataSet.setSliceSpace(2f);
    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);
    //dataSet.setSelectionShift(0f);

    PieData data = new PieData(xVals, dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(11f);
    data.setValueTextColor(Color.WHITE);
    data.setValueTypeface(tf);
    mChart.setData(data);

    // undo all highlights
    mChart.highlightValues(null);

    mChart.invalidate();
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码执行此操作:

private void setData() {
        // Label for each slice
        String[] valueArray = new String[] { "Q1", "Q2", "Q3" };
        // Values for pie chart
        ArrayList<Entry> yVals1 = new ArrayList<Entry>();
        yVals1.add(new Entry(1000, 0));
        yVals1.add(new Entry(2000, 2));
        yVals1.add(new Entry(3000, 1));

        ArrayList<String> xVals = new ArrayList<String>();

        for (int i = 0; i < valueArray.length + 1; i++)
            xVals.add(valueArray[i % valueArray.length]);

        PieDataSet dataSet = new PieDataSet(yVals1, "");
        dataSet.setSliceSpace(2f);
        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);
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(xVals, dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(12f);
        data.setValueTextColor(Color.WHITE);
        data.setValueTypeface(tf);
        mChart.setData(data);

        // undo all highlights
        mChart.highlightValues(null);

        mChart.invalidate();
    }