I am developing line chart using achartengine 1.1.0.jar.
I am facing one issue when my final line chart is drawn.
The y axis labels drawn without vertical space between labels and all labels drawn together.
I have attached screen shot of my line chart drawn.
Here i am also attaching snippet code with it.
package com.example.simplelinecharexample;
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.XYMultipleSeriesRenderer.Orientation;
import org.achartengine.renderer.XYSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.widget.LinearLayout;
public class main extends Activity {
private GraphicalView mChart;
private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
"Jun",
"Jul", "Aug" };
private String[] mweek = new String[] { "Mon", "Tue", "Wed", "Thu", "Fri",
"Sat",
"Sun" };
private String[] steps = new String[31];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i <= 15; i++)
if (i == 0)
steps[i] = String.valueOf(0);
else
steps[i] = String.valueOf(Integer.parseInt(steps[i - 1]) + 2000);
OpenChart();
}
private void OpenChart() {
int z[] = { 0, 1, 2, 3, 4, 5, 6, 7};
// int x[] = { 10, 18, 32, 21, 48, 60, 53, 80 };
int[] income = { 2000, 2500, 2700, 3000, 2800, 3500,5000 , 15000};
XYSeries xSeries = new XYSeries("X Series");
for (int i = 0; i < z.length; i++) {
xSeries.add(z[i], income[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(xSeries);
XYSeriesRenderer Xrenderer = new XYSeriesRenderer();
Xrenderer.setColor(Color.RED);
Xrenderer.setPointStyle(PointStyle.DIAMOND);
Xrenderer.setDisplayChartValues(true);
Xrenderer.setLineWidth(5);
Xrenderer.setFillPoints(true);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setChartTitle("X Vs Y Chart");`enter code here`
mRenderer.setXTitle("X Values");
mRenderer.setYTitle("Y Values");
mRenderer.setXLabels(0);![enter image description here][2]
mRenderer.setShowGrid(true);
mRenderer.setYLabels(49);
for (int i = 0; i < mweek.length; i++) {
mRenderer.addXTextLabel(i, mweek[i]);
}
mRenderer.addSeriesRenderer(Xrenderer);
mRenderer.setXLabelsAlign(Align.LEFT);
mRenderer.setYLabelsAlign(Align.RIGHT);
mRenderer.setLabelsTextSize(20);
mRenderer.setYAxisMin(0);
mRenderer.setXAxisMin(-1);
mRenderer.setYAxisMax(98000);
mRenderer.setPanEnabled(false, true);
mRenderer.setPanLimits(new double[]{0.0,Double.MAX_VALUE,0.0,98000});
mRenderer.setZoomEnabled(true, false);
mRenderer.setAntialiasing(true);
mRenderer.setMargins(new int[] { 60, 120, 60, 60 });
LinearLayout chart_container = (LinearLayout) findViewById(R.id.Chart_layout);
mChart = (GraphicalView) ChartFactory.getLineChartView(
getBaseContext(), dataset, mRenderer);
chart_container.addView(mChart);
}
}
I want add vertical space between y axis labels and it should auto scrollable.
Just replace your line
mRenderer.setYLabels(49);
to
mRenderer.setYLabels(10);
When you decrease the size of SetYlabels it will increase the space between Y labels.
Enjoy...
Related
I have a CombinedChart and I would like that LineChart will take left axis as reference and BarChart will take right axis as reference but I could not get it out.
I am trying the following code:
barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
but now both charts (LineChart and BarChart) are on the left (mixing them and right axis have disappeared).
Left and right axis have different granularity.
How can I properly make that each chart takes as reference one different Y axis (left or right respectively)?
Thanks in advance!
This is certainly possible in MPAndroidChart version 3.0.2.
As you can observe from the screenshot below, the LineDataSet has AxisDependency.LEFT (scaled from 0 - 21) while the BarDataSet has AxisDependency.RIGHT (scaled from 0 - 50) and, as required, they have different granularity:
Here is a proof of concept for generating the above chart that is a simple adaptation of the example project on GitHub:
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.WindowManager;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.CombinedChart.DrawOrder;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import java.util.ArrayList;
public class CombinedChartActivity extends FragmentActivity {
private String[] mMonths = new String[] {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
};
private CombinedChart mChart;
private final int itemcount = 12;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combined);
mChart = (CombinedChart) findViewById(R.id.chart1);
mChart.getDescription().setEnabled(false);
mChart.setBackgroundColor(Color.WHITE);
mChart.setDrawGridBackground(false);
mChart.setDrawBarShadow(false);
mChart.setHighlightFullBarEnabled(false);
mChart.setDrawOrder(new DrawOrder[]{
DrawOrder.BAR, DrawOrder.BUBBLE, DrawOrder.CANDLE, DrawOrder.LINE, DrawOrder.SCATTER
});
Legend l = mChart.getLegend();
l.setWordWrapEnabled(true);
l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setGranularity(10f);
rightAxis.setAxisMinimum(0f);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setDrawGridLines(false);
rightAxis.setGranularity(1f);
leftAxis.setAxisMinimum(0f);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTH_SIDED);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return mMonths[(int) value % mMonths.length];
}
});
CombinedData data = new CombinedData();
data.setData(generateLineData());
data.setData(generateBarData());
xAxis.setAxisMaximum(data.getXMax() + 0.25f);
mChart.setData(data);
mChart.invalidate();
}
private LineData generateLineData() {
LineData d = new LineData();
ArrayList<Entry> entries = new ArrayList<Entry>();
for (int index = 0; index < itemcount; index++)
entries.add(new Entry(index + 0.5f, getRandom(15, 5)));
LineDataSet set = new LineDataSet(entries, "Line DataSet");
set.setColor(Color.rgb(0,100,0));
set.setLineWidth(2.5f);
set.setCircleColor(Color.rgb(0,100,0));
set.setCircleRadius(5f);
set.setFillColor(Color.rgb(0,100,0));
set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set.setDrawValues(true);
set.setValueTextSize(10f);
set.setValueTextColor(Color.rgb(0,100,0));
set.setAxisDependency(YAxis.AxisDependency.LEFT);
d.addDataSet(set);
return d;
}
private BarData generateBarData() {
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
for (int index = 0; index < itemcount; index++) {
entries.add(new BarEntry(index, getRandom(25, 25)));
}
BarDataSet set = new BarDataSet(entries, "Bar DataSet");
set.setColor(Color.rgb(60, 220, 78));
set.setValueTextColor(Color.rgb(60, 220, 78));
set.setValueTextSize(10f);
set.setAxisDependency(YAxis.AxisDependency.RIGHT);
return new BarData(set);
}
private float getRandom(float range, float startsfrom) {
return (float) (Math.random() * range) + startsfrom;
}
}
I have placed a chart library graph in my app, but the x-axis values are rounded off like 0,2,6,8 i want to show it as 1,2,3,4,5,6,7,8.
LineChart.java
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.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class LineChart extends Activity {
public static final String TYPE = "type";
private XYMultipleSeriesDataset mDataset = getDemoDataset();
private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
private GraphicalView mChartView;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart);
setRendererStyling();
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getLineChartView(this, mDataset,
mRenderer);
mRenderer.setSelectableBuffer(100);
layout.addView(mChartView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
} else
mChartView.repaint();
}
private void setRendererStyling() {
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPointSize(10);
}
private XYMultipleSeriesDataset getDemoDataset() {
double[] seriesFirstY = {20,-20,67,180,-45,24,99,-34,-8};
double[] seriesSecondY = {10,80,-40,-20,135,24,199,-34,80};
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYSeries firstSeries = new XYSeries("Sample series One");
for (int i = 0; i < 9; i++)
firstSeries.add(i, seriesFirstY[i]);
dataset.addSeries(firstSeries);
XYSeries secondSeries = new XYSeries("Sample series Two");
for (int j = 0; j < 9; j++)
secondSeries.add(j, seriesSecondY[j]);
dataset.addSeries(secondSeries);
return dataset;
}
private XYMultipleSeriesRenderer getDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
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.WHITE);
r.setFillPoints(true);
renderer.addSeriesRenderer(r);
r = new XYSeriesRenderer();
r.setPointStyle(PointStyle.CIRCLE);
r.setColor(Color.GREEN);
r.setFillPoints(true);
renderer.addSeriesRenderer(r);
renderer.setAxesColor(Color.DKGRAY);
renderer.setLabelsColor(Color.LTGRAY);
return renderer;
}
}
activity_Line_chart.xml
Layout for displaying the graph
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/chart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.81"
android:orientation="horizontal" />
</LinearLayout>
Referred from this site
You have an option to set a number of labels for desired axis. In your case you need this piece of code
mRendererHR.setXLabels(8);
We are creating bar chart for file manager application, we have to display different color for each bar.
Written following code globally.
GraphicalView chartView = null;
private CategorySeries series = new CategorySeries("Space");
private XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> sizes = new ArrayList<String>();
Written following code to display different colors.
public void getBarChart(){
for(int i = 0;i < name.length; i++){
series.add(name[i],values[i]);
multiRenderer.addXTextLabel(i, name[i]);
}
dataset.addSeries(series.toXYSeries());
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.rgb(0, 255, 255));
renderer.setFillPoints(true);
renderer.setLineWidth(2);
renderer.setDisplayChartValues(true);
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Chart Used Space");
multiRenderer.setXTitle("Name of Directory");
multiRenderer.setAxisTitleTextSize((float)18);
multiRenderer.setChartTitleTextSize((float)20);
multiRenderer.setYTitle("Amount of Space in MB");
multiRenderer.setXLabelsAngle((float)35);
multiRenderer.setLabelsTextSize((float)15);
multiRenderer.setXLabelsAlign(Align.LEFT);
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setBarSpacing((float) 1.0);
multiRenderer.addSeriesRenderer(renderer);
chartView = ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, org.achartengine.chart.BarChart.Type.DEFAULT);
}
The bar color in a bar chart is given by its renderer. Each series has a renderer. In order to have a separate color for each bar you will have to add several series, each one having only one bar.
Hi Have a look at this code.
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
LinearLayout linearChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearChart = (LinearLayout) findViewById(R.id.linearChart);
int colerloop[] = { 1, 2, 2, 2, 3, 3, 3, 3, 1, 1 };
int heightLoop[] = { 300, 200, 200, 200, 100, 100, 100, 100, 300, 300 };
for (int j = 0; j < colerloop.length; j++) {
drawChart(1, colerloop[j], heightLoop[j]);
}
}
public void drawChart(int count, int color, int height) {
System.out.println(count + color + height);
if (color == 3) {
color = Color.RED;
} else if (color == 1) {
color = Color.BLUE;
} else if (color == 2) {
color = Color.GREEN;
}
for (int k = 1; k <= count; k++) {
View view = new View(this);
view.setBackgroundColor(color);
view.setLayoutParams(new LinearLayout.LayoutParams(25, height));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view
.getLayoutParams();
params.setMargins(3, 0, 0, 0); // substitute parameters for left,
// top, right, bottom
view.setLayoutParams(params);
linearChart.addView(view);
}
}
}
For more details please visit the below link.
http://www.androidhub4you.com/2013/05/custom-bar-chart-in-android-dynamic.html
If you follow above link your bar chart will be look like below image.
http://4.bp.blogspot.com/-V7SwZZIRPo8/UaH3pR99oSI/AAAAAAAAAhY/N8etB_mAzec/s1600/device-2013-05-26-171036.png
I am new to android. I am creating a bar chart using achartengine. I am not able to plot the bar chart. Can you please tell me what am I doing wrong here. I have created a database using sqlite data browser and copied the data base into the assets folder of my project.
I had written a code to draw the bar chart with static values and now i modified it to take values from a database.
package flu.solutions.travelsense;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
public class ChartActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
XYMultipleSeriesRenderer renderer = getBarDemoRenderer();
setChartSettings(renderer);
Intent intent = ChartFactory.getBarChartIntent(this, getBarDemoDataset(), renderer, Type.DEFAULT);
startActivity(intent);
}
public Intent createIntent()
{
String[] titles = new String[] { "Top 10 Destinations", " " };
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 8, 6, 4, 6, 7, 7, 9, 5, 7, 8, 8 });
values.add(new double[] {});
int[] colors = new int[] { Color.RED, Color.BLACK};
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.HORIZONTAL);
setChartSettings(renderer);
renderer.setXLabels(1);
renderer.setYLabels(10);
renderer.addXTextLabel(1, "Bangalore");
renderer.addXTextLabel(2, "Mysore");
renderer.addXTextLabel(3, "Chennai");
renderer.addXTextLabel(4, "Delhi");
renderer.addXTextLabel(5, "Kolkatta");
renderer.addXTextLabel(6, "Kashmir");
renderer.addXTextLabel(7, "Hyderabad");
renderer.addXTextLabel(8, "Mumbai");
renderer.addXTextLabel(9, "Kerala");
renderer.addXTextLabel(10, "Gujarat");
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++)
{
SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(false);
}
return ChartFactory.getBarChartIntent(this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
}
public XYMultipleSeriesRenderer getBarDemoRenderer() {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setMargins(new int[] {20, 30, 15, 0});
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(Color.BLUE);
renderer.addSeriesRenderer(r);
r = new SimpleSeriesRenderer();
r.setColor(Color.GREEN);
renderer.addSeriesRenderer(r);
return renderer;
}
private XYMultipleSeriesDataset getBarDemoDataset() {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
final int nr = 10;
Random r = new Random();
int SERIES_NR = 2;
for (int i = 0; i < SERIES_NR; i++) {
CategorySeries series = new CategorySeries("Demo series " + (i + 1));
for (int k = 0; k < nr; k++) {
series.add(100 + r.nextInt() % 100);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
private void setChartSettings(XYMultipleSeriesRenderer renderer) {
renderer.setChartTitle("Chart demo");
renderer.setXTitle("x values");
renderer.setYTitle("y values");
renderer.setXAxisMin(0);
renderer.setXAxisMax(5);
renderer.setYAxisMin(0);
renderer.setYAxisMax(250);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_chart, menu);
return true;
}
}
your createIntent() method doesn't even look like its been referenced. i believe that the correct OnCreate would be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = createIntent();
startActivity(intent);
}
But you should know that you're eventually going to want to make your graph load as view rather than intent to get the real-time functionality that you wanted.
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).