Menu

Create multi-line series chart

Perform the following steps to create a multi-line series chart.

1. Create data set of XYSeries coordinates as follows:

  • Create the instance of XYMultipleSeriesDataset, and then create XYSeries instance for each series.
  • Add the coordinates to each XYSeries instance separately.
  • Add all the instances of XYSeries to the instance of XYMultipleSeriesDataset.
XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYSeries series1 = new XYSeries("XXX");
series1.add(1, 100);
series1.add(2, 150);
series1.add(3, 50);
series1.add(4, 25);
series1.add(5, 70); 
mDataset.addSeries(series1);
XYSeries series2 = new XYSeries("YYY");
series2.add(1, 124);
series2.add(2, 105);
series2.add(3, 112);
series2.add(4, 112);
series2.add(5, 93);
mDataset.addSeries(series2);

2. Create render style configuration for each XYseries that is being added in the dataset as follows:

  • Create the instance of XYMultipleSeriesRenderer, and then create a XYSeriesRenderer instance for each series.
  • Use each XYSeriesRenderer instance to create different rendering styles for different series.
  • Add the entire XYSeriesRenderer instance to the instance of XYMultipleSeriesRenderer.
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer1 = new XYSeriesRenderer();
renderer1.setColor(Color.GREEN);
renderer1.setPointStyle(PointStyle.CIRCLE);
renderer1.setFillPoints(true);
renderer1.setLineWidth(2F);
renderer1.setDisplayChartValues(true);

mRenderer.addSeriesRenderer(renderer1);
	
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.BLUE);
renderer2.setPointStyle(PointStyle.DIAMOND);
renderer2.setFillPoints(true);
renderer2.setLineWidth(3F);
renderer2.setDisplayChartValues(true);

mRenderer.addSeriesRenderer(renderer2);

3. Use the chart factory method to create the line chart.

View v = ChartFactory.getLineChartView(getActivity(), mDataset, mRenderer);

4. Add the returned graph view to your app’s view group for display.

Output