1
votes

I have a problem when I'm using "MPAndroidCharts" when I try to charge data into Bachar I use two ArrayList one ArrayList for Dates and one for data but it show's me a red line on BarData theData = new BarData(theDates,barDataSet);

My hole code for charts:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_statistics);
    barChart = (BarChart)findViewById(R.id.bargraph);
    BarInit();}


private void BarInit(){
        ArrayList<BarEntry> barEntries = new ArrayList<>();
        barEntries.add(new BarEntry(44f,0));
        barEntries.add(new BarEntry(88f,1));
        barEntries.add(new BarEntry(41f,2));
        barEntries.add(new BarEntry(85f,3));
        barEntries.add(new BarEntry(96f,4));
        barEntries.add(new BarEntry(25f,5));
        barEntries.add(new BarEntry(10f,6));
        BarDataSet barDataSet = new BarDataSet(barEntries,"Dates");
        ArrayList<String> theDates = new ArrayList<>();
        theDates.add("Mars");
        theDates.add("Avril");
        theDates.add("Dec");
        theDates.add("May");
        theDates.add("OCt");
        theDates.add("Nov");
        theDates.add("Fir");
        BarData theData = new BarData(theDates,barDataSet);//----Line of error
        barChart.setData(theData);
        barChart.setTouchEnabled(true);
        barChart.setDragEnabled(true);
        barChart.setScaleEnabled(true);
    }

And when I cast "theDates" into IBarDataSet it display an other crash using BarData theData = new BarData((IBarDataSet) theDates,barDataSet);

Process: com.example.transportor, PID: 13423 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.transportor/com.example.transportor.Statistics}: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.mikephil.charting.interfaces.datasets.IBarDataSet at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5235) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.mikephil.charting.interfaces.datasets.IBarDataSet at com.example.transportor.Statistics.BarInit(Statistics.java:239) at com.example.transportor.Statistics.onCreate(Statistics.java:73) at android.app.Activity.performCreate(Activity.java:6001) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1285)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5235)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 

1

1 Answers

2
votes

I think you're trying to do it like this.

private fun BarInit() {
    val barEntries: ArrayList<BarEntry> = ArrayList()
    barEntries.add(BarEntry(0f, 44f))
    barEntries.add(BarEntry(1f, 88f))
    barEntries.add(BarEntry(2f, 41f))
    barEntries.add(BarEntry(3f, 85f))
    barEntries.add(BarEntry(4f, 96f))
    barEntries.add(BarEntry(5f, 25f))
    barEntries.add(BarEntry(6f, 10f))
    val barDataSet = BarDataSet(barEntries, "Dates")
    val theDates: ArrayList<String> = ArrayList()
    theDates.add("Mars")
    theDates.add("Avril")
    theDates.add("Dec")
    theDates.add("May")
    theDates.add("OCt")
    theDates.add("Nov")
    theDates.add("Fir")
    barChart.xAxis.valueFormatter = IndexAxisValueFormatter(theDates)
    val theData = BarData(barDataSet) //----Line of error
    barChart.data = theData
    barChart.setTouchEnabled(true)
    barChart.isDragEnabled = true
    barChart.setScaleEnabled(true)
}

Take a look

Ofcource you can hide that Description Label and Grid Lines.

Edit with Java

private void BarInit() {
    ArrayList<BarEntry> barEntries = new ArrayList<>();
    barEntries.add(new BarEntry(0f, 44f));
    barEntries.add(new BarEntry(1f, 88f));
    barEntries.add(new BarEntry(2f, 41f));
    barEntries.add(new BarEntry(3f, 85f));
    barEntries.add(new BarEntry(4f, 96f));
    barEntries.add(new BarEntry(5f, 25f));
    barEntries.add(new BarEntry(6f, 10f));
    BarDataSet barDataSet = new BarDataSet(barEntries, "Dates");
    ArrayList<String> theDates = new ArrayList<>();
    theDates.add("Mars");
    theDates.add("Avril");
    theDates.add("Dec");
    theDates.add("May");
    theDates.add("OCt");
    theDates.add("Nov");
    theDates.add("Fir");
    barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(theDates));
    BarData theData = new BarData(barDataSet);//----Line of error
    barChart.setData(theData);
    barChart.setTouchEnabled(true);
    barChart.setDragEnabled(true);
    barChart.setScaleEnabled(true);
}