我想从json数据下面创建一个图。
[{"month":"August 2020","total":"4587175.08"},{"month":"July 2020","total":"9151128.27"},{"month":"June 2020","total":"10859553.16"},{"month":"May 2020","total":"2600435.33"}]
该数据是从db到我的图形的,但是在插入实体的代码上面有一个错误,如下所示
public void onResponse(@NotNull Call<List<MonthlySales>> call, @NotNull Response<List<MonthlySales>> response) {
//check if the response body is null
if(response.body()!=null){
List<BarEntry> barEntries = new ArrayList<>();
for (MonthlySales monthlySales : response.body()) {
barEntries.add(new BarEntry(monthlySales.getMonth(),monthlySales.getTotal()));
}
BarDataSet dataSet = new BarDataSet(barEntries,"Monthly Sales");
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
BarData data = new BarData(dataSet);
data.setBarWidth(10f);
chart.setVisibility(View.VISIBLE);
chart.animateXY(2000, 2000);
chart.setData(data);
chart.setFitBars(true);
Description description = new Description();
description.setText("Sales per month");
chart.setDescription(description);
chart.invalidate();
我做错什么了?请注意,月份应该是xaxis标签,而总的yaxis,如何实现这一点呢?
发布于 2020-08-20 05:48:08
构造函数是一种特殊的方法,用于初始化对象。创建类的对象时调用构造函数。
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
检查类。
/**
* Constructor for normal bars (not stacked).
*
* @param x
* @param y
*/
public BarEntry(float x, float y) {
super(x, y);
}
public BarEntry(float x, float y, Object data) {
super(x, y, data);
}
public BarEntry(float x, float y, Drawable icon) {
super(x, y, icon);
}
public BarEntry(float x, float y, Drawable icon, Object data) {
super(x, y, icon, data);
}
public BarEntry(float x, float[] vals) {
super(x, calcSum(vals));
this.mYVals = vals;
}
public BarEntry(float x, float[] vals, Object data) {
super(x, calcSum(vals), data);
this.mYVals = vals;
}
public BarEntry(float x, float[] vals, Drawable icon) {
super(x, calcSum(vals), icon);
this.mYVals = vals;
}
public BarEntry(float x, float[] vals, Drawable icon, Object data) {
super(x, calcSum(vals), icon, data);
this.mYVals = vals;
}
演示
ArrayList<BarEntry> barEntries = new ArrayList<>();
barEntries.add(new BarEntry(1f, 0));
barEntries.add(new BarEntry(2f, 1));
https://stackoverflow.com/questions/63505219
复制相似问题