Amalo
Amalo

Reputation: 772

MPAndroid chart need to add values using array list

I have an arraylist of object, I need to create a line chart using this array of object, I have the minimum y value and maximum y value for y axis, and also I have string array for x axis. This 2 arrays doesn't have the same length.

My problem is in MPAndroidChart library I can create a line chart using just static values. But here I need to create it using dynamic values.

  for (int i=0;i<deals.size();i++) {

        float y = (float)deals.get(i).getPrice();

        values.add(new Entry(y, y));
    }

    // create a dataset and give it a type
    LineDataSet set1 = new LineDataSet(values, "DataSet 1");
    set1.setAxisDependency(YAxis.AxisDependency.LEFT);
    set1.setColor(ColorTemplate.getHoloBlue());
    set1.setValueTextColor(ColorTemplate.getHoloBlue());
    set1.setLineWidth(1.5f);
    set1.setDrawCircles(false);
    set1.setDrawValues(false);
    set1.setFillAlpha(65);
    set1.setFillColor(ColorTemplate.getHoloBlue());
    set1.setHighLightColor(Color.rgb(244, 117, 117));
    set1.setDrawCircleHole(false);

    // create a data object with the datasets
    LineData data = new LineData(set1);
    data.setValueTextColor(Color.WHITE);
    data.setValueTextSize(9f);

    // set data
    mChart.setData(data);

Upvotes: 0

Views: 3200

Answers (2)

Sandip Patel
Sandip Patel

Reputation: 501

You need to create two data set
1. For Y axis data
2. For X axis label

you can simply skip X value if the corresponding index has not value to display.

ArrayList<YourData> data= new ArrayList<>();
data.addAll(getDataFromDataSource());

ArrayList<String> xVals = new ArrayList<String>();
ArrayList<Entry> yVals = new ArrayList<>();

for (int i = 0; i <= data.size() - 1; i++) {            
        if(data.getName().equals(null)){
           xVals.add(i,"");
        }
        xVals.add(i, data.getName());
        yVals.add(new Entry(data.getValue(), i));
}

After creating data source, create LineDataSet object to apply Y axis line color, line width, etc.

LineDataSet set1 = new LineDataSet(yVals, "");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);

Create LineData object for X axis labels

LineData data = new LineData(xVals, set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);

And finally pass LineData object to LineChart

mChart.setData(data);

Your final code should look like this

ArrayList<YourData> data= new ArrayList<>();
data.addAll(getDataFromDataSource());

ArrayList<String> xVals = new ArrayList<String>();
ArrayList<Entry> yVals = new ArrayList<>();

for (int i = 0; i <= data.size() - 1; i++) {            
        if(data.getName().equals(null)){
           xVals.add(i,"");
        }
        xVals.add(i, data.getName());
        yVals.add(new Entry(data.getValue(), i));
}

LineDataSet set1 = new LineDataSet(yVals, "");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);

LineData data = new LineData(xVals, set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(9f);

mChart.setData(data);

Hope it works :)

Upvotes: 1

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

First you have to create ArrayList values , which i think you created upon adding values.add(new Entry(y, y)); you have to give index in second argument you are giving value in both so you should do something like values.add(new Entry(y,i));

Hope it helps, Do let me know if it works. I used Mpcharts library alot in case of further queries you can contact me. Best of luck !

Upvotes: 1

Related Questions