Here Below showlineChart() method will be create piechart using Achartengine in Android.
In showlineChart() method org.achartengine.GraphicalActivity class is Responsable to create LineChart.
Android Activity(MainActivity.java)
package com.androidsurya.achartengine;
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.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
public class MainActivity extends Activity {
private String[] mMonth = new String[] {
"Jan", "Feb" , "Mar", "Apr", "May", "Jun",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showlineChart();
}
private void showlineChart(){
int[] x = { 1,2,3,4,5};
int[] income = { 240,467,259,570,500};
int[] expense = {200, 521, 290, 219, 457};
// Creating an XYSeries for Income
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Income
XYSeries expenseSeries = new XYSeries("Expenses");
// Adding data to Income and Expense Series
for(int i=0;i<x.length;i++){
incomeSeries.add(x[i], income[i]);
expenseSeries.add(x[i],expense[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.WHITE);
incomeRenderer.setPointStyle(PointStyle.CIRCLE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.BLUE);
expenseRenderer.setPointStyle(PointStyle.CIRCLE);
expenseRenderer.setFillPoints(true);
expenseRenderer.setLineWidth(3);
expenseRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setXTitle("Year 2011");
multiRenderer.setYTitle("Amount in Rupees");
multiRenderer.setZoomButtonsVisible(true);
for(int i=0;i<x.length;i++){
multiRenderer.addXTextLabel(i+1, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
// Creating an intent to plot line chart using dataset and multipleRenderer
Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
// Start Activity
startActivity(intent);
}
}
Android Manifest file
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidsurya.achartengine.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.achartengine.GraphicalActivity" />
</application>
Note:
Don't Forget Register this Below Activity In Manifest File
<activity android:name="org.achartengine.GraphicalActivity" />
Output Screenshot:
In showlineChart() method org.achartengine.GraphicalActivity class is Responsable to create LineChart.
Android Activity(MainActivity.java)
package com.androidsurya.achartengine;
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.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
public class MainActivity extends Activity {
private String[] mMonth = new String[] {
"Jan", "Feb" , "Mar", "Apr", "May", "Jun",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showlineChart();
}
private void showlineChart(){
int[] x = { 1,2,3,4,5};
int[] income = { 240,467,259,570,500};
int[] expense = {200, 521, 290, 219, 457};
// Creating an XYSeries for Income
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Income
XYSeries expenseSeries = new XYSeries("Expenses");
// Adding data to Income and Expense Series
for(int i=0;i<x.length;i++){
incomeSeries.add(x[i], income[i]);
expenseSeries.add(x[i],expense[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.WHITE);
incomeRenderer.setPointStyle(PointStyle.CIRCLE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.BLUE);
expenseRenderer.setPointStyle(PointStyle.CIRCLE);
expenseRenderer.setFillPoints(true);
expenseRenderer.setLineWidth(3);
expenseRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setXTitle("Year 2011");
multiRenderer.setYTitle("Amount in Rupees");
multiRenderer.setZoomButtonsVisible(true);
for(int i=0;i<x.length;i++){
multiRenderer.addXTextLabel(i+1, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
// Creating an intent to plot line chart using dataset and multipleRenderer
Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
// Start Activity
startActivity(intent);
}
}
Android Manifest file
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.androidsurya.achartengine.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.achartengine.GraphicalActivity" />
</application>
Note:
Don't Forget Register this Below Activity In Manifest File
<activity android:name="org.achartengine.GraphicalActivity" />
Output Screenshot:
No comments:
Post a Comment