Android achartengine drawing line chart with multiple series - android

I am trying to develop an application with achart engine which draws a chart with multiple series .
Here is my code :
import java.util.ArrayList;
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.content.Context;
import android.graphics.Color;
public class Graph {
private Context context;
XYMultipleSeriesDataset dataset;
XYMultipleSeriesRenderer renderer;
public static boolean ClickEnabled = true;
public Graph(Context context) {
this.context = context;
}
public void initData(ArrayList<Double> x,ArrayList<Double> y,ArrayList<Double> z){
XYSeries seriesX = new XYSeries("X");
for(int i =0 ; i< x.size();i++){
seriesX.add(x.get(i), i);
}
XYSeries seriesY = new XYSeries("Y");
for(int i =0 ; i< y.size();i++){
seriesY.add(y.get(i), i);
}
XYSeries seriesZ = new XYSeries("Z");
for(int i =0 ; i< z.size();i++){
seriesZ.add(z.get(i), i);
}
dataset = new XYMultipleSeriesDataset();
dataset.addSeries(seriesX);
dataset.addSeries(seriesY);
dataset.addSeries(seriesZ);
renderer = new XYMultipleSeriesRenderer();
}
public void setProperties(){
renderer.setPanEnabled(true, false);
renderer.setClickEnabled(ClickEnabled);
renderer.setBackgroundColor(Color.WHITE);
renderer.setApplyBackgroundColor(true);
renderer.setChartTitle("AccelerometerData");
renderer.setAxesColor(Color.BLACK);
XYSeriesRenderer renderer1 = new XYSeriesRenderer();
renderer1.setColor(Color.RED);
renderer1.setPointStyle(PointStyle.DIAMOND);
renderer.addSeriesRenderer(renderer1);
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.GREEN);
renderer2.setPointStyle(PointStyle.DIAMOND);
renderer.addSeriesRenderer(renderer2);
XYSeriesRenderer renderer3 = new XYSeriesRenderer();
renderer3.setColor(Color.BLUE);
renderer3.setPointStyle(PointStyle.DIAMOND);
renderer.addSeriesRenderer(renderer3);
}
public GraphicalView getGraph(){
return ChartFactory.getLineChartView(context, dataset, renderer);
}
}
I am getting a chart like this:
I want all three series parallelly and want to remove black background from chart;

For Change the black color you can do like this
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setMargins(new int[] { top, left, bottom, right });
renderer.setMarginsColor(Color.RED);
and for your second problem plz be more specific by screenshot or any refrence image.

Related

How to draw dotted line between axis on Bar graph android

I am using achartengine jar for Bar graph view. My requirement of Bar Graph design example diagram has attached below.
The top Image is My requiremnt bar graph
The Bottom Image is my output image.
My code is:
public class TestClass extends AppCompatActivity {
private GraphicalView BarChartView;
#SuppressLint("ParserError")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
if (BarChartView==null )
{
XYMultipleSeriesRenderer renderer = buildBarRenderer();
setChartSettings(renderer);
BarChartView = ChartFactory.getBarChartView(this, getBarDataset(), renderer, Type.DEFAULT);
LinearLayout playout = (LinearLayout) findViewById(R.id.graph2);
playout.addView(BarChartView);
} else
{
BarChartView.repaint();
BarChartView.setVisibility(View.GONE);
}
}
private void setChartSettings(XYMultipleSeriesRenderer renderer)
{
renderer.setYTitle("Hits");
renderer.setXAxisMin(0);
renderer.setXAxisMax(7);
renderer.setYAxisMin(0);
renderer.setYAxisMax(100);
}
private XYMultipleSeriesDataset getBarDataset()
{
XYSeries series1 = new XYSeries("A");
XYSeries series2 = new XYSeries("B");
XYSeries series3 = new XYSeries("C");
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
series1.add(3, 50);
dataset.addSeries(series1);
series2.add(4, 25);
dataset.addSeries(series2);
series3.add(5, 100);
dataset.addSeries(series3);
return dataset;
}
protected XYMultipleSeriesRenderer buildBarRenderer() {
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
//double[] range = {5,5,0};
mrenderer.setAxisTitleTextSize(16);
mrenderer.setChartTitleTextSize(20);
mrenderer.setLabelsTextSize(15);
mrenderer.setLegendTextSize(15);
mrenderer.setBarSpacing(0.5);
mrenderer.setXLabels(0);
mrenderer.addXTextLabel(0, "SAVINGS TOTAL");
mrenderer.addXTextLabel(1.5, "SPEND BREAKDOWN");
mrenderer.addXTextLabel(3, "DEPT");
mrenderer.setMargins(new int[]{20, 30, 15, 0});
mrenderer.setAxesColor(Color.WHITE);
mrenderer.setChartTitle("");
mrenderer.setXTitle("");
// mrenderer.setInitialRange(range, 1);
mrenderer.setBarWidth(100);
XYSeriesRenderer renderer1 = new XYSeriesRenderer();
renderer1.setColor(Color.GREEN);
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.RED);
XYSeriesRenderer renderer3 = new XYSeriesRenderer();
renderer3.setColor(Color.BLUE);
mrenderer.addSeriesRenderer(renderer1);
mrenderer.addSeriesRenderer(renderer2);
mrenderer.addSeriesRenderer(renderer3);
return mrenderer;
}
}
Kindly Please help me solve the Bar Grapg dotted line between x and Y axis.
If any one have idea please le me know. Thanks in advance.
use
renderer.setStroke(BasicStroke.DASHED);
or
renderer.setStroke(BasicStroke.DOTTED);

