Thursday, 13 June 2013

android: achartengine BarChart


package com.example.graphtouch;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

GridView gridviewloader;
private String[] xAxisBarDisplayBottomValues;
private int[] xAxisTempPositions;
private int[] barValues;
private GraphicalView mChartView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

xAxisBarDisplayBottomValues = new String[] { "Jan", "Feb", "Mar", "Apr" };
xAxisTempPositions = new int[] { 0, 1, 2, 3 };
barValues = new int[] { 20, 60, 27, 90 };

final XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setBarWidth(60);
multiRenderer.setXLabels(0);// set X- Interval
multiRenderer.setYLabels(5);// set Y- Interval
multiRenderer.setInScroll(true);
multiRenderer.setShowAxes(true);
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.WHITE);
multiRenderer.setMarginsColor(Color.WHITE);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setYTitle("% to Total Listings");
multiRenderer.setZoomButtonsVisible(false);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setAxesColor(Color.parseColor("#949494"));
multiRenderer.setLabelsColor(Color.BLACK);
multiRenderer.setShowGrid(true);
multiRenderer.setYAxisMin(0);
multiRenderer.setYAxisMax(100);
multiRenderer.setXAxisMin(-0.5);
multiRenderer.setXAxisMax(3.5);
// multiRenderer.setBarSpacing(0.0);
multiRenderer.setFitLegend(true);
multiRenderer.setShowLegend(false);
multiRenderer.setZoomEnabled(true, false);
multiRenderer.setPanEnabled(true, false);
multiRenderer.setYLabelsAlign(Align.RIGHT);
multiRenderer.setMargins(new int[] { 45, 45, 45, 45 });
multiRenderer.setClickEnabled(true);

for (int i = 0; i < barValues.length; i++) {
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.BLACK);
// to give the gradient color to the bars.
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setGradientEnabled(true);
incomeRenderer.setGradientStart(0, Color.rgb(165, 192, 15));
incomeRenderer.setDisplayChartValues(true);
multiRenderer.addXTextLabel(xAxisTempPositions[i],
xAxisBarDisplayBottomValues[i]);
multiRenderer.addSeriesRenderer(incomeRenderer);
}

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

for (int i = 0; i < barValues.length; i++) {
XYSeries incomeSeries1 = new XYSeries("Income1");
incomeSeries1.add(xAxisTempPositions[i], barValues[i]);
dataset.addSeries(incomeSeries1);
}

LinearLayout rl = (LinearLayout) findViewById(R.id.icon_image);
mChartView = ChartFactory.getBarChartView(this, dataset, multiRenderer,
Type.STACKED);
rl.addView(mChartView);

}

}





No comments:

Post a Comment