aChartEngine制作双y轴(barChart+TimeChart)图表(x轴为日期/时间)

在做一个安卓项目时需要做一个x轴为时间,两个y轴,一个用折线表示的体重,一个用柱状图表示的运动量的图。找到了achart这个开源的引擎,找了一圈,没发现做类似图的教程。只好去看官方的文档,下图是引擎中图表类之间的关系图:


x坐标为时间(日期)的图表是TimeChart,他继承自折线图(LineChart),而要制作折线-柱形复合图却需要用combinedXYChart,而CombinedXyChart没有的对横坐标时间的支持。看了下源码,决定模仿TimeChart,自己扩展一个CombinedTimeChart。

先写一个支持双y轴的数据类:

public class MyTimeSeries extends XYSeries {
	/**
	 * Builds a new date / time series.
	 * 
	 * @param title
	 *            the series title
	 */
	public MyTimeSeries(String title) {
		super(title);
	}
	
	public MyTimeSeries(String title, int scaleNumber) {
		super(title, scaleNumber);//其实相比原来的TimeSeries只是加了这个构造函数
	}
	/**
	 * Adds a new value to the series.
	 * 
	 * @param x
	 *            the date / time value for the X axis
	 * @param y
	 *            the value for the Y axis
	 */
	public synchronized void add(Date x, double y) {
		super.add(x.getTime(), y);
	}

	protected double getPadding(double x) {
		return 1;
	}
}
然后写一个CombineTimeChart继承 combinedXYChart

public class CombinedTimeChart extends CombinedXYChart {
	 /** The constant to identify this chart type. */
	  public static final String TYPE = "Time";
	  /** The number of milliseconds in a day. */
	  public static final long DAY = 24 * 60 * 60 * 1000;
	  /** The date format pattern to be used in formatting the X axis labels. */
	  private String mDateFormat;
	  /** The starting point for labels. */
	  private Double mStartPoint;


	  /**
	   * Builds a new time chart instance.
	   * 
	   * @param dataset the multiple series dataset
	   * @param renderer the multiple series renderer
	   */
	  public CombinedTimeChart(XYMultipleSeriesDataset dataset, XYMultipleSeriesRenderer renderer,
		      String[] types) {
	    super(dataset, renderer, types);
	  }

	  /**
	   * Returns the date format pattern to be used for formatting the X axis
	   * labels.
	   * 
	   * @return the date format pattern for the X axis labels
	   */
	  public String getDateFormat() {
	    return mDateFormat;
	  }

	  /**
	   * Sets the date format pattern to be used for fo
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值