Reputation: 426
Can any one please give me at least one sample java code to create a bar chart in android using achartengine?I did some searches on Google but they are all not making me clear.
Thanks for your precious time!..
Upvotes: 1
Views: 1427
Reputation: 136
I'll try to give you a hint about a process of creating a Bar chart.
First, key ingredients:
Series - one series holds a set of data points which are the values for X and Y axis. It can be XYSeries, TimeSeries, CategorySeries... It depends what is the type of your data, and you can have more than one.
XYSeries series = new XYSeries("Series name");
Renderer - for every series you need one renderer. Renderer is used for giving some basic attributes to a series.
XYSeriesRenderer renderer = new XYSeriesRenderer();
Dataset - is a collection of series. You have to worry about order of adding series and renderers, it must be the same.
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
Multiple renderer - it contains the regular renderers, and trough it you can set a lot attributes of your chart in general, from limits of your X and Y axis, to zoom enabled, bar width, bar spacing, visibility of axis, labels, legend and grid and much more.
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
GraphicalView - basically it is a container for everything mentioned earlier. This element is your chart, and you just need to add it to appropriate view in your layout.
GraphicalView chartView;
example of assigning few attributes to renderers
renderer.setFillPoints(true);
renderer.setColor(getResources()
.getColor(R.color.black));
rendererRight.setDisplayChartValues(true);
mRenderer.setXAxisMin(0);
mRenderer.setXAxisMax(10);
mRenderer.setYAxisMin(0);
mRenderer.setYAxisMax(100);
mRenderer.setMarginsColor(colorWhite);
mRenderer.setZoomEnabled(false, false);
mRenderer.setPanEnabled(false, false);
mRenderer.setShowLabels(true);
mRenderer.setBarSpacing(0.5);
we will use Random to fill our series iterator i will be are X value, and random value from 0 to 99 will be are Y value
Random r = new Random();
for (int i = 0; i < 10; i++) {
series.add(i, r.nextInt(100));
}
Next lines are for adding renderers to multiple renderer, series to dataset
mRenderer.addSeriesRenderer(renderer);
dataset.addSeries(series);
last step in forming are Bar chart is using ChartFactory to create it we must add parameters context, dataset, multiple renderer, and a type which can be Default or Stacked
chartView = ChartFactory.getBarChartView(getActivity(), dataset, mRenderer, BarChart.Type.DEFAULT);
at the end we add are chart to appropriate view, in this example FrameLayout
(FrameLayout) findViewById(R.id.bar_chart)).addView(chartView);
Upvotes: 0
Reputation: 32391
You can also try to search for AChartEngine on youtube. You will find nice video tutorials on how to build charts with AChartEngine. There is one for bar charts.
Upvotes: 2
Reputation: 1860
AChartEngine provides a demo app which you can refer - http://code.google.com/p/achartengine/source/browse/#svn%2Ftrunk%2Fclient%2Fsrc%2Forg%2Fachartengine%2Fdemo
Upvotes: 0