0

A problem occured while building a chart. Studio doesn't accept data in this form. Actually problem is only with month's names. Without them code works fine. MPAndroidChart version 3. In official examples i understood nothing). Studio says:Error:(46, 24) error: constructor BarData in class BarData cannot be applied to given types; required: IBarDataSet[] found: ArrayList,BarDataSet reason: varargs mismatch; ArrayList cannot be converted to IBarDataSet. Please tell me how to add strings with months. Thank you.

    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();

2 Answers 2

3

Buddy use version 3.0.4 and follow example below:

    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);
Sign up to request clarification or add additional context in comments.

1 Comment

Do let me know if you need more help. Happy coding mate :)
0

one days ago ,i have same question

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

change by:

final HashMap<Integer,String>nuMap = new HashMap<>();
nuMap.put(0,"Jan");
nuMap.put(1,"Feb");
nuMap.put(2,"Mar");
nuMap.put(3,"Apr");
nuMap.put(4,"May");
nuMap.put(5,"June");

and now setValueFormatter

XAxis xAxis = yourchart.getXAxis();
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return nuMap.get((int)value);
        }

    });

new it's OK you can plot the XAxis Label's word!

1 Comment

I did everything according your recommendation, but: Error:(82, 13) error: method does not override or implement a method from a supertype

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.