0
基於此example,我想每10秒更新一次我的時間系列圖表。但是,如果我使用擺動Timer
使圖表每10秒更新一次,則在圖表上顯示,即使時間流逝了10秒,它也僅在1秒內更新自身。這裏是我的變化,基本上抓住了從一個位置的最新溫度從在線數據庫每隔10秒(試圖)繪製它的圖形:如何每10秒更新一次JFreeChart TimeSeries圖表?
/** @see https://stackoverflow.com/questions/5048852 */
public class l extends ApplicationFrame {
TestWeatherTimeLapseService getTemp = new TestWeatherTimeLapseService(); //gets temperature
private static final String TITLE = "Dynamic Series";
private static final int COUNT = 1 ;
private static final int DELAY = 10000;
private Timer timer;
public l(final String title) {
super(title);
final DynamicTimeSeriesCollection dataset =
new DynamicTimeSeriesCollection(1,60, new Second());
Date date = new Date();
dataset.setTimeBase(new Second(date));
dataset.addSeries(gaussianData(), 0, "Gaussian data");
JFreeChart chart = createChart(dataset);
this.add(new ChartPanel(chart), BorderLayout.CENTER);
timer = new Timer(DELAY, new ActionListener() {
float[] newData = new float[1];
@Override
public void actionPerformed(ActionEvent e) {
newData[0] = randomValue();
dataset.advanceTime();
dataset.appendData(newData);
}
});
}
private float randomValue() {
String currTemperature="";
try {
currTemperature=getTemp.getWeatherData("Laverton")[1]; //get temperature of laverton
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
float number = Float.parseFloat(currTemperature);
number=(float) (number-273.15);
return number;
}
private float[] gaussianData() {
float[] a = new float[COUNT];
a[0] = randomValue();
return a;
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
TITLE, "time", "milliVolts", dataset, true, true, false);
final XYPlot plot = result.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
ValueAxis range = plot.getRangeAxis();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setTickUnit(new DateTickUnit(DateTickUnitType.SECOND, 5));
axis.setDateFormatOverride(new SimpleDateFormat("mm:ss"));
range.setRange(-10,50);
return result;
}
public void start() {
timer.start();
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
l demo = new l(TITLE);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
// Run some code;
demo.start();
}
});
}}
與你的問題相關的最重要的方法似乎是advanceTime(),但你不顯示它。請提供一個[SSCCE](http://www.sscce.org) – Michael
@Michael advaceTime()似乎是內置的JFREE圖表方法 – fredjohnson
@Michael:我同意advaceTime()是必不可少的,但是' ActionListener'調用它。 – trashgod