Is it possible to create a multiple color (stacked) bar chart using MPAndroidChart?

后端 未结 2 1258
耶瑟儿~
耶瑟儿~ 2020-12-10 22:04

I have below data to represent in chart format using MPAndroidChart library:

Subject Marks       
    Needs Attention Average Outstanding
Hindi    1/5     2/         


        
相关标签:
2条回答
  • 2020-12-10 22:18

    according to me, you can set that logically in mpchart library

    else

    you can use achartengine

    0 讨论(0)
  • 2020-12-10 22:19

    This is called a "stacked bar chart" and it is possible in MPAndroidChart.

    There is an example Activity in the Github repo for MPAndroidChart here generated by the following code:

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
    
        for (int i = 0; i < size + 1; i++) {
            float mult = (size + 1);
            float val1 = (float) (Math.random() * mult) + mult / 3;
            float val2 = (float) (Math.random() * mult) + mult / 3;
            float val3 = (float) (Math.random() * mult) + mult / 3;
    
            yVals1.add(new BarEntry(
                    i,
                    new float[]{val1, val2, val3},
                    getResources().getDrawable(R.drawable.star)));
        }
    
        BarDataSet set1;
    
        if (mChart.getData() != null &&
                mChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, "Statistics Vienna 2014");
            set1.setDrawIcons(false);
            set1.setColors(getColors());
            set1.setStackLabels(new String[]{"Births", "Divorces", "Marriages"});
    
            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);
    
            BarData data = new BarData(dataSets);
            data.setValueFormatter(new MyValueFormatter());
            data.setValueTextColor(Color.WHITE);
    
            mChart.setData(data);
        }
    
        mChart.setFitBars(true);
        mChart.invalidate();
    

    And the output looks like this:

    0 讨论(0)
提交回复
热议问题