我参考了@Caroilik Android Dynamic Chart关于使用Achartengine绘制图表的答案。我正在尝试使用他提供的代码绘制三条线,但似乎无法弄清楚如何做到这一点。有人可以帮忙吗?感谢。
答案 0 :(得分:1)
您必须再向timeSeries
添加两个dataset
。你必须通过移动一些实例来解决一些竞争条件。
dataset
和renderer
已经有多个系列。
import java.util.Date;
import java.util.Random;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
public class ThreeTimeChart extends Activity {
private static Random random = new Random();
private static TimeSeries[] timeSeries;
private static XYMultipleSeriesDataset dataset;
private static XYMultipleSeriesRenderer renderer;
private static XYSeriesRenderer rendererSeries;
private static GraphicalView view;
private static Thread mThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataset = new XYMultipleSeriesDataset();
renderer = new XYMultipleSeriesRenderer();
renderer.setAxesColor(Color.BLUE);
renderer.setAxisTitleTextSize(16);
renderer.setChartTitle("Time");
renderer.setChartTitleTextSize(15);
renderer.setFitLegend(true);
renderer.setGridColor(Color.LTGRAY);
renderer.setPanEnabled(true, true);
renderer.setPointSize(10);
renderer.setXTitle("Time");
renderer.setYTitle("Number");
renderer.setMargins( new int []{20, 30, 15, 0});
renderer.setZoomButtonsVisible(true);
renderer.setBarSpacing(10);
renderer.setShowGrid(true);
rendererSeries = new XYSeriesRenderer();
rendererSeries.setColor(Color.RED);
rendererSeries.setFillPoints(true);
rendererSeries.setPointStyle(PointStyle.CIRCLE);
renderer.addSeriesRenderer(rendererSeries);
rendererSeries = new XYSeriesRenderer();
rendererSeries.setColor(Color.BLUE);
rendererSeries.setFillPoints(true);
rendererSeries.setPointStyle(PointStyle.CIRCLE);
renderer.addSeriesRenderer(rendererSeries);
rendererSeries = new XYSeriesRenderer();
rendererSeries.setColor(Color.YELLOW);
rendererSeries.setFillPoints(true);
rendererSeries.setPointStyle(PointStyle.CIRCLE);
renderer.addSeriesRenderer(rendererSeries);
timeSeries = new TimeSeries[3];
timeSeries[0] = new TimeSeries("Random 0");
timeSeries[1] = new TimeSeries("Random 1");
timeSeries[2] = new TimeSeries("Random 2");
timeSeries[0].add(new Date(), random.nextInt(10));
timeSeries[1].add(new Date(), random.nextInt(10));
timeSeries[2].add(new Date(), random.nextInt(10));
}
@Override
protected void onStart() {
super.onStart();
dataset.addSeries(timeSeries[0]);
dataset.addSeries(timeSeries[1]);
dataset.addSeries(timeSeries[2]);
view = ChartFactory.getTimeChartView(this, dataset, renderer, "Test");
view.refreshDrawableState();
view.repaint();
setContentView(view);
mThread = new Thread(){
public void run(){
while(true){
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
timeSeries[0].add(new Date(), random.nextInt(10));
timeSeries[1].add(new Date(), random.nextInt(10));
timeSeries[2].add(new Date(), random.nextInt(10));
view.repaint();
}
}
};
mThread.start();
}
}