Friday, 7 June 2013

Android: achartengine bar chart.



 mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };



LayoutInflater li = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.grid_item, null);

LinearLayout rl = (LinearLayout) v.findViewById(R.id.icon_image);

int[] x = { 0, 1, 2, 3, 4, 5, 6, 7 };
int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 };

XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Income

// Adding data to Income Series
for (int i = 0; i < x.length; i++) {
incomeSeries.add(i, income[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

// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);

// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setInScroll(true);
multiRenderer.setShowAxes(false);
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.WHITE);
multiRenderer.setMarginsColor(Color.WHITE);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setXTitle("Year 2012");
multiRenderer.setYTitle("Amount in Dollars");
multiRenderer.setZoomButtonsVisible(false);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setAxesColor(Color.BLACK);
multiRenderer.setLabelsColor(Color.BLACK);

// for bar spacing
multiRenderer.setBarSpacing(1.0);

for (int i = 0; i < x.length; i++) {
multiRenderer.addXTextLabel(i, months[i]);
}

multiRenderer.addSeriesRenderer(incomeRenderer);

mChartView = ChartFactory.getBarChartView(context, dataset,
multiRenderer, null);
rl.addView(mChartView);

No comments:

Post a Comment