0

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);

2 Answers 2

1

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 !

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

6 Comments

If its ok for you then share your project i will fix it for you as i have nothing to do today :)
great do u have skype ??
yes I do [email protected] but you can share it on dropbox and give me the link or google drive its feasible for me
its works now, i need to tap on the screeen to see the chart :O , now how can i add spaces between x values and make the chart scroll horizontally
Oh thats good! you can zoom it by pinching outside by default it can zoom.Check it if it works.
|
1

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 :)

3 Comments

LineData must takes ILineDataSet not xvalues and LineDataSet, which version of mp android chart you use
its 2.x.x, what's your version?
im using version 3.0 i think this is the problem so what x,x u use ?

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.