我无法在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);
}

最佳答案

您缺少第二个渲染器。

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


快照



编辑:

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

关于android - 无法在achartengine中绘制多个系列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17240083/

10-16 09:11