本文介绍了如何在JFreeChart TimeSeries图表上显示降雨指数和温度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的TimeSeries图表每2秒显示一个位置的温度。

现在,如果我想每2秒显示降雨指数和温度,我该如何实现呢? br>
这是我的代码:

  import testWeatherService.TestWeatherTimeLapseService; 

公共类图扩展JFrame实现ActionListener {

私有静态TimeSeries系列;

静态最终int COUNT = 200;

/ ** Constant WINDOW。 * /
public static final int WINDOW = 25;
TestWeatherTimeLapseService getLocData = new TestWeatherTimeLapseService();
公共图(最终字符串标题){
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(图表);


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);
}
私有JFreeChart createChart(最终XYDataset数据集){
final JFreeChart result = ChartFactory.createTimeSeriesChart(
Graph,
Time,
温度,
数据集,
true,
true,
false
);

final XYPlot plot = result.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000); // 60秒
轴= 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);
返回结果;
}
public void actionPerformed(final ActionEvent e){
//
}

public void update(){
String currTemperature = ;
try {
currTemperature = getLocData.getWeatherData(Laverton)[1]; //得到laverton的温度
} catch(例外e){
// TODO自动生成的catch块
e.printStackTrace();
}
浮点数= 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){//当用户关闭图表时停止
demo.dispose();
((计时器)e.getSource())。stop();
}
});
update();
}
});
timer.start();
}
});


}}


解决方案

  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;

/ ** @参见https://stackoverflow.com/q/44037591/230513 * /
公共类ChartPanelTest {

私有静态最终随机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);
定时器计时器=新计时器(2000,新的ActionListener(){
第二个s =新的第二个();
{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 =(秒)s.next()。next();
}
});
timer.start();
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}


Currently, my TimeSeries graph shows the temperature of a location every 2 seconds.
Now, if I want to show the rainfall index along with the temperature every 2 seconds, how can I achieve it?
Here is my code:

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();
        }
    });


}}
解决方案

As noted here, a TimeSeriesCollection can display more than one series. Simply add both series to the collection and update each series in your timer's ActionListener. In the example below, setMaximumItemAge() is also invoked on each TimeSeries, as shown here, to discard older data. To accommodate different units, you can set multiple axes on the same plot, as shown here.

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);
        });
    }
}

这篇关于如何在JFreeChart TimeSeries图表上显示降雨指数和温度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:33