Menu

Create bar chart graph view

Perform the following steps to create a bar chart graph view.

1. Create data set of XYseries coordinates as follows:

a. Create the instance of XYMultipleSeriesDataset, and then create XYSeries instance.

b. Add the X and Y values using this Instance.

c. Once the X and Y coordinates are added to XYSeries, add the XYSeries instance to the XYMultipleSeriesDataset instance.

XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYSeries series1 = new XYSeries("XYZ");
series1.add(1, -34);
series1.add(2, 230);
series1.add(3, -112);
series1.add(4, 64);
series1.add(5, 193);
series1.add(6, 89);
series1.add(7, 90);
series1.add(8, 46);
series1.add(9, 50);
series1.add(10, 129);
series1.add(11, 50);
series1.add(12, 209);
series1.add(13,-50); 
mDataset.addSeries(series1);

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 XYSeriesRenderer instance.
  • Using the XYSeriesRenderer instance, configure the style. Refer the EDU SDK Javadoc for different style configuration.
  • Add XYSeriesRenderer instance to the instance of XYMultipleSeriesRenderer.
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[]{ 50, 50, 15, 10 });

XYSeriesRenderer renderer1 = new XYSeriesRenderer();
renderer1.setColor(Color.BLUE);
renderer1.setPointStyle(PointStyle.CIRCLE);
renderer1.setFillPoints(true);
renderer1.setLineWidth(2F);
renderer.addSeriesRenderer(renderer1);

3. Use the chart factory method to create the bar chart view.

String[] types = new String[]{ BarChart.TYPE };
View combinedChartView = ChartFactory. getBarChartView(this.getActivity(), mDataset, mRenderer, types);

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

Output