修改AChartEngine支持多条Y轴曲线图

来源:互联网 发布:ubuntu怎么重装系统 编辑:程序博客网 时间:2024/05/02 04:59

因为项目需要,需要曲线图能够支持多个Y轴,查阅Demo,发现Temprature and Shnshine能够支持双Y轴,研究了以下代码,发现可以支持多个Y轴。代码如下:

/** * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL *   * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *   *      http://www.apache.org/licenses/LICENSE-2.0 *   * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.achartengine.chartdemo.demo.chart;import java.util.ArrayList;import java.util.List;import org.achartengine.ChartFactory;import org.achartengine.chart.PointStyle;import org.achartengine.model.XYMultipleSeriesDataset;import org.achartengine.renderer.XYMultipleSeriesRenderer;import org.achartengine.renderer.XYSeriesRenderer;import android.content.Context;import android.content.Intent;import android.graphics.Color;import android.graphics.Paint.Align;/** * Multiple temperature demo chart. */public class MultipleTemperatureChart extends AbstractDemoChart {/** * Returns the chart name. *  * @return the chart name */public String getName() {return "Temperature and sunshine";}/** * Returns the chart description. *  * @return the chart description */public String getDesc() {return "The average temperature and hours of sunshine in Crete (line chart with multiple Y scales and axis)";}/** * Executes the chart demo. *  * @param context *            the context * @return the built intent */public Intent execute(Context context) {String[] titles = new String[] { "Air temperature" };List<double[]> x = new ArrayList<double[]>();for (int i = 0; i < titles.length; i++) {x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });}int[] colors = new int[] { Color.BLUE, Color.YELLOW ,Color.GREEN,Color.RED};PointStyle[] styles = new PointStyle[] { PointStyle.POINT,PointStyle.POINT, PointStyle.POINT,PointStyle.POINT};XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(4);setRenderer(renderer, colors, styles);int length = renderer.getSeriesRendererCount();for (int i = 0; i < length; i++) {XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);r.setLineWidth(3f);}setChartSettings(renderer, "Average temperature", "Month","", 0.5, 12.5, 0, 32, Color.LTGRAY, Color.LTGRAY);renderer.setXLabels(12);renderer.setYLabels(10);renderer.setShowGrid(true);renderer.setXLabelsAlign(Align.RIGHT);renderer.setYLabelsAlign(Align.RIGHT);renderer.setZoomButtonsVisible(false);renderer.setPanLimits(new double[] { -10, 20, -10, 40 });renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });renderer.setZoomRate(1.05f);renderer.setLabelsColor(Color.WHITE);renderer.setXLabelsColor(Color.GREEN);renderer.setYLabelsColor(0, colors[0]);renderer.setYLabelsColor(1, colors[1]);renderer.setYLabelsColor(2, colors[2]);renderer.setYLabelsColor(3, colors[3]);//renderer.setYTitle("Hours", 1);//renderer.setYTitle("Ho", 2);renderer.setYAxisAlign(Align.LEFT, 0);renderer.setYAxisAlign(Align.RIGHT, 1);renderer.setYAxisAlign(Align.LEFT, 2);renderer.setYAxisAlign(Align.RIGHT,3);renderer.setYLabelsAlign(Align.LEFT, 0);renderer.setYLabelsAlign(Align.RIGHT, 1);renderer.setYLabelsAlign(Align.RIGHT, 2);renderer.setYLabelsAlign(Align.LEFT, 3);List<double[]> values = new ArrayList<double[]>();/************/values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4,26.1, 23.6, 20.3, 17.2, 13.9 });XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);/************/values.clear();values.add(new double[] { 4.3, 4.9, 5.9, 8.8, 10.8, 11.9, 13.6, 12.8,11.4, 9.5, 7.5, 5.5 });addXYSeries(dataset, new String[] { "Sunshine hours" }, x, values, 1);/************/values.clear();values.add(new double[] { 1.3, 2.9, 3.9, 3.8, 3.8, 3.9, 3.6, 3.8,3.4, 3.5, 3.5, 3.5 });addXYSeries(dataset, new String[] { "xxx" }, x, values, 2);/************/values.clear();values.add(new double[] { 4.3, 5.9, 3.9, 3.8, 5.8, 3.9, 3.6, 3.8,3.4, 3.5, 6.5, 8.5 });addXYSeries(dataset, new String[] { "afhk" }, x, values, 3);/******/Intent intent = ChartFactory.getCubicLineChartIntent(context, dataset,renderer, 0.3f, "Average temperature");return intent;}}


这里支持了4条Y轴的曲线,但是Y轴的标刻线除了显示在左右外,没有找到的控制办法。希望有高人能够解决


0 0
原创粉丝点击