gpt4 book ai didi

java - 如何在 JFreeChart TimeSeries 图表上显示降雨指数和温度?

转载 作者:行者123 更新时间:2023-12-02 13:09:02 29 4
gpt4 key购买 nike

目前,我的 TimeSeries 图表每 2 秒显示一个位置的温度。
现在,如果我想每 2 秒显示一次降雨指数和温度,我该如何实现?
这是我的代码:

import testWeatherService.TestWeatherTimeLapseService;

public class graph extends JFrame implements ActionListener {

private static TimeSeries series;

static final int COUNT = 200;

/** The Constant WINDOW. */
public static final int WINDOW = 25;
TestWeatherTimeLapseService getLocData = new TestWeatherTimeLapseService();
public graph(final String title) {
super(title);
this.series = new TimeSeries("Random Data", Millisecond.class);
this.series2 = new TimeSeries("Random Data", Millisecond.class);

final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
final JFreeChart chart = createChart(dataset);

final ChartPanel chartPanel = new ChartPanel(chart);


final JPanel content = new JPanel(new BorderLayout());

add(chartPanel, BorderLayout.SOUTH);
content.add(chartPanel);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 600));
setContentPane(content);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private JFreeChart createChart(final XYDataset dataset) {
final JFreeChart result = ChartFactory.createTimeSeriesChart(
"Graph",
"Time",
"Temperature",
dataset,
true,
true,
false
);

final XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000); // 60 seconds
axis = plot.getRangeAxis();
axis.setRange(-10.0, 50.0);

XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
renderer.setSeriesPaint( 0 , Color.RED );
renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
renderer.setSeriesPaint( 1 , Color.RED );
renderer.setSeriesStroke( 1 , new BasicStroke( 4.0f ) );
plot.setRenderer( renderer );
return result;
}
public void actionPerformed(final ActionEvent e) {
//
}

public void update(){
String currTemperature="";
try {
currTemperature=getLocData.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);
final Millisecond now = new Millisecond();
System.out.println("Now = " + now.toString());
this.series.add(new Millisecond(), number);


}
public void doIt(){
final graph demo = new graph("Graph");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
Timer timer;
timer = new Timer(2000, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
demo.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) { //stop when user closes chart
demo.dispose();
((Timer)e.getSource()).stop();
}
});
update();
}
});
timer.start();
}
});


}}

最佳答案

如上所述hereTimeSeriesCollection 可以显示多个系列。只需将两个系列添加到集合中并更新计时器的 ActionListener 中的每个系列即可。在下面的示例中,还会在每个 TimeSeries 上调用 setMaximumItemAge(),如 here 所示。 ,丢弃旧数据。为了适应不同的单位,您可以在同一个图上设置多个轴,如图 here .

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
import org.jfree.chart.*;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/** @See https://stackoverflow.com/q/44037591/230513 */
public class ChartPanelTest {

private static final Random R = new Random();
private static final int AGE = 60;

public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries series1 = new TimeSeries("T1");
series1.setMaximumItemAge(AGE);
dataset.addSeries(series1);
TimeSeries series2 = new TimeSeries("T2");
series2.setMaximumItemAge(AGE);
dataset.addSeries(series2);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Test", "Time", "Value", dataset, false, false, false);
ChartPanel chartPanel = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
};
f.add(chartPanel);
Timer timer = new Timer(2000, new ActionListener() {
Second s = new Second();
{ for (int i = 0; i < AGE; i++) actionPerformed(null); }
@Override
public void actionPerformed(ActionEvent e) {
series1.add(s, R.nextGaussian());
series2.add(s, R.nextGaussian());
s = (Second) s.next().next();
}
});
timer.start();
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

关于java - 如何在 JFreeChart TimeSeries 图表上显示降雨指数和温度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44037591/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com