Here, 2 Line Graphs
- Random values
- Sin()
- Download achartengine-1.1.0.jar
- import in lib
MainActivity.java
package com.example.linechatachart;
import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
public class MainActivity extends Activity {
private static final int SERIES_NR = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = ChartFactory.getLineChartIntent(this, getDataset(),
getRenderer());
startActivity(intent);
}
private XYMultipleSeriesDataset getDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
final int n = 30;
double v = 0;
// Random r = new Random();
XYSeries series1 = new XYSeries("Demo series 1");
for (int i = 0; i < n; i++) {
v += 0.9;
series1.add(i, Math.sin(v));
}
XYSeries series2 = new XYSeries("Demo series 2");
series2.add(1.0, 4.2);
series2.add(5.0, 6.2);
series2.add(3.5, 12.2);
series2.add(12.3, 23.2);
series2.add(25.0, 14.2);
series2.add(14.0, 2.2);
series2.add(5.5, 9.2);
series2.add(10.3, 12.2);
series2.add(8, 25);
series2.add(5, 15.2);
dataset.addSeries(series2);
dataset.addSeries(series1);
return dataset;
}
private XYMultipleSeriesRenderer getRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(20);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(25);
renderer.setLegendTextSize(25);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.BLUE);
r.setPointStyle(PointStyle.SQUARE);
r.setFillBelowLine(true);
r.setFillBelowLineColor(Color.rgb(225, 225, 225));
r.setFillPoints(true);
renderer.addSeriesRenderer(r);
r = new XYSeriesRenderer();
r.setPointStyle(PointStyle.CIRCLE);
r.setColor(Color.RED);
r.setFillPoints(true);
r.setFillBelowLine(true);
// r.setFillBelowLineColor(Color.rgb(234, 241, 245));
renderer.addSeriesRenderer(r);
renderer.setAxesColor(Color.DKGRAY);
renderer.setLabelsColor(Color.LTGRAY);
return renderer;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
-----------------------------------------------------------------------------------------------------------------------
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
-----------------------------------------------------------------------------------------------------------------------
OutPut: