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

时间:2017-05-18 02:26:17

标签: java jfreechart

目前,我的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();
        }
    });


}}

1 个答案:

答案 0 :(得分:1)

如上所述hereTimeSeriesCollection可以显示多个系列。只需将两个系列添加到集合中,然后更新计时器ActionListener中的每个系列。在下面的示例中,还会在每个setMaximumItemAge()上调用TimeSeries,如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);
        });
    }
}