Kom*_*pta 4 android mpandroidchart
我有一个堆栈条形图。我想在条形图的顶部显示一个值,而不是为每个条形堆栈显示一个值。我正在使用 MpAndroichart 库,默认情况下它会像第 1 周一样显示栏。
我想在栏上显示值,如第 1 周而不是第 2 周。
我今天也试图解决这个问题,我用一个小技巧完成了它。这是我所做的:
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;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我为 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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不仅会显示堆积条形图中的最高值,还会显示条形图中所有堆积值的总和。希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
1976 次 |
| 最近记录: |