我试图根据此视频在图表中显示两行: http://www.youtube.com/watch?v=5DGldVzC-bU
我的代码:
public class LineGraph {
public Intent getIntent(Context context){
int[] x = {1,2,3,4,5,6,7,8,9,10};
int[] y = {31,29,54,94,75,36,47,118,92,10};
TimeSeries series = new TimeSeries("Line1");
for (int i=0;i<x.length;i++){
series.add(x[i],y[i]);
}
int[] x2 = {1,2,3,4,5,6,7,8,9,10};
int[] y2 = {39,145,35,87,2,65,48,49,100,106};
TimeSeries series2 = new TimeSeries("Line2");
for (int i=0;i<x2.length;i++){
series.add(x2[i],y2[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
dataset.addSeries(series2);
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.CYAN);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
renderer.setLineWidth(3);
renderer.setDisplayChartValues(true);
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.BLACK);
renderer2.setPointStyle(PointStyle.CIRCLE);
renderer2.setFillPoints(true);
renderer2.setLineWidth(3);
renderer2.setDisplayChartValues(true);
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
mrenderer.addSeriesRenderer(renderer);
mrenderer.addSeriesRenderer(renderer2);
mrenderer.setChartTitle("title");
mrenderer.setBackgroundColor(Color.WHITE);
mrenderer.setShowGrid(true);
mrenderer.setApplyBackgroundColor(true);
mrenderer.setXTitle("Days");
mrenderer.setYTitle("datas");
mrenderer.setGridColor(Color.GRAY);
// mrenderer.setZoomButtonsVisible(true);
Intent intent = ChartFactory.getLineChartIntent(context, dataset, mrenderer, "Line graph title");
return intent;
}
与视频中的完全相同。但我没有工作,而不是得到两个不同的线,我只得到一个。当第一行(x)的最后数据结束时,它转到第二行(x2)的第一数据,因此第二行从第一行继续。 提前谢谢!
答案 0 :(得分:5)
不应该如下:
series.add(x2[i],y2[i]);
实际上是:
series2.add(x2[i],y2[i]);
注意'2'。
答案 1 :(得分:0)
TimeSeries series = new TimeSeries("Line1");
for (int i=0;i<x.length;i++){
series.add(x[i],y[i]);
}
int[] x2 = {1,2,3,4,5,6,7,8,9,10};
int[] y2 = {39,145,35,87,2,65,48,49,100,106};
TimeSeries series2 = new TimeSeries("Line2");
for (int i=0;i<x2.length;i++){
series2.add(x2[i],y2[i]); // The change is here...
}