2

I am trying to make a barchart which is going to be the result of specific calculated number, However when I put this into my code it comes up with the error:

the problem is between two stars getDataSet() and the error msg is cannot resolve constructor BarData(java.util.ArrayList<java.lang.String>java.util.ArrayList.com.gethub. mikefil.charting.Data.BarDataSet)

package com.example.firas.ti;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.graphics.Color;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BarChart chart = (BarChart) findViewById(R.id.chart);
        BarData data = new BarData(getXAxisValues(), **getDataSet()**);
        chart.setData(data);
        chart.setDescription("My Chart");
        chart.animateXY(2000, 2000);
        chart.invalidate();
    }

    private ArrayList<BarDataSet> getDataSet() {
        ArrayList<BarDataSet> dataSets = null;
        ArrayList<BarEntry> valueSet1 = new ArrayList<>();
        BarEntry v1e1 = new BarEntry(110.000f, 0); // Jan
        valueSet1.add(v1e1);
        BarEntry v1e2 = new BarEntry(40.000f, 1); // Feb
        valueSet1.add(v1e2);
        BarEntry v1e3 = new BarEntry(60.000f, 2); // Mar
        valueSet1.add(v1e3);
        BarEntry v1e4 = new BarEntry(30.000f, 3); // Apr
        valueSet1.add(v1e4);
        BarEntry v1e5 = new BarEntry(90.000f, 4); // May
        valueSet1.add(v1e5);
        BarEntry v1e6 = new BarEntry(100.000f, 5); // Jun
        valueSet1.add(v1e6);

        ArrayList<BarEntry> valueSet2 = new ArrayList<>();
        BarEntry v2e1 = new BarEntry(150.000f, 0); // Jan
        valueSet2.add(v2e1);
        BarEntry v2e2 = new BarEntry(90.000f, 1); // Feb
        valueSet2.add(v2e2);
        BarEntry v2e3 = new BarEntry(120.000f, 2); // Mar
        valueSet2.add(v2e3);
        BarEntry v2e4 = new BarEntry(60.000f, 3); // Apr
        valueSet2.add(v2e4);
        BarEntry v2e5 = new BarEntry(20.000f, 4); // May
        valueSet2.add(v2e5);
        BarEntry v2e6 = new BarEntry(80.000f, 5); // Jun
        valueSet2.add(v2e6);

        BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
        barDataSet1.setColor(Color.rgb(0, 155, 0));
        BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Brand 2");
        barDataSet2.setColors(ColorTemplate.COLORFUL_COLORS);

        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;
    }

}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Firas
  • 31
  • 1
  • 3

3 Answers3

1

Just change the below line

compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'

to this one

compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'

in build.gradle.

Alex M
  • 2,756
  • 7
  • 29
  • 35
0

Just change this:

private ArrayList<BarDataSet> getDataSet() {
    ArrayList<BarDataSet> dataSets = null;

to this:

private ArrayList<IBarDataSet> getDataSet() {
    ArrayList<IBarDataSet> dataSets = null;

Also it's better to use an interface instead of concrete implementation when declaring variables, for example instead of this:

ArrayList<Object> list = new ArrayList<Object>();

you use this:

List<Object> list = new ArrayList<Object>();

same for method return type. Check out a related stackoverflow post for more information

Community
  • 1
  • 1
x0r
  • 1,271
  • 11
  • 13
0

Take a look at the following example code: https://stackoverflow.com/a/38766149/1386969

The new constructor is used to create grouped bar chart. :)

enter image description here

Community
  • 1
  • 1
lidox
  • 1,901
  • 3
  • 21
  • 40