/* Step 1: First you need to download the library from http://www.achartengine.org/content/download.html. */
/* Step 2: Now create a new project in Android Studio.
Copy the file “achartengine- 1.0.0.jar” to the app>libs folder.
Add the library as a dependency in build.gradle file.
Now rebuild the project you should see your library in the libs folder. */
/* Step 3: Now as our chart is a type of view so we need to create a layout for it. I have called my view “chartlay” */
<LinearLayout
android:id="@+id/chartlay"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:orientation="vertical">
/* Step 4: Now create an empty program which will be called in the onCreate method of the activity. I have called my program drawChart. */
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_charting);
drawChart();
}
/* Step 5: Now it is important to know that the achartengine library draws graph by taking a series as an argument not regular arrays.
So we need to create a series and then enter our data in that series. */
int[] x = {1,2,3,4,5,6,7,8};
int[] y = {10,15,12,13,15,19,12,18};
XYSeries series = new XYSeries("chart");
for(int i=0; i<x.length;i++){
series.add(x[i],y[i]);
}
/* In above code first we create our arrays which contain the x and y points we want to plot.
Then we use constructer XYSeries() to define a new series. I have called it simply “series”.
Now using series.add() method insert the data points in the series. */
/* Step 6: Now we need an XYMultipleSeriesDataset. XYMultipleSeriesDataset is passed as an argument to the drawing program at the end. */
XYMultipleSeriesDataset datas = new XYMultipleSeriesDataset();
I have called my dataset “datas”
Now add the series to fill the dataset.
datas.addSeries(series);
/* Step 7: Now we need a renderer. A renderer can be understood as a painter of your chart.
You can control various features of you chart through the renderer.
Some of them I have demonstrated like color of the graph, pointstyle etc. */
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.RED);
renderer.setPointStyle(PointStyle.DIAMOND);
renderer.setFillPoints(true);
renderer.setLineWidth(3);
renderer.setDisplayChartValues(true);
/* Step 8: Now create the XYMultipleSeriesRenderer. */
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
/* Almost done, just final few steps.
Add add your renderer to mRenderer. */
mRenderer.addSeriesRenderer(renderer);
/* Create a LinearLayout object and initialize it to our “chartlay” layout. */
LinearLayout chartContainer = (LinearLayout)findViewById(R.id.chartlay);
/* Create a new view with the arguments and add it to the layout. */
View chart = ChartFactory.getLineChartView(getBaseContext(),datas,mRenderer);
chartContainer.addView(chart);
Snippet that shows how to draw line graph
using Achart Engine lib ( http://www.achartengine.org/) engine in Android.
Full project sample can be seen at: http://www.compiletimeerror.com/2016/07/draw-line-graph-using-achart-engine-in.html#.WAUEvXUrK02
#achart #line #graphic #lineGraphic #canvas #android
#cesarnog
using Achart Engine lib ( http://www.achartengine.org/) engine in Android.
Full project sample can be seen at: http://www.compiletimeerror.com/2016/07/draw-line-graph-using-achart-engine-in.html#.WAUEvXUrK02
#achart #line #graphic #lineGraphic #canvas #android
#cesarnog
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.