问题描述
我有以下数据可使用MPAndroidChart库以图表格式表示:
I have below data to represent in chart format using MPAndroidChart library:
Subject Marks
Needs Attention Average Outstanding
Hindi 1/5 2/5 2/5
English 2/7 3/7 2/7
Maths 1/3 1/3 1/3
Science 2/7 2/7 3/7
Physics 2/9 4/9 4/9
Sanskrit 3/7 3/5 0
我想要一个类似下面的图表:
And I want a chart like below:
以上图表仅是示例,我希望每个条形(堆叠)中有三种颜色.我已经用不同的颜色制作了条形图,但是不能制作这么多种颜色的图.在MPAndroidChart中可以吗?
The above chart is only sample, I want three colours in each bar (stacked). I have already made a bar chart with separate colors, but can't make such multiple coloured chart. Is it possible in MPAndroidChart?
推荐答案
这被称为堆积条形图",并且在MPAndroidChart中也是可能的.
This is called a "stacked bar chart" and it is possible in MPAndroidChart.
Github存储库中有一个针对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();
输出看起来像这样:
这篇关于是否可以使用MPAndroidChart创建多色(堆叠)条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!