I need to Update the x-axis value 1-8 in a chart library android

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);

Line chart with custom y axis space issue in android

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...

Bar chart using database

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.

AChartEngine add second series

I have a simple TimeChart in AChartEngine, everything works good but i have no idea how to add second series. Every my try was ended by ANR. Have you got any ideas how to add another series? This is my code:
EDIT: I changed code, i tried add second line but i have ANR:
public class TestgrafActivity extends Activity {
private XYMultipleSeriesDataset mDataset;
private XYMultipleSeriesRenderer mRenderer;
List<double[]> values = new ArrayList<double[]>();
private GraphicalView mChartView;
private TimeSeries time_series,time_series2;
private LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (LinearLayout) findViewById(R.id.chart);
time_series = new TimeSeries("Temperature");
for (int i = 0; i < 50; i++){
long value = new Date().getTime();
time_series.add(new Date(value+(i*1000)), new Random().nextInt(25));
}
time_series2 = new TimeSeries("Temperature2");
for (int i = 0; i < 50; i++){
long value = new Date().getTime();
time_series2.add(new Date(value+(i*1000)), new Random().nextInt(40));
}
mDataset = new XYMultipleSeriesDataset();
mDataset.addSeries(time_series);
mDataset.addSeries(time_series2);
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.GREEN);
r.setPointStyle(PointStyle.CIRCLE);
r.setFillPoints(true);
r.setLineWidth(3);
r.setDisplayChartValues(true);
XYSeriesRenderer r2 = new XYSeriesRenderer();
r2.setColor(Color.RED);
r2.setPointStyle(PointStyle.CIRCLE);
r2.setFillPoints(true);
r2.setLineWidth(3);
r2.setDisplayChartValues(true);
mRenderer = new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(r);
mRenderer.addSeriesRenderer(r2);
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setPointSize(3f);
mRenderer.setPanEnabled(true, true);
mRenderer.setZoomEnabled(true, true);
mRenderer.addSeriesRenderer(r2);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(20);
mRenderer.setPanEnabled(true);
mRenderer.setChartTitle("Temperature");
mRenderer.setXTitle("Time");
mRenderer.setYTitle("Degrees C");
mRenderer.setZoomButtonsVisible(true);
mRenderer.setZoomEnabled(true);
mRenderer.setXLabels(12);
mRenderer.setYLabels(10);
mRenderer.setShowGrid(true);
mRenderer.setXLabelsAlign(Align.RIGHT);
mRenderer.setYLabelsAlign(Align.RIGHT);
mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,
"HH:mm:ss");
layout.addView(mChartView);
}
}
You have only one Series in this code. Where is the code where you try to add another series?
The general approach is this:
mDataset.addSeries(0, time_series);
mRenderer.addSeriesRenderer(0, time_renderer);
//...
//...
mDataset.addSeries(1, some_other_series);
mRenderer.addSeriesRenderer(1, some_other_renderer);
//...
//...

Categories

Resources