Hello guys, whats up?? Today in this blog we are going to see how to draw graphs in Android. For this purpose we are going to use a library called as "achartengine" . So , lets get started.
1) First of all create an Android Application Project.
2) Next go to "res/alyout/activity_main.xml" and create a LinearLayout there.
activity_main.xml
3) Now go to http://www.achartengine.org/content/download.html and download "achartengine" library from there.
4) Right click on the project file in eclipse and go to properties->Java Bild Path-> Libraries-> Export External Jar files and select the downloaded "achartengine" library from there.
5) Now go to "src/your_package_name/MainActivity.java" and paste the following code there.
MainActivity.java
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you
1) First of all create an Android Application Project.
2) Next go to "res/alyout/activity_main.xml" and create a LinearLayout there.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/label" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > </LinearLayout>
3) Now go to http://www.achartengine.org/content/download.html and download "achartengine" library from there.
4) Right click on the project file in eclipse and go to properties->Java Bild Path-> Libraries-> Export External Jar files and select the downloaded "achartengine" library from there.
5) Now go to "src/your_package_name/MainActivity.java" and paste the following code there.
MainActivity.java
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.*;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.graphics.Paint.Align;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private GraphicalView mChart;
private XYSeriesRenderer mCurrentRenderer;
private XYSeriesRenderer mCurrentRenderer1;
private XYSeriesRenderer mCurrentRenderer2;
private XYSeries mCurrentSeries;
private XYSeries mCurrentSeries1;
private XYSeries mCurrentSeries2;
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
private View rootView = null;
private void addSampleData1() {
double[] arrayOfDouble = { 20.0D, 30.0D, 67.0D, 80.0D, 85.0D, 94.0D,
99.0D, 100.0D, 102.0D, 104.0D, 106.0D, 108.0D };
for (int i = 0;; i++) {
if (i >= 12) {
return;
}
this.mCurrentSeries.add(i, arrayOfDouble[i]);
}
}
private void addSampleData2() {
double[] arrayOfDouble = { 30.0D, 55.0D, 80.0D, 90.0D, 91.0D, 100.0D,
102.0D, 105.0D, 110.0D, 115.0D, 120.0D, 125.0D };
for (int i = 0;; i++) {
if (i >= 12) {
return;
}
this.mCurrentSeries1.add(i, arrayOfDouble[i]);
}
}
private void addSampleDatamid() {
double[] arrayOfDouble = { 25.0D, 50.0D, 75.0D, 85.0D, 87.0D, 99.0D,
100.0D, 105.0D, 106.0D, 107.0D, 108.0D, 109.0D };
for (int i = 0;; i++) {
if (i >= 12) {
return;
}
this.mCurrentSeries2.add(i, arrayOfDouble[i]);
}
}
private void initChart() {
this.mCurrentSeries = new XYSeries("Min Price");
this.mCurrentSeries1 = new XYSeries("Max Price");
this.mCurrentSeries2 = new XYSeries("Predicted Price");
this.mDataset.addSeries(this.mCurrentSeries);
this.mDataset.addSeries(this.mCurrentSeries1);
this.mDataset.addSeries(this.mCurrentSeries2);
this.mCurrentRenderer = new XYSeriesRenderer();
this.mCurrentRenderer1 = new XYSeriesRenderer();
this.mCurrentRenderer2 = new XYSeriesRenderer();
this.mCurrentRenderer.setColor(-16776961);
this.mCurrentRenderer1.setColor(-16711936);
this.mCurrentRenderer2.setColor(-65536);
this.mCurrentRenderer.setPointStyle(PointStyle.SQUARE);
this.mCurrentRenderer1.setPointStyle(PointStyle.CIRCLE);
this.mCurrentRenderer2.setPointStyle(PointStyle.DIAMOND);
this.mCurrentRenderer.setFillPoints(true);
this.mCurrentRenderer1.setFillPoints(true);
this.mCurrentRenderer2.setFillPoints(true);
this.mRenderer.addSeriesRenderer(this.mCurrentRenderer);
this.mRenderer.addSeriesRenderer(this.mCurrentRenderer1);
this.mRenderer.addSeriesRenderer(this.mCurrentRenderer2);
this.mRenderer.setXTitle("month");
this.mRenderer.setYTitle("price");
this.mRenderer.setZoomButtonsVisible(true);
this.mRenderer.setPointSize(2.0F);
this.mRenderer.setShowGridX(true);
String[] arrayOfString = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
for (int i = 0;; i++) {
if (i >= arrayOfString.length) {
this.mRenderer.setXLabels(0);
this.mRenderer.setXLabelsAlign(Align.CENTER);
return;
}
this.mRenderer.addTextLabel(i, arrayOfString[i]);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume() {
super.onResume();
LinearLayout localLinearLayout = (LinearLayout) findViewById(R.id.label);
if (this.mChart == null) {
initChart();
addSampleData1();
addSampleData2();
addSampleDatamid();
this.mChart = ChartFactory.getLineChartView(this, this.mDataset,
this.mRenderer);
localLinearLayout.addView(this.mChart);
return;
}
this.mChart.repaint();
}
}
Now we are done. Post your questions in the comment. I will be happy to answer those.
For more info visit on facebook https://www.facebook.com/androidcoolstuffs
Thank you


0 comments:
Post a Comment