1

I am trying to create a line graph using MPAndroidChart. I have looked at many examples online and they all seem to suggest that you add data to a chart like this:

    LineChart graph = (LineChart) view.findViewById(R.id.chart);

    // creating list of entry
    ArrayList<Entry> entries = new ArrayList<>();
    entries.add(new Entry(4f, 0));
    entries.add(new Entry(8f, 1));
    entries.add(new Entry(6f, 2));
    entries.add(new Entry(2f, 3));
    entries.add(new Entry(18f, 4));
    entries.add(new Entry(9f, 5));

    // creating labels
    ArrayList<String> labels = new ArrayList<String>();
    labels.add("January");
    labels.add("February");
    labels.add("March");
    labels.add("April");
    labels.add("May");
    labels.add("June");

    graph.setData(entries, labels);

But I get a compiler error on setData saying:

setData (com.github.mikephil.charting.data.LineData) in Chart cannot be applied to (java.util.ArrayList, java.util.ArrayList)

I can fix the compiler error by changing my code to:

LineDataSet dataSet = new LineDataSet(entries, "# of Calls");
LineData lineData = new LineData(dataSet);

graph.setData(lineData);

But then I get this run time error:

FATAL EXCEPTION: main Process: com.ebucher.lisa, PID: 11365 java.lang.NegativeArraySizeException: -2 at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:181) at com.github.mikephil.charting.renderer.LineChartRenderer.drawValues(LineChartRenderer.java:570) at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:272) at android.view.View.draw(View.java:17469) at android.view.View.updateDisplayListIfDirty(View.java:16464) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.draw(View.java:17472) at android.view.View.updateDisplayListIfDirty(View.java:16464) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.updateDisplayListIfDirty(View.java:16459) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1373) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.updateDisplayListIfDirty(View.java:16459) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.updateDisplayListIfDirty(View.java:16459) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.updateDisplayListIfDirty(View.java:16459) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.updateDisplayListIfDirty(View.java:16459) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.updateDisplayListIfDirty(View.java:16459) at android.view.View.draw(View.java:17238) at android.view.ViewGroup.drawChild(ViewGroup.java:3921) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3711) at android.view.View.draw(View.java:17472) at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:3205) at android.view.View.updateDisplayListIfDirty(View.java:16464) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:325) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:331) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:366) at android.view.ViewRootImpl.draw(ViewRootImpl.java:3134) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2933) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2522) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920) at android.view.Choreographer.doCallbacks(Choreographer.java:695) at android.view.Choreographer.doFrame(Choreographer.java:631) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

I get the same errors when I try to create a bar graph instead of a line graph.

7
  • Try: LineData lineData = new LineData(labels, dataSet); Commented Jul 22, 2016 at 17:10
  • @Guardanis LineData (com.github.mikephil.charting.interfaces.datasets.ILineDataSet...) in LineData cannot be applied to (java.util.ArrayList<java.lang.String>, com.github.mikephil.charting.data.LineDataSet) Commented Jul 22, 2016 at 17:35
  • 1
    Strange, it's definitely available in v2.2.5 Commented Jul 22, 2016 at 18:03
  • @Guardanis I am using "com.github.PhilJay:MPAndroidChart:v3.0.0-beta1" I will try using 2.2.5 instead. Commented Jul 22, 2016 at 20:06
  • 1
    Have you tried sorting the entries by x-index? Also, please note that in version 3.0.0 the first paramater of Entry is the x-value, and the second one is the y-value, so chances are you'll want to switch the order of your Entry intialization, so it's e.g. new Entry(0, 4f);. Commented Jul 27, 2016 at 8:04

5 Answers 5

3

Starting with version 3.0.0-beta1 the Entry constructors have changed to Entry(float x, float y) so you'll have to first pass the x-index and then the corresponding y-value. Even though this breaks backwards compatibility it feels more natural now that float x-values are supported.

So you should change your code to this:

LineChart graph = (LineChart) view.findViewById(R.id.chart);

// creating list of entry
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(0, 4f));
entries.add(new Entry(1, 8f));
entries.add(new Entry(2, 6f));
entries.add(new Entry(3, 2f));
entries.add(new Entry(4, 18f));
entries.add(new Entry(5, 9f));

LineDataSet dataSet = new LineDataSet(entries, "# of Calls");
LineData lineData = new LineData(dataSet);

graph.setData(lineData);

Also, in order to do rendering optimizations the entries have to be sorted by x-value, which might be why your chart is crashing (since you've switched x- and y-values in your entries). The fixed code above is already properly sorted.

Sign up to request clarification or add additional context in comments.

Comments

1

The error is caused because you have unsorted values (not sorted by x-position). You can fix doing:

List<Entry> entries = ...;
Collections.sort(entries, new EntryXComparator());

Also check the documentation for more information on that issue.

Comments

0

I'm struggling with this error too.

I just found a method of avoid error.

List can't store index more than 6. You try list store index less than 5.

Example:

ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(4f, 0));
entries.add(new Entry(8f, 1));
entries.add(new Entry(6f, 2));
entries.add(new Entry(2f, 3));
entries.add(new Entry(18f, 4));

// creating labels
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");

But application sometimes finish and get NegativeArraySizeException:-2.

When you try list store index less than 4.

I think this error is v3.0.0-beta1's bug.

Sorry for my poor English. I hope you'll understand.

Comments

0

You are adding LinedataSet object to setDate instead of List of LinedataSet objects

Refer below code

LineDataSet dataSet = new LineDataSet(entries, "# of Calls");
ArrayList<ILineDataSet> dataSetList = new ArrayList<ILineDataSet>();
            dataSets.add(dataSet ); // add the datasets
LineData lineData = new LineData(dataSetList);

graph.setData(lineData);

Comments

-1

Try downgrading the dependency to:

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

Comments

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.