如何在java中使用JFree图表绘制移动和运行正弦波形图?

Jay*_*Jay 3 java animation swing awt jfreechart

我正在使用JFreeChart在java中使用netbeans绘制Running或Moving Sine Wave.我为它编写代码,但它不喜欢移动或运行正弦波图.所以,如果您对此有任何想法,那么建议我.我的代码如下

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;


public class DrawChart extends ApplicationFrame implements ActionListener {

    public XYSeries series;

    public DrawChart(final String title) {

        super(title);
        series = new XYSeries("Sine", true, true);
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        final JFreeChart chart = createChart(dataset);

        final ChartPanel chartPanel = new ChartPanel(chart);
        final JButton button = new JButton("Add New Data Item");
        button.setActionCommand("ADD_DATA");
        button.addActionListener(this);

        final JPanel content = new JPanel(new BorderLayout());
        content.add(chartPanel);
        content.add(button, BorderLayout.SOUTH);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(content);

    }

    private JFreeChart createChart(final XYDataset dataset) {
        JFreeChart jfreechart = ChartFactory.createXYLineChart("Sin Curve", "Angle (Deg)", "Value", dataset, PlotOrientation.VERTICAL, true, true, true);
        jfreechart.setBackgroundPaint(Color.white);
        XYPlot xyplot = (XYPlot) jfreechart.getPlot();
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setDomainGridlinePaint(Color.white);
        xyplot.setRangeGridlinePaint(Color.white);
        return jfreechart;
    }

    public void actionPerformed(final ActionEvent e) {
        if (e.getActionCommand().equals("ADD_DATA")) {
            for (int i = 0; i < 100; i++) {
                final double x = (i)/10.0 ;
                final double y = Math.sin(x);
                this.series.addOrUpdate(x, y);
            }
        }
    }

    public static void main(final String[] args) {

        final DrawChart demo = new DrawChart("Dynamic Data view");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

tra*_*god 5

从该起始示例,替换Math.sin()nextGaussian()产生的下图.甲javax.swing.Timer踱步动画在1Hz.

图片

private ChartPanel createPane() {
    final XYSeries series = new XYSeries("Data");
    for (int i = 0; i < random.nextInt(N) + N / 2; i++) {
        series.add(i, Math.sin(i));
    }
    XYSeriesCollection dataset = new XYSeriesCollection(series);
    new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            series.add(series.getItemCount(), Math.sin(series.getItemCount()));
        }
    }).start();
    …
}
Run Code Online (Sandbox Code Playgroud)