Right now i have created a bar-chart in android using AchartEngine library.
Here my problem is,
After getting launch of the emulator, my application is showing the bar-charts instantly,
But my need is to show each bar one by one.Which means each bars should want to be visible one after another.
How to do this?
Suggestions please?
Please find my sources for reference
MainActivity.java
public class MainActivity extends Activity {
Button btn_result;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_result = (Button)findViewById(R.id.button1_invoke);
btn_result.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
Intent intnt = new Intent(MainActivity.this,Result_BarActivity.class);
startActivity(intnt);
}
}); }}
Result_BarActivity.java
public class Result_BarActivity extends Activity
{
RelativeLayout rltv;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.result_barchart);
final GraphicalView grfv =createIntent();
RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeLayout_ID);
rl.addView(grfv);
}
public GraphicalView createIntent()
{
rltv = (RelativeLayout)findViewById(R.id.RelativeLayout_ID);
String[] titles = new String[] { "Order's profit of the year", " " };
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 7, 2, 5, 8 });
values.add(new double[] {});
int[] colors = new int[] { Color.BLUE, Color.WHITE};
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.HORIZONTAL);
setChartSettings(renderer, "Profit for the year 2012", " ", " ",0,10, 0,10, Color.GRAY, Color.LTGRAY);
renderer.setXLabels(1);
renderer.addXTextLabel(0.75, "ord1");
renderer.addXTextLabel(1.75, "ord2");
renderer.addXTextLabel(2.75, "ord3");
renderer.addXTextLabel(3.75, "ord4");
renderer.setPanEnabled(true, false);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++)
{
SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(true);
}
final GraphicalView grfv = ChartFactory.getBarChartView(Result_BarActivity.this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
return grfv;
/* ChartFactory.getBarChartIntent(this, buildBarDataset(titles, values), renderer,Type.DEFAULT);*/
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors)
{
// creates a SeriesRenderer and initializes it with useful default values as well as colors
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(15);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
int length = colors.length;
for (int i = 0; i < length; i++)
{
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
renderer.addSeriesRenderer(r);
}
return renderer;
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,int labelsColor)
{
// sets lots of default values for this renderer
renderer.setChartTitle(title);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.MAGENTA);
}
protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values)
{
// adds the axis titles and values into the data-set
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++)
{
CategorySeries series = new CategorySeries(titles[i]);
double[] v = values.get(i);
int seriesLength = v.length;
for (int k = 0; k < seriesLength; k++)
{
series.add(v[k]);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}}
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" >
<Button
android:id="#+id/button1_invoke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="195dp"
android:text="Invoke barchart" />
</RelativeLayout>
result_barchart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout_ID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</RelativeLayout>
Thanks in advance...
You can use dynamic graph plot means keep sending data at particular time and repaint it
from other function send data to value list
int i = 0;
While(true)
{
int[] series ={1,2,1,3,4,5,3,2,1,} // ex: your input data to be ploted
Thread.sleep(300000); //aprox. 5minutes delay
update(series[i]);
}
in update() function add data
update(int newval)
{
value.add(newval);
}
After this repaint the graph
Related
I m trying to set doughnut chart inner circle radius and outter circle radius in aChartengine, below is my code :
public class MainActivity extends Activity {
GraphicalView gv;
RelativeLayout rl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<double[]> values1 = new ArrayList<double[]>();
values1.add(new double[] { 15, 5 });
gv = createIntent(values1);
rl = (RelativeLayout) findViewById(R.id.rel);
rl.addView(gv);
}
#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;
}
public GraphicalView createIntent(List<double[]> values1) {
List<String[]> titles = new ArrayList<String[]>();
titles.add(new String[] { " ", " " });
int[] colors = new int[] { Color.BLUE, Color.GREEN };
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setApplyBackgroundColor(true);
renderer.setShowLegend(false);
renderer.setShowLabels(false);
renderer.setStartAngle(270);
renderer.setBackgroundColor(Color.rgb(222, 222, 200));
renderer.setLabelsColor(Color.GRAY);
return ChartFactory.getDoughnutChartView(MainActivity.this,
buildMultipleCategoryDataset("Project budget", titles, values1),
renderer);
}
protected MultipleCategorySeries buildMultipleCategoryDataset(String title,
List<String[]> titles, List<double[]> values) {
MultipleCategorySeries series = new MultipleCategorySeries(title);
int k = 0;
for (double[] value : values) {
series.add(2007 + k + "", titles.get(k), value);
k++;
}
return series;
}
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] { 20, 30, 15, 0 });
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
But I have browser this query of setting radius , in many search I have found radius on canvas but in my case I don't have canvas.
Can anyone tell me how to set radius here in my code ?
renderer.setScale((flaot)1.3) solved my problem but the inner and outer radius is approx not prefect.
You have to just chage value of decCoef (variable in DoughnutChart class).
In DefaultRenderer add your own radius variable and create getter and setter.
now, use this variable in DoughnutChart.
for example ,
DefaultRenderer defaultRenderer = new DefaultRenderer();
defaultRenderer.setmWidth(0.1f);
In DoughnutChart class :
public class DoughnutChart extends RoundChart {
private float mWidth; // make your own variable and initialize in constructor
public DoughnutChart(MultipleCategorySeries dataset, DefaultRenderer renderer) {
super(null, renderer);
mDataset = dataset;
mWidth = renderer.getmWidth();
}
double decCoef = mWidth / cLength; // change value of decCoef by using our variable.
I want to display graph using Achartengine in Fragment.
In my layout file I have used empty linear layout to add graph returned by createIntent() method
but it only shows blank screen
I am using following code
My Fragment activity is as below
pls help
public class Analytics_Screen extends Fragment implements OnClickListener,SlideActivity.Callbacks{
View checkanalytics_view;
SlideActivity activity;
LinearLayout graph_container;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activity = (SlideActivity) getActivity();
metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
if (checkanalytics_view == null)
{
checkanalytics_view = inflater.inflate(R.layout.checkanalytics_screen, null, false);
}
activity.OpenLeftmenu(0);
SlideActivity.registerCallback(this);
graph_container = (LinearLayout)checkanalytics_view.findViewById(R.id.graph_container);
final GraphicalView gv =createIntent();
graph_container.addView(gv,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
return checkanalytics_view;
}
public GraphicalView createIntent()
{
// List<double[]> values = new ArrayList<double[]>();
System.out.println("==============create Intent Start==========================");
List<Double> dValues=new ArrayList<Double>();
dValues.add((double) 5000);
dValues.add((double) 6000);
dValues.add((double) 7000);
dValues.add((double) 8000);
dValues.add((double) 10000);
dValues.add((double) 5000);
dValues.add((double) 25000);
dValues.add((double) 50000);
dValues.add((double) 50000);
// values.add(new double[] { 5000, 7300, 9240,10230, 11300, 10040,14230, 12300, 14240});
int[] colors = new int[] { Color.parseColor("#77c4d3")};
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.VERTICAL);
setChartSettings(renderer, "Analitics", "", "",0.1,
12.5, 0,50000, Color.BLACK, Color.BLACK);
renderer.setXLabels(1);
renderer.setYLabels(10);
renderer.addXTextLabel(1, "Male");
renderer.addXTextLabel(2, "Female");
renderer.addXTextLabel(3, "Age 0-20");
renderer.addXTextLabel(4, "Age 21-30");
renderer.addXTextLabel(5, "Age 31-40");
renderer.addXTextLabel(6, "Age 41-50");
renderer.addXTextLabel(7, "Age 50+");
renderer.addXTextLabel(8, "Penciled");
renderer.addXTextLabel(9, "Attended");
/* renderer.addXTextLabel(10, "Oct");
renderer.addXTextLabel(11, "Nov");
renderer.addXTextLabel(12, "Dec");*/
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(true);
}
final GraphicalView grfv = ChartFactory.getBarChartView(activity, buildBarDataset(dValues), renderer,Type.DEFAULT);
System.out.println("===============Graph View=================="+grfv);
return grfv;
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(10);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(10);
renderer.setLegendTextSize(5);
renderer.setBarSpacing(1);
renderer.setMarginsColor(Color.parseColor("#EEEDED"));
renderer.setXLabelsColor(Color.BLACK);
renderer.setYLabelsColor(0,Color.BLACK);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
renderer.setInScroll(true);
int length = colors.length;
Log.d("Color Len",""+length);
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
// r.setChartvalueAngle(-90);
r.setChartValuesSpacing(25);
renderer.addSeriesRenderer(r);
}
return renderer;
}
protected XYMultipleSeriesDataset buildBarDataset(List<Double> values) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
CategorySeries series = new CategorySeries("Amount");
// Double v = values.get(0);
int seriesLength = values.size();
for (int k = 0; k < seriesLength; k++) {
series.add(values.get(k));
}
dataset.addSeries(series.toXYSeries());
return dataset;
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
int labelsColor) {
renderer.setChartTitle(title);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setMargins(new int[] {15,50,50,50 });
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
Use this instead
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors)
{
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(10);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(10);
renderer.setLegendTextSize(5);
renderer.setBarSpacing(1);
renderer.setMarginsColor(Color.parseColor("#EEEDED"));
renderer.setXLabelsColor(Color.BLACK);
renderer.setYLabelsColor(0, Color.BLACK);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
renderer.setInScroll(true);
int length = colors.length;
Log.d("Color Len", "" + length);
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[0]);
// r.setChartvalueAngle(-90);
// r.setChartValuesSpacing(25);
renderer.addSeriesRenderer(r);
return renderer;
}
I am not sure if my answer can solve your problem, but I was successful in displaying a graph in a fragment. This is what I did, following is my fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4DDBFF">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graphLayout"
android:background="#000000" />
</LinearLayout>
I created 2 classes (a)SideFragment.java (b) LineGraph.java
LineGraph.java generates the graph and the following method return the view in this class
public GraphicalView getView(Context context){
GraphicalView view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
In SideFragment.java I have a GraphicalView variable which is initialised in onCreate() by calling LineGraph.getView() as follows
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity().getApplicationContext();
line = new LineGraph();
graphview = line.getView(context);
}
And I add this Graphview is attached to the fragment Layout in onStart()
public void onStart() {
super.onStart();
Log.i(TAG, "onStart");
LinearLayout fragmentLayout = (LinearLayout) view.findViewById(R.id.graphLayout);
fragmentLayout.addView(graphview);
}
Since I am generating the graph dynamically I keep repainting the GraphView on a background thread (AsyncTask) which I have initiated in onActivityCreated()
protected void onProgressUpdate(String... values) {
Log.i(TAG, "onProgressUpdate");
graphview.repaint();
super.onProgressUpdate(values);
}
I hope this could be of some help to you.
I am using aChartEngine Library to draw donut in my android application.I want to make two donut - one inside another with two different color. But I am unable to make them with two different color.This is my donut class which draw donut in LinearLayout.
public class PunchStatGraph {
private GraphicalView mChartView2;
static int count = 2;
int[] Mycolors = new int[] { Color.parseColor("#D4272D"),
Color.parseColor("#6F1717") };
String[] average = { "SPEED", "FORCE"};
String[] max = { "SPEED", "FORCE"};
public Intent execute(Context context, LinearLayout parent, double avg_values[],double max_values[]) {
parent.removeAllViews();
int[] colors = new int[count];
for (int i = 0; i < count; i++) {
colors[i] = Mycolors[i];
}
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setShowLabels(false);
renderer.setBackgroundColor(Color.BLACK);
renderer.setPanEnabled(false);// Disable User Interaction
renderer.setScale((float) 1.4);
renderer.setInScroll(true); //To avoid scroll Shrink
renderer.setStartAngle(90);
renderer.setShowLegend(false);
MultipleCategorySeries categorySeries = new MultipleCategorySeries(
"Punch Graph");
categorySeries.add(average, avg_values);
categorySeries.add(max, max_values);
mChartView2 = ChartFactory.getDoughnutChartView(context,
categorySeries, renderer);
parent.addView(mChartView2);
return ChartFactory.getDoughnutChartIntent(context, categorySeries,
renderer, null);
}
protected DefaultRenderer buildCategoryRenderer(int[] colors) {
DefaultRenderer renderer = new DefaultRenderer();
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
}
Check Image also.Thanks in advance.
I cannot able to show the bar-charts on the next screen.Here i am using achartengine for creating a bar chart.And i didn't get any errors on the log-cat too.Suggestions please!..
Thanks for your precious time!..
Please find my sources for reference
Chart_MainActivity.java
public class Chart_MainActivity extends Activity
{
Button btn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button_invoke);
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent int_obj = new Intent(Chart_MainActivity.this,NextActivity.class);
startActivity(int_obj);
}
}); }}
NextActivity.java
public class NextActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
createIntent();
/*
Intent intent = createIntent();
startActivity(intent);
*/
}
public void createIntent()
{
String[] titles = new String[] { "Order's profit of the year", " " };
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 7, 2, 5, 8 });
values.add(new double[] {});
int[] colors = new int[] { Color.BLUE, Color.WHITE};
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.HORIZONTAL);
setChartSettings(renderer, "Profit for the year 2012", " ", " ",0,10, 0,10, Color.GRAY, Color.LTGRAY);
renderer.setXLabels(1);
renderer.addXTextLabel(0.75, "ord1");
renderer.addXTextLabel(1.75, "ord2");
renderer.addXTextLabel(2.75, "ord3");
renderer.addXTextLabel(3.75, "ord4");
renderer.setPanEnabled(true, false);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++)
{
SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(true);
}
ChartFactory.getBarChartIntent(this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors)
{
// creates a SeriesRenderer and initializes it with useful default values as well as colors
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(15);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
int length = colors.length;
for (int i = 0; i < length; i++)
{
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
renderer.addSeriesRenderer(r);
}
return renderer;
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,int labelsColor)
{
// sets lots of default values for this renderer
renderer.setChartTitle(title);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.GRAY);
}
protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values)
{
// adds the axis titles and values into the data-set
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++)
{
CategorySeries series = new CategorySeries(titles[i]);
double[] v = values.get(i);
int seriesLength = v.length;
for (int k = 0; k < seriesLength; k++)
{
series.add(v[k]);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.barchart_test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" android:maxSdkVersion="15"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".Chart_MainActivity"
android:label="#string/title_activity_chart__main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NextActivity"></activity>
</application>
</manifest>
i think you need to fix ur manifest xml and add intent to the .Nexy activity activity
<activity android:name=".NextActivity">
<intent-filter>
<action android:name="android.intent.action.next_activity" />
<category android:name="android.intent.category.DEFAULT"
</activity>
I want to use line chart in my application, I want to show it in below the image which is setting as background image for linear layout 1, in 2nd linear layout i want to add line chart, I'm using achart engine libraries. Null pointer exception at line 66(layout.addView(mChartView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));) please help
package com.android.linegraph;
import java.util.ArrayList;
import java.util.List;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
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.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class LineGraph extends Activity {
private GraphicalView mChartView;
XYMultipleSeriesRenderer renderer;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout2);
String[] titles = new String[] { "spens1" };
List<double[]> x = new ArrayList<double[]>();
x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
// }
List<double[]> values = new ArrayList<double[]>();
values
.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13,
10 });
int[] colors = new int[] { Color.BLUE };
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE };
renderer = buildRenderer(colors, styles);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
.setFillPoints(true);
}
setChartSettings(renderer, "SCORE CARD VIEW", "Attetempts",
"Marks Scored", 0.5, 12.5, -10, 40, Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
if (mChartView == null) {
Log.d("Oncreate ", "if (mChartView == null)");
mChartView = ChartFactory.getLineChartView(this, mDataset(titles,
x, values), renderer);
layout.addView(mChartView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
} else {
// mChartView.repaint();
Log.d("Oncreate ", "if (mChartView != null)");
}
}
private void setChartSettings(XYMultipleSeriesRenderer renderer3,
String title, String xTitle, String yTitle, double xMin,
double xMax, double yMin, double yMax, int axesColor,
int labelsColor) {
// TODO Auto-generated method stub
renderer3.setChartTitle(title);
renderer3.setXTitle(xTitle);
renderer3.setYTitle(yTitle);
renderer3.setXAxisMin(xMin);
renderer3.setXAxisMax(xMax);
renderer3.setYAxisMin(yMin);
renderer3.setYAxisMax(yMax);
renderer3.setAxesColor(axesColor);
renderer3.setLabelsColor(labelsColor);
}
private XYMultipleSeriesRenderer buildRenderer(int[] colors,
PointStyle[] styles) {
// TODO Auto-generated method stub
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
setRenderer(renderer, colors, styles);
return renderer;
}
private void setRenderer(XYMultipleSeriesRenderer renderer2, int[] colors,
PointStyle[] styles) {
// TODO Auto-generated method stub
renderer2.setAxisTitleTextSize(16);
renderer2.setChartTitleTextSize(20);
renderer2.setLabelsTextSize(15);
renderer2.setLegendTextSize(15);
renderer2.setPointSize(5f);
renderer2.setMargins(new int[] { 20, 30, 15, 20 });
int length = colors.length;
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[i]);
r.setPointStyle(styles[i]);
renderer2.addSeriesRenderer(r);
}
}
private XYMultipleSeriesDataset mDataset(String[] titles,
List<double[]> xValues, List<double[]> yValues) {
// TODO Auto-generated method stub
XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
addXYSeries(dataset1, titles, xValues, yValues, 0);
return dataset1;
}
private void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles,
List<double[]> xValues, List<double[]> yValues, int scale) {
// TODO Auto-generated method stub
int length = titles.length;
for (int i = 0; i < length; i++) {
XYSeries series = new XYSeries(titles[i], scale);
double[] xV = xValues.get(i);
double[] yV = yValues.get(i);
int seriesLength = xV.length;
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
}
}
<RelativeLayout android:id="#+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="#+id/linearLayout1"
android:background="#drawable/cricket" android:layout_alignParentLeft="true"
android:layout_height="wrap_content" android:layout_width="wrap_content"></LinearLayout>
<LinearLayout android:id="#+id/linearLayout2"
android:layout_below="#id/linearLayout1"
android:layout_alignParentLeft="true" android:layout_height="wrap_content"
android:layout_width="wrap_content"></LinearLayout>
</RelativeLayout>
I can't test this in practice now, but I think you are missing a call to
setContentView(R.layout.NAME_OF_YOUR_XML_FILE);
this could e.g. be after this line:
super.onCreate(savedInstanceState);
Without this the layout variable is null, thus causing the exception (my guess).