尝试编译堆积条形图时出现以下错误。我已经复制了用于分组条形图的代码,并且我正在尝试将“堆叠”功能添加到此代码中。错误在下面的代码中的注释中标记,在每个条形图行中,如下例所示: “ BarEntry v1e1 = new BarEntry(0f,new float [] {10,20,20}); “ 这是代码:
public class StackedBarGraph extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.groupedbargraph);
BarChart chart = (BarChart) findViewById(R.id.mpandroidstackedbargraph );
BarData data = new BarData(getXAxisValues(),getDataSet()); //!! ignore the complaint - it compiles and works
chart.setData(data);
chart.setDescription("My Chart");
chart.animateXY(2000, 2000);
chart.invalidate();
}
private ArrayList<IBarDataSet> getDataSet() {
ArrayList<IBarDataSet> dataSets = null;
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
BarEntry v1e1 = new BarEntry(0f, new float[]{10,20,20}); // ERROR
valueSet1.add(v1e1); //THESE LINES ALL GIVE ABOVE ERROR
BarEntry v1e2 = new BarEntry(1f, new float[]{20,30,30}); // ERROR
valueSet1.add(v1e2);
BarEntry v1e3 = new BarEntry(2f, new float[]{30,50,40}); // ERROR
valueSet1.add(v1e3);
BarEntry v1e4 = new BarEntry(3f, new float[]{50,70,50}); // ERROR
valueSet1.add(v1e4);
BarEntry v1e5 = new BarEntry(4f, new float[]{70,40,60}); // ERROR
valueSet1.add(v1e5);
BarEntry v1e6 = new BarEntry(5f, new float[]{40,30,70}); // Jun
valueSet1.add(v1e6);
ArrayList<BarEntry> valueSet2 = new ArrayList<>();
BarEntry v2e1 = new BarEntry(0, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e1);
BarEntry v2e2 = new BarEntry(1, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e2);
BarEntry v2e3 = new BarEntry(2, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e3);
BarEntry v2e4 = new BarEntry(3, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e4);
BarEntry v2e5 = new BarEntry(4, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e5);
BarEntry v2e6 = new BarEntry(5, new float[]{10,20,30}); // ERROR
valueSet2.add(v2e6);
BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
barDataSet1.setColor(Color.rgb(0, 155, 0)); // Green
BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Brand 2");
barDataSet1.setColor(Color.rgb(0, 155, 155)); // Light Blue
dataSets = new ArrayList<>();
dataSets.add(barDataSet1);
dataSets.add(barDataSet2);
return dataSets;
}
private ArrayList<String> getXAxisValues() {
ArrayList<String> xAxis = new ArrayList<>();
xAxis.add("JAN");
xAxis.add("FEB");
xAxis.add("MAR");
xAxis.add("APR");
xAxis.add("MAY");
xAxis.add("JUN");
return xAxis;
}
答案 0 :(得分:0)
BarEntry包含以下构造函数:
BarEntry(float[] vals, int xIndex)
BarEntry(float val, int xIndex)
BarEntry(float[] vals, int xIndex, String label)
BarEntry(float val, int xIndex, Object data)
所以,你做的事情是不可能的。翻转你的论点......
即
BarEntry v1e1 = new BarEntry(new float[]{10,20,20}, 0);
请查看源代码了解详情。