使用Achartengine可以实现多个堆叠条形图吗?

时间:2012-08-08 12:43:36

标签: java android bar-chart achartengine stacked

我尝试将另一组值添加到stacked bar charts with achartengine的演示示例中,但我引入的新值不会显示在图表上。堆叠条限制为两个柱?

public Intent getIntent(Context context) {

    String[] titles = new String[] { "Good", "Defect", "Repair" };
    List<double[]> values = new ArrayList<double[]>();

    values.add(new double[] { 14230, 12300, 1424, 15240, 15900, 19200,
    22030, 21200, 19500, 15500, 12060, 14000 });
    values.add(new double[] { 14230, 12300, 14240, 15244, 15900, 19200,
            22030, 21200, 19500, 15500, 12600, 14000 });
    values.add(new double[] { 5230, 7300, 9240, 10540, 7900, 9200, 12030,
            11200, 9500, 10500, 11600, 13500 });

    int[] colors = new int[] { Color.GREEN, Color.YELLOW, Color.RED };

    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);

    setChartSettings(renderer, "Machine Efficiency Rates",
            "Machine", "NET produce", 0.5, 12.5, 0, 24000, Color.GRAY,
            Color.LTGRAY);

    renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
    renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
    renderer.getSeriesRendererAt(2).setDisplayChartValues(true);
    renderer.setXLabels(12);
    renderer.setYLabels(5);
    renderer.setXLabelsAlign(Align.LEFT);
    renderer.setYLabelsAlign(Align.LEFT);
    renderer.setPanEnabled(true, false);
    renderer.setZoomEnabled(true);
    renderer.setZoomRate(1.1f);
    renderer.setBarSpacing(0.5f);
    return ChartFactory.getBarChartIntent(context,
            buildBarDataset(titles, values), renderer, Type.STACKED);

}

2 个答案:

答案 0 :(得分:4)

AChartEngine显示堆积条形图的方式存在误解。它并没有真正堆叠它们,而是将它们一个显示在另一个上面。这意味着您需要始终先添加较大的值,然后再将较小的值添加到第一个系列,第二个在第一个系列之上,依此类推。

更新:从版本1.2.0开始,有新的HEAP堆积条形图,以您需要的方式显示。

答案 1 :(得分:0)

我找不到一个好的答案。但是,您可以使用getCombinedXYChartIntent 创建多个堆叠条。

我使用CombinedTemperatureChart作为模板并进行了修改以实现此目的。

初始原始代码是

package org.achartengine.chartdemo.demo.chart;

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

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.BubbleChart;
import org.achartengine.chart.CubicLineChart;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.model.XYValueSeries;
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;

/**
 * Combined temperature demo chart.
 */
public class CombinedTemperatureChart extends AbstractDemoChart {
  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  public String getName() {
    return "Combined temperature";
  }

  /**
   * Returns the chart description.
   * 
   * @return the chart description
   */
  public String getDesc() {
    return "The average temperature in 2 Greek islands, water temperature and sun shine hours (combined chart)";
  }

  /**
   * Executes the chart demo.
   * 
   * @param context the context
   * @return the built intent
   */
  public Intent execute(Context context) {
    String[] titles = new String[] { "Crete Air Temperature", "Skiathos 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 });
    }

    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 });
    values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
    int[] colors = new int[] { Color.GREEN, Color.rgb(200, 150, 0) };
    PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND };
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
    //renderer.setPointSize(5.5f);
    int length = renderer.getSeriesRendererCount();
    for (int i = 0; i < length; i++) {
      XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
      r.setDisplayChartValues(true);
      r.setChartValuesTextSize(25);
      //r.setLineWidth(2);
      r.setFillPoints(true);
    }
    setChartSettings(renderer, "Weather data", "Month", "Temperature", -1, 12.5, 0, 40,
        Color.LTGRAY, Color.LTGRAY);

    renderer.setXLabels(12);
    renderer.setYLabels(10);
    renderer.setShowGrid(true);
    renderer.setXLabelsAlign(Align.RIGHT);
    renderer.setYLabelsAlign(Align.RIGHT);
    renderer.setZoomButtonsVisible(true);
    renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
    renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });


    XYValueSeries sunSeries = new XYValueSeries("Sunshine hours");
    sunSeries.add(0.5, 17);
    sunSeries.add(1.5, 18);
    sunSeries.add(2.5, 19);
    sunSeries.add(3.5, 19);
    sunSeries.add(4.5, 20.8);
    sunSeries.add(5.5, 24.9);
    sunSeries.add(6.5, 26);
    sunSeries.add(7.5, 27.8);
    sunSeries.add(8.5, 28.4);
    sunSeries.add(9.5, 25.5);
    sunSeries.add(10.5, 23.5);
    sunSeries.add(11.5, 19.5);

    XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
    lightRenderer.setColor(Color.YELLOW);

    XYSeries waterSeries = new XYSeries("Water Temperature");
    waterSeries.add(0.5, 16);
    waterSeries.add(1.5, 15);
    waterSeries.add(2.5, 16);
    waterSeries.add(3.5, 17);
    waterSeries.add(4.5, 20);
    waterSeries.add(5.5, 23);
    waterSeries.add(6.5, 25);
    waterSeries.add(7.5, 25.5);
    waterSeries.add(8.5, 26.5);
    waterSeries.add(9.5, 24);
    waterSeries.add(10.5, 22);
    waterSeries.add(11.5, 18);

    renderer.setBarSpacing(2);
    renderer.setBarWidth(2);
    XYSeriesRenderer waterRenderer = new XYSeriesRenderer();
    waterRenderer.setColor(Color.argb(250, 0, 210, 250));

    XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
    dataset.addSeries(0, sunSeries);
    dataset.addSeries(1, waterSeries);
    renderer.setBarWidth(0.5f);
    renderer.addSeriesRenderer(lightRenderer);
    renderer.addSeriesRenderer(waterRenderer);

    waterRenderer.setDisplayChartValues(true);
    waterRenderer.setChartValuesTextSize(25);

    lightRenderer.setDisplayChartValues(true);
    lightRenderer.setChartValuesTextSize(25);

    String[] types = new String[] {  BarChart.TYPE, BarChart.TYPE, BarChart.TYPE, BarChart.TYPE };
    Intent intent = ChartFactory.getCombinedXYChartIntent(context, dataset, renderer, types,
        "Weather parameters");
    return intent;
  }

}

我希望有人仍然需要这个,需要一段时间来弄明白。 不幸的是,我没有点粘贴图像。