0

I am trying to add MPAndroidChart to my app by using the following code, but i am getting an error on

BarData barData = new BarData(xData, barDataSet); ------this line

and the error is

 BarData in BarData cannot be applied to (java.util.ArrayList <java.lang.String>,
com.github.mikephil.charting.data.BarDataSet)

How to resolve this error, please help

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab1, container, false);
        TextView myTextView = (TextView)rootView.findViewById(R.id.textView5);
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/slabo.ttf");
        myTextView.setTypeface(typeface);
        barChart = (BarChart)rootView.findViewById(R.id.chart);
        float yValues [] = {10, 20, 30, 40, 50};
        String xValues [] = {"first", "second", "third", "four", "five"};
        drawBarGraph(yValues,xValues);
        return rootView;
    }
    private void drawBarGraph(float yValues[], String []xValues){
        ArrayList<BarEntry> yData = new ArrayList<>();
        for (int i = 0; i < yValues.length; i++){
            yData.add(new BarEntry(yValues[i],i));
        }
        ArrayList<String> xData = new ArrayList<>();
        for(int i = 0; i < xValues.length; i++){
            xData.add(xValues[i]);
        }
        BarDataSet barDataSet = new BarDataSet(yData, "Cells");
        barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

    BarData barData =  new BarData(xData, barDataSet);
    barData.setValueTextSize(13f);
    barData.setValueTextColor(Color.BLACK);

    barChart.setData(barData);
    barChart.invalidate();
}
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Rohit B
  • 51
  • 3
  • 10
  • I like MPCharts: https://github.com/PhilJay/MPAndroidChart – Juan Jun 18 '17 at 13:28
  • @Juan can you help me with the implementation of this library with firebase – Rohit B Jun 18 '17 at 13:36
  • I can help answering doubts you may have, but you have plenty examples here: https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample – Juan Jun 18 '17 at 14:12
  • @Juan i am trying to solve the above conflict, can you please help me so that it can be implemented successfully. – Rohit B Jun 20 '17 at 20:29

1 Answers1

0

1) I think you are passing in the (x,y) data the other way round. It should be Data.add(new BarEntry(i, yValues[i]));

2) You are passing in two parameters for BarData. It shouyld be only one.

barData =  new BarData(barDataSet);

3) For the labels use an IAxisValueFormatter and set the granularity between labels to the 1f which is the difference between the X axis values (0, 1, 2, 3, 4) used when setting the BarEntrys.

This is an example code based on your code, the examples in MPCharts GitHub, and this question on setting the labels for BarChart

public class ChartsActivity extends AppCompatActivity {
    BarChart barChart;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chart_layout);

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

        float yValues [] = {10, 20, 30, 40, 50};
        String xValues [] = {"first", "second", "third", "four", "five"};

        XAxis xAxis = barChart.getXAxis();
        xAxis.setGranularity(1f);
        xAxis.setGranularityEnabled(true);
        drawBarGraph(yValues,xValues);

    }

    private void drawBarGraph(float [] yValues, String [] xValues){
        ArrayList<BarEntry> yData = new ArrayList<>();
        for (int i = 0; i < yValues.length; i++){
            yData.add(new BarEntry(i,yValues[i]));
        }


        BarDataSet barDataSet = new BarDataSet(yData, "Cells");
        barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);


        BarData barData =  new BarData(barDataSet);

        barData.setValueTextSize(13f);
        barData.setValueTextColor(Color.MAGENTA);

        barChart.getXAxis().setValueFormatter(new LabelFormatter(xValues));
        barChart.setData(barData);
        barChart.invalidate();
    }



    public class LabelFormatter implements IAxisValueFormatter {
        private final String[] mlabels;

        public LabelFormatter(String[] labels) {
            this.mlabels = mlabels;
        }

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return mLabels[(int) value];
        }
    }
}
Juan
  • 5,525
  • 2
  • 15
  • 26
  • thanks a lot but i think there is an error in this code, the `private final String[ ] labels;` should be replaced with `private final String[ ] mLabels` and `this.labels = labels;` should be `mLabels = labels;` but thanks a lot again i really appreciate it. – Rohit B Jun 21 '17 at 07:55