0

I am facing an issue in plotting my points using a LineChart in MPAndroidCharts library in Android.

Scenario

I want to make a chart like the image below. I have values for x-axis that might increase or decrease throughout my ArrayList.

Sample Image

PROBLEM I have a list of coordinates which has the x and y coordinates for the plot. But when the values in X-axis decrease from the previous one it throws an error

E/UncaughtException: java.lang.NegativeArraySizeException: -6
at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:178)

MY EFFORT

My model class that creates list of objects having lat-long

public List<DataSetModel> getData() {

    List<DataSetModel> dataSetModelList=new ArrayList<>();
    DataSetModel dataSetModel = new DataSetModel();
    dataSetModel.setxCoordinate(10.0f);
    dataSetModel.setyCoordinate(10.0f);
    dataSetModelList.add(dataSetModel);

    DataSetModel dataSetModel2 = new DataSetModel();
    dataSetModel2.setxCoordinate(15.0f);
    dataSetModel2.setyCoordinate(15.0f);
    dataSetModelList.add(dataSetModel2);

    DataSetModel dataSetModel3 = new DataSetModel();
    dataSetModel3.setxCoordinate(20.0f);
    dataSetModel3.setyCoordinate(18.0f);
    dataSetModelList.add(dataSetModel3);

    DataSetModel dataSetModel4 = new DataSetModel();
    dataSetModel4.setxCoordinate(23.0f);
    dataSetModel4.setyCoordinate(20.0f);
    dataSetModelList.add(dataSetModel4);

    DataSetModel dataSetModel5 = new DataSetModel();
    dataSetModel5.setxCoordinate(25.0f);
    dataSetModel5.setyCoordinate(18.0f);
    dataSetModelList.add(dataSetModel5);

    DataSetModel dataSetModel6 = new DataSetModel();
    dataSetModel6.setxCoordinate(23.0f);
    dataSetModel6.setyCoordinate(15.0f);
    dataSetModelList.add(dataSetModel6);


    DataSetModel dataSetModel8 = new DataSetModel();
    dataSetModel8.setxCoordinate(20.0f);
    dataSetModel8.setyCoordinate(16.0f);
    dataSetModelList.add(dataSetModel8);

    return dataSetModelList;
}

The code that I wrote to get the line-chart

LineChart chart = findViewById(R.id.LineChartId);

    List<DataSetModel> dataObjects=getData();
    List<Entry> entries = new ArrayList<Entry>();

    for (int i = 0; i < dataObjects.size(); i++) {
        entries.add(new Entry(dataObjects.get(i).getxCoordinate(),dataObjects.get(i).getyCoordinate()));
    }

    LineDataSet dataSet = new LineDataSet(entries, "Label1"); // add entries to dataset
    dataSet.setColor(R.color.colorSeverityHigh);
    dataSet.setLineWidth(3.0f);
    dataSet.setValueTextColor(R.color.colorSeverityHigh); // styling, ...
    dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);

    LineData lineData = new LineData(dataSet);

    chart.setData(lineData);

I even tried ScatterChart, but was unable to connect the dots in that case. Any help is appreciated, thanks

Lucas Cabrales
  • 2,073
  • 1
  • 12
  • 21
The Bat
  • 1,085
  • 1
  • 13
  • 31

1 Answers1

1

According to the library documentation, the LineChart only supports ordered values for the x-axis. The functionality you are trying to achieve is not supported.

The order of entries

Please be aware that this library does not officially support drawing LineChart data from an Entry list not sorted by the x-position of the entries in ascending manner. Adding entries in an unsorted way may result in correct drawing, but may also lead to unexpected behaviour. A List of Entry objects can be sorted manually or using the EntryXComparator:

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

The reason why this is required is because the library uses binary search algorithms for better performance only working on sorted lists.

I recommend that you use another library, such as SparkLine

Lucas Cabrales
  • 2,073
  • 1
  • 12
  • 21