MPAndroidChart添加数据

时间:2018-03-02 09:20:51

标签: android charts mpandroidchart

构建图表时出现问题。 Studio不接受此处的数据 形成。实际上问题只出现在月份的名字上。没有他们代码工作正常。 MPAndroidChart版本3.在官方的例子中,我什么都不懂。 Studio说:错误:(46,24)错误:类BarData中的构造函数BarData不能应用于给定类型; required:IBarDataSet [] found:ArrayList,BarDataSet reason:varargs mismatch; ArrayList无法转换为IBarDataSet。请告诉我如何添加几个月的字符串。谢谢。

    barChart= (BarChart) findViewById(R.id.bargraph);

    List<BarEntry> calls = new ArrayList<>();
    calls.add(new BarEntry(0, 9f));
    calls.add(new BarEntry(1, 3f));
    calls.add(new BarEntry(2, 5f));
    calls.add(new BarEntry(3, 2f));
    calls.add(new BarEntry(4, 6f));
    calls.add(new BarEntry(5, 12f));

    BarDataSet barDataSet = new BarDataSet(calls,"num");


    ArrayList<String> months = new ArrayList<>();
    months.add("Jan");
    months.add("Feb");
    months.add("Mar");
    months.add("Apr");
    months.add("May");
    months.add("June");


    BarData data;
    data = new BarData(months,barDataSet);


    data.setBarWidth(0.9f);
    barChart.setData(data);
    barChart.setFitBars(true);
    barChart.invalidate();

1 个答案:

答案 0 :(得分:1)

Buddy使用3.0.4版并按照以下示例进行操作:

    ArrayList<BarEntry>() barEntries = new ArrayList<BarEntry>();

    barEntries.add(new BarEntry(0, 1));
    barEntries.add(new BarEntry(1, 2));
    barEntries.add(new BarEntry(2, 4));
    barEntries.add(new BarEntry(3, 6));
    barEntries.add(new BarEntry(4, 5));
    barEntries.add(new BarEntry(5, 7));

    barDataSet = new BarDataSet(barEntries, "Contracts");
    barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
    //        barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
    barDataSet.setColor(getColor("defaultYellow"));
    barDataSet.setHighlightEnabled(true);
    barDataSet.setHighLightColor(Color.RED);
    barDataSet.setValueTextSize(defaultValueTextSize);
    barDataSet.setValueTextColor(getColor("primaryDark"));

    BarData barData = new BarData(barDataSet);

    barChart.getDescription().setText("No. of Contracts signed in 6 months");
    barChart.getDescription().setTextSize(12);
    barChart.setDrawMarkers(true);
    barChart.setMarker(markerView(context));
    barChart.getAxisLeft().addLimitLine(lowerLimitLine(2,"Minimum",2,12,getColor("defaultOrange"),getColor("defaultOrange")));
    barChart.getAxisLeft().addLimitLine(upperLimitLine(5,"Target",2,12,getColor("defaultGreen"),getColor("defaultGreen")));
    barChart.getAxisLeft().setAxisMinimum(0);
    barChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTH_SIDED);

ArrayList<String> labels = new ArrayList<String> ();

    labels.add( "JAN");
    labels.add( "FEB");
    labels.add( "MAR");
    labels.add( "APR");
    labels.add( "MAY");
    labels.add( "JUN");


    barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels));
    barChart.animateY(1000);

    barChart.getXAxis().setGranularityEnabled(true);
    barChart.getXAxis().setGranularity(1.0f);
    barChart.getXAxis().setLabelCount(barDataSet.getEntryCount());

    barChart.setData(barData);