答案 0 :(得分:3)
我今天也试图解决这个问题而且我已经用一个小技巧完成了它。这就是我所做的:
private BarData generateBarData()
{
ArrayList<String> xVals = new ArrayList<>();
for(int i=0; i<mMonths.length; i++)
{
xVals.add(mMonths[i]);
}
ArrayList<BarEntry> yVals = new ArrayList<>();
yVals.add(new BarEntry(new float[] {15, 5, 5}, 0));
yVals.add(new BarEntry(new float[] {12, 2, 6}, 1));
yVals.add(new BarEntry(new float[] {15, 3, 4}, 2));
yVals.add(new BarEntry(new float[] {15, 5, 1}, 3));
yVals.add(new BarEntry(new float[] {13, 4, 1}, 4));
yVals.add(new BarEntry(new float[] {10, 2, 1}, 5));
yVals.add(new BarEntry(new float[] {15, 10, 2}, 6));
yVals.add(new BarEntry(new float[] {15, 2, 1}, 7));
yVals.add(new BarEntry(new float[] {14, 3, 6}, 8));
yVals.add(new BarEntry(new float[] {15, 7, 2}, 9));
yVals.add(new BarEntry(new float[] {15, 3, 1}, 10));
yVals.add(new BarEntry(new float[] {11, 2, 4}, 11));
ValueFormatter custom = new MyBarValueFormatter(mContext);
BarDataSet set = new BarDataSet(yVals, "Data Set");
set.setBarSpacePercent(45f);
set.setColors(getColors());
set.setHighlightEnabled(false);
set.setValueFormatter(custom);
set.setDrawValues(true);
BarData barData = new BarData(xVals, set);
return barData;
}
private int[] getColors() {
int[] colors = new int[3];
colors[0] = getResources().getColor(R.color.barColor1);
colors[1] = getResources().getColor(R.color.barColor2);
colors[2] = getResources().getColor(R.color.barColor3);
return colors;
}
在上面的代码中,我为BarChart生成了样本数据,我已经在 MyBarValueFormatter 类中完成了我的技巧,如下所示。
public class MyBarValueFormatter implements ValueFormatter {
private DecimalFormat mFormat;
private Context context;
int toggle;
float totalVal;
public MyBarValueFormatter(Context context){
toggle = 0;
totalVal = 0;
this.context = context;
mFormat = new DecimalFormat("###,###,###,##0");
}
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
if(toggle % 3 == 0){
toggle++;
totalVal = value;
return "";
}
else if(toggle % 3 == 1){
toggle++;
totalVal = totalVal + value;
return "";
}
else{
toggle++;
totalVal = totalVal + value;
return context.getResources().getString(R.string.rupee) + " " + mFormat.format(totalVal) + "000";
}
}
}
这不仅会显示堆积条形图中的最高值,还会显示条形图中所有堆叠值的总和。希望这有帮助!
答案 1 :(得分:0)
for (IDataSet set : mChart.getData().getDataSets()){
set.setDrawValues(set.isDrawValuesEnabled());
}
将此项用于栏顶部的显示值。