使用AChartEngine在图形中创建多重加载线

时间:2013-10-10 13:31:05

标签: android colors charts dataset achartengine

如何使用AChartEngine创建包含多于1种颜色的线条的图形。 这是我尝试执行此操作的方法,遵循示例中的SalesBarChart:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_herd_overview);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    TextView herdoverview = (TextView) findViewById(R.id.herdoverview);
    EditText edit_herdinfo = (EditText) findViewById(R.id.edit_herdinfo);
    Button button = (Button) findViewById(R.id.confirmherdinfo);
    button.setVisibility(View.GONE);
    chartLayout = findViewById(R.id.chartHorizontalEvolution);
    chartLayout.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            LogService.log(TAG, "It has entered on Click");
            Intent myIntent = null;
            myIntent = new Intent(HerdOverview.this, HerdAnalysis.class);
            startActivity(myIntent);
        }
    });
    createGraph();

}


protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
    LogService.log(TAG, "titles.size : " + titles.length);
    LogService.log(TAG, "values.size : " + values.size());
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    int length = titles.length;
    for (int i = 0; i < length; i++) {
        LogService.log(TAG, "I : " + i + " | titles: " + titles[i] + " | values: " + values.get(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]);
            LogService.log(TAG, "K : " + k + " | values: " + v[k]);
        }
        dataset.addSeries(series.toXYSeries());
    }
    return dataset;
}

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);
}

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;
}

现在,每当我做一个新的dataset.addSeries时,它会添加第二行,第二行,第一行,第一行,我怎样才能使每个标题直接将行放在先前放置的行上?

1 个答案:

答案 0 :(得分:1)

我在创建ChartFactort.getCarChartView时改变了,Stacked的类型 从:

mChartViewCellLevel = ChartFactory.getBarChartView(this, buildBarDataset(titles, values), renderer, BarChart.Type.DEFAULT);

要:

mChartViewCellLevel = ChartFactory.getBarChartView(this, buildBarDataset(titles, values), renderer, BarChart.Type.STACKED);