`
bogongjie
  • 浏览: 235913 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

android 图表引擎AChartEngine(柱状图)

阅读更多

使用AChartEngine画动态柱状图:

1.定义类:

package com.example.test.achartengine;

import java.util.Date;
import java.util.List;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.MultipleCategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

/**
 * An abstract class for the demo charts to extend. It contains some methods for building datasets and renderers.
 */
public class AbstractDemoChart {

	/**
	 * Builds an XY multiple dataset using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param xValues
	 *            the values for the X axis
	 * @param yValues
	 *            the values for the Y axis
	 * @return the XY multiple dataset
	 */
	protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		addXYSeries(dataset, titles, xValues, yValues, 0);
		return dataset;
	}

	public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues, List<double[]> yValues, int scale) {
		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);
		}
	}

	/**
	 * Builds an XY multiple series renderer.
	 * 
	 * @param colors
	 *            the series rendering colors
	 * @param styles
	 *            the series point styles
	 * @return the XY multiple series renderers
	 */
	protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		setRenderer(renderer, colors, styles);
		return renderer;
	}

	protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setPointSize(5f);
		renderer.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]);
			renderer.addSeriesRenderer(r);
		}
	}

	/**
	 * Sets a few of the series renderer settings.
	 * 
	 * @param renderer
	 *            the renderer to set the properties to
	 * @param title
	 *            the chart title
	 * @param xTitle
	 *            the title for the X axis
	 * @param yTitle
	 *            the title for the Y axis
	 * @param xMin
	 *            the minimum value on the X axis
	 * @param xMax
	 *            the maximum value on the X axis
	 * @param yMin
	 *            the minimum value on the Y axis
	 * @param yMax
	 *            the maximum value on the Y axis
	 * @param axesColor
	 *            the axes color
	 * @param labelsColor
	 *            the labels color
	 */
	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.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	/**
	 * Builds an XY multiple time dataset using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param xValues
	 *            the values for the X axis
	 * @param yValues
	 *            the values for the Y axis
	 * @return the XY multiple time dataset
	 */
	protected XYMultipleSeriesDataset buildDateDataset(String[] titles, List<Date[]> xValues, List<double[]> yValues) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		int length = titles.length;
		for (int i = 0; i < length; i++) {
			TimeSeries series = new TimeSeries(titles[i]);
			Date[] 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);
		}
		return dataset;
	}

	/**
	 * Builds a category series using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param values
	 *            the values
	 * @return the category series
	 */
	protected CategorySeries buildCategoryDataset(String title, double[] values) {
		CategorySeries series = new CategorySeries(title);
		int k = 0;
		for (double value : values) {
			series.add("Project " + ++k, value);
		}

		return series;
	}

	/**
	 * Builds a multiple category series using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param values
	 *            the values
	 * @return the category series
	 */
	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;
	}

	/**
	 * Builds a category renderer to use the provided colors.
	 * 
	 * @param colors
	 *            the colors
	 * @return the category renderer
	 */
	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;
	}

	/**
	 * Builds a bar multiple series dataset using the provided values.
	 * 
	 * @param titles
	 *            the series titles
	 * @param values
	 *            the values
	 * @return the XY multiple bar dataset
	 */
	protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
		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;
	}

	/**
	 * Builds a bar multiple series renderer to use the provided colors.
	 * 
	 * @param colors
	 *            the series renderers colors
	 * @return the bar multiple series renderer
	 */
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		renderer.setAxisTitleTextSize(16);
		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;
	}

}

 

柱状图类:

 

package com.example.test.achartengine;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;

public class DynamicBarChart {

	public AbstractDemoChart abstractDemoChart;
	public XYMultipleSeriesRenderer renderer;
	public XYMultipleSeriesDataset dataset;
	public GraphicalView chartView;

	private Context context;
	private int[] colors;
	private String[] titles;
	private List<double[]> values;

	public DynamicBarChart(Context context) {
		super();
		// TODO Auto-generated constructor stub
		this.context = context;
		abstractDemoChart = new AbstractDemoChart();
		init();
	}

	private void init() {
		titles = new String[] { "2014" };
		values = new ArrayList<double[]>();
		values.add(new double[] { 14230, 12300, 14240, 15244, 15900, 19200, 22030, 21200, 19500, 15500, 12600, 14000 });
		colors = new int[] { Color.GRAY };
		renderer = abstractDemoChart.buildBarRenderer(colors);
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setXLabelsAlign(Align.LEFT);
		renderer.setYLabelsAlign(Align.LEFT);
		renderer.setPanEnabled(true, false);
		// renderer.setZoomEnabled(false);
		renderer.setZoomRate(1.1f);
		renderer.setBarSpacing(0.5f);

		abstractDemoChart.setChartSettings(renderer, "Monthly sales", "Month", "Units sold", 0.5, 12.5, 0, 24000, Color.GRAY, Color.LTGRAY);

		dataset = abstractDemoChart.buildBarDataset(titles, values);
		chartView = ChartFactory.getBarChartView(context, dataset, renderer, Type.DEFAULT);
	}

	public void setView() {
		dataset = abstractDemoChart.buildBarDataset(titles, values);
		chartView = ChartFactory.getBarChartView(context, dataset, renderer, Type.DEFAULT);
	}

	public void setValues(List<double[]> values) {
		this.values = values;
	}

	public GraphicalView getChartView() {
		return chartView;
	}
}

 2.使用方法:

 

private void addDynamicBarchart(Context context) {
		mDynamicBarChartLayout = (LinearLayout) findViewById(R.id.dynamicbarchart);

		mDynamicBarChart = new DynamicBarChart(context);
		chartView = mDynamicBarChart.getChartView();
		chartView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
		chartView.setBackgroundColor(Color.BLACK);
		mDynamicBarChartLayout.addView(chartView);

	}

 

private void updateChart() {
		updateVaules(); //更新Values
		mDynamicBarChart.setValues(values);
		mDynamicBarChart.setView();

		mDynamicBarChartLayout.removeAllViews();

		chartView = mDynamicBarChart.getChartView();
		chartView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
		chartView.setBackgroundColor(Color.BLACK);
		mDynamicBarChartLayout.addView(chartView);

	}

 

 

 

 

参考:http://blog.csdn.net/lk_blog/article/details/7645509

分享到:
评论

相关推荐

    android 图表引擎AChartEngine

    **Android图表引擎AChartEngine详解** AChartEngine是一款强大的开源库,专为Android平台设计,用于创建各种类型的图表,如柱状图、线形图、饼图等。它提供了丰富的功能,使得开发者能够轻松地在应用程序中集成图表...

    AChartEngine图表库之动态柱状图

    Android图表引擎AChartEngine实现动态柱状图,可以实时传入数据实时刷新图表

    Andorid 图表引擎achartengine 的示例代码及API文档

    Android图表引擎AChartEngine是一个强大的库,用于在Android应用程序中创建各种类型的图表,如线图、柱状图、饼图、散点图等。它提供了一个简单易用的API,使得开发者无需深入了解复杂的图形绘制机制就能实现丰富的...

    achartengine 生成柱状图demo

    `achartengine`是一个开源库,专为Android平台设计,用于快速且轻松地创建各种图表,如柱状图、线图、饼图等。这个“achartengine生成柱状图demo”就是展示如何利用`achartengine`库在Android应用中实现柱状图的实例...

    安卓图表引擎AChartEngine(六)-框架源码结构图.pdf

    AChartEngine是一款开源的Android图表库,它为开发者提供了在Android应用中绘制各种图表的能力。这个库包含了多种图表类型,如线形图、饼图、散点图等,方便开发者在应用中展示数据可视化信息。在本篇文章中,我们将...

    android 绘图引擎 achartengine

    `ACHARTENGINE`是一个用Java编写的库,专为Android平台设计,支持多种类型的图表,包括折线图(LineChart)、饼图(PieChart)、柱状图(BarChart)、散点图(ScatterChart)以及更多其他图表类型。它简化了Android...

    Android制作曲线、柱状图、饼形等图表—使用AChartEngine

    在Android应用开发中,视觉元素的呈现对于数据的展示和用户交互至关重要,特别是各种图表,如曲线图、柱状图和饼形图。AChartEngine是一个强大的开源库,专为Android设计,它允许开发者轻松地在应用程序中创建这些...

    安卓图表引擎AChartEngine源码及示例

    柱状图),pie chart ( 饼图),pie chart ( 饼图),bubble chart (气泡图),doughnut chart (圆环图),range (high-low) bar chart (范围条形图),dial chart / gauge (拨号盘/压力表),combined (any combination of line, ...

    android图表achartengine-1.1.0 Demo

    **Android图表库AChartEngine详解** AChartEngine是一款在Android平台上用于创建各种图表的强大框架,其1.1.0版本提供了丰富的图表类型和高度自定义的功能。这个框架使得开发者能够轻松地在Android应用中集成数据...

    Android学习之AChartEngine折线图

    在Android开发中,数据可视化是十分重要的,AChartEngine是一个流行的开源库,它为开发者提供了在Android应用中绘制各种图表的能力,包括折线图、饼图、柱状图等。这篇博客“Android学习之AChartEngine折线图”将...

    android 绘制各种图表的开源引擎achartengine 7.0

    在"android 绘制各种图表的开源引擎achartengine 7.0"中,我们可以找到版本为0.1.7.0的AChartEngine库。 该库支持多种图表类型,包括但不限于: 1. 折线图(LineChart):用于展示数据随时间的变化趋势。 2. 柱状...

    achartengine绘制动态柱状图

    AChartEngine是一个强大的开源图表库,适用于Android应用开发,它提供了多种类型的图表,包括柱状图、线形图、饼图等。在这个场景中,我们关注的是如何使用AChartEngine来绘制动态柱状图。动态柱状图在数据可视化中...

    Android报表库aChartEngine 横轴为时间曲线图表

    aChartEngine是一个强大的开源库,专门用于在Android平台上创建各种类型的图表,包括折线图、柱状图、饼图等。本文将深入探讨如何利用aChartEngine库创建以时间为横轴的时间曲线图表。 **一、aChartEngine简介** ...

    Android画图之AChartEngine折线图

    这个库提供了多种图表类型,如折线图、柱状图、饼图等。在`AndroidManifest.xml`中添加对应的权限后,可以通过Maven或Gradle将其作为依赖引入项目。例如,如果你使用Gradle,可以在`build.gradle`文件中添加以下依赖...

    android 折线图 (柱状图、饼状图)

    总的来说,Android中的数据可视化可以通过各种图表库实现,AChartEngine作为一个成熟的选择,提供了简单易用的API来创建折线图、柱状图和饼状图。理解并掌握这些基本的图表类型和相关库的使用,将有助于你在Android...

    Android之柱状图

    首先,ACHartEngine是一个开源的图表库,为Android应用提供了丰富的图表类型,包括柱状图、线形图、饼图等。它允许开发者轻松地在应用中集成这些图表,以便用户可以更好地理解和分析数据。 在实现柱状图的过程中,...

    android实现柱状图,饼状图,曲线图

    AChartEngine是一个强大的图形库,为Android应用提供了多种图表类型,如条形图(柱状图)、饼图和折线图(曲线图)。它的使用非常简单,通过几个简单的API调用就能快速构建出专业的图表。 要实现在Android项目中...

    Android图表--achartengine

    AChartEngine is a charting library for Android applications It currently supports the following chart types: line chart area chart scatter chart time chart bar chart pie chart bubble chart doughnut ...

    android 炫酷动画柱状图例子

    本示例聚焦于创建一个"android 炫酷动画柱状图例子",它打破了Android原生图表控件的限制,通过结合HTML5和JavaScript的力量,实现了样式丰富、动态效果强烈的柱状图。 Android自带的图表库,如`MPAndroidChart`或`...

Global site tag (gtag.js) - Google Analytics