无法在achartengine中绘制多个系列

时间:2013-06-21 16:29:07

标签: android achartengine

我无法在achartengine中绘制多个系列。如果我添加第二个系列 数据集,然后它只是不起作用,它实际上崩溃了。只有一个系列添加到数据集中,代码工作正常。

调试显示,在执行此操作时它会在此处崩溃:ChartFactory.getLineChartView(context,dataset,seriesRenderer);

以下是代码段,非常感谢任何帮助。

public class LineGraph {

public View getView(Context context) {

    int x[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    int y[] = {30,34,25,45,5,30,20,25,29,30,10,20,30,40,50,60,70,60,50,40};     

    TimeSeries series = new TimeSeries("Scores");
    TimeSeries lineSeries = new TimeSeries("Line");

    for(int i=0;i<x.length;i++) {
        series.add(x[i], y[i]);
        series.addAnnotation(i + "", x[i], y[i]+5);

        lineSeries.add(x[i], y[i]+10);
    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series);
    dataset.addSeries(lineSeries);

    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setPointStyle(PointStyle.CIRCLE);
    renderer.setFillPoints(true);
    renderer.setColor(Color.WHITE);
    renderer.setLineWidth(2f);

    XYMultipleSeriesRenderer seriesRenderer = new XYMultipleSeriesRenderer();
    seriesRenderer.addSeriesRenderer(renderer);
    seriesRenderer.setShowAxes(false); // show both axes
    seriesRenderer.setShowLegend(false);
    seriesRenderer.setShowLabels(false);
    seriesRenderer.setZoomButtonsVisible(false); // bye bye zoom  

    //seriesRenderer.setMargins(new int[] {1, 1, 1, 10});

    seriesRenderer.setXAxisMin(0);
    seriesRenderer.setXAxisMax(10);

    seriesRenderer.setTextTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Lifestyle Rounded M54.ttf"));
    seriesRenderer.setPointSize(10f);
    seriesRenderer.setPanEnabled(true, false);
    seriesRenderer.setGridColor(Color.GRAY);


    View view = ChartFactory.getLineChartView(context, dataset, seriesRenderer);

    return view;
}

}

以下是我如何使用它:

public class PerformanceActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_performance);
    Utils.overrideFonts(this, findViewById(android.R.id.content));

    LineGraph lineGraph = new LineGraph();
    View graphView = lineGraph.getView(getApplicationContext());

    RelativeLayout graphHolder = (RelativeLayout)findViewById(R.id.rl_graph);
    graphHolder.addView(graphView);     
}

2 个答案:

答案 0 :(得分:6)

您缺少第二个渲染器。

public class LineGraph {

    public Intent getIntent(Context context)
    {

    int x[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    int y[] = {30,34,25,45,5,30,20,25,29,30,10,20,30,40,50,60,70,60,50,40};     

    TimeSeries series = new TimeSeries("Scores");
    TimeSeries lineSeries = new TimeSeries("Line");

    for(int i=0;i<x.length;i++) {
        series.add(x[i], y[i]);
        //series.addAnnotation(i + "", x[i], y[i]+5);

        lineSeries.add(x[i], y[i]+10);
    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series);
    dataset.addSeries(lineSeries);

    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setPointStyle(PointStyle.CIRCLE);
    renderer.setFillPoints(true);
    renderer.setColor(Color.BLACK);
    renderer.setLineWidth(2f);

    XYSeriesRenderer renderer2= new XYSeriesRenderer();//renderer for 2
    // missing renderer 2    
    renderer2.setColor(Color.GREEN);
    renderer2.setFillPoints(true);
    //renderer2.setChartValuesSpacing(1f);
    renderer.setLineWidth(2f);

    XYMultipleSeriesRenderer seriesRenderer = new XYMultipleSeriesRenderer();
    seriesRenderer.addSeriesRenderer(renderer);
    seriesRenderer.addSeriesRenderer(renderer2); 
    seriesRenderer.setShowAxes(false); // show both axes
    seriesRenderer.setShowLegend(false);
    seriesRenderer.setShowLabels(false);
    seriesRenderer.setZoomButtonsVisible(false); 
    Intent  intent= ChartFactory.getLineChartIntent(context, dataset, seriesRenderer);
    return intent;
    }
}
快照

enter image description here

编辑:

XYMultipleSeriesRenderer seriesRenderer = new XYMultipleSeriesRenderer();
seriesRenderer.addSeriesRenderer(renderer);
seriesRenderer.addSeriesRenderer(renderer2); 
seriesRenderer.setChartTitle("Line Graph");
seriesRenderer.setXTitle("X Values");
seriesRenderer.setYTitle("Y values");
seriesRenderer.setBarSpacing(10);
seriesRenderer.setZoomEnabled(true); 

enter image description here

答案 1 :(得分:2)

您需要为每个系列添加一个系列渲染器,您只添加了一个渲染器。添加第二个你应该很高兴。