本文介绍了如何绘制移动和使用JFree图表中的Java运行正弦波图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我米使用JFreeChart的绘制运行或与NetBeans移动正弦波在java中。我写code它,但它并不像移动或运行正弦波曲线图。所以,如果你有任何关于这则建议我的想法。
我的code是低于
进口java.awt.BorderLayout中;
进口java.awt.Color中;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口javax.swing.JButton中;
进口javax.swing.JPanel中;
进口org.jfree.chart.ChartFactory;
进口org.jfree.chart.ChartPanel;
进口org.jfree.chart.JFreeChart;
进口org.jfree.chart.plot.PlotOrientation;
进口org.jfree.chart.plot.XYPlot;
进口org.jfree.data.xy.XYDataset;
进口org.jfree.data.xy.XYSeries;
进口org.jfree.data.xy.XYSeriesCollection;
进口org.jfree.ui.ApplicationFrame;
公共类DrawChart扩展ApplicationFrame实现的ActionListener { 公共XYSeries系列; 公共DrawChart(最后弦乐标题){ 超(职称);
系列=新XYSeries(正弦波,真实,真实);
XYSeriesCollection数据=新XYSeriesCollection(系列);
最终的JFreeChart图表= createChart(数据); 最后ChartPanel chartPanel =新ChartPanel(图)
最后一个JButton按钮=的新的JButton(添加新数据项);
button.setActionCommand(add_data工具);
button.addActionListener(本); 最终的JPanel内容=新JPanel(新的BorderLayout());
content.add(chartPanel);
content.add(按钮,BorderLayout.SOUTH);
chartPanel.set preferredSize(新java.awt.Dimension中(500,270));
setContentPane(内容); } 私人的JFreeChart createChart(最终XYDataset数据集){
JFreeChart的jfreechart的= ChartFactory.createXYLineChart(仙曲线,角度(度),值,数据集,PlotOrientation.VERTICAL,真实的,真实的,真正的);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot =(XYPlot)jfreechart.getPlot();
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
返回jfreechart的;
} 公共无效的actionPerformed(ActionEvent的最终E){
如果(e.getActionCommand()。等于(add_data工具)){
的for(int i = 0; I< 100;我++){
最终双X =(我)/10.0;
最终双Y = Math.sin(X);
this.series.addOrUpdate(X,Y);
}
}
} 公共静态无效的主要(最终字串[] args){ 最后DrawChart演示=新DrawChart(动态数据视图);
demo.pack();
RefineryUtilities.centerFrameOnScreen(演示);
demo.setVisible(真);
}
}
解决方案
此例如,替换 Math.sin()
为 nextGaussian()
产生如下图所示。在1赫兹A javax.swing.Timer中的
百步动画。
私人ChartPanel createPane(){
最后XYSeries系列=新XYSeries(数据);
对(INT I = 0; I&下; random.nextInt(N)+ N / 2;我++){
series.add(ⅰ,Math.sin(ⅰ));
}
XYSeriesCollection数据=新XYSeriesCollection(系列);
新的Timer(1000,新的ActionListener(){ @覆盖
公共无效的actionPerformed(ActionEvent的五){
series.add(series.getItemCount(),Math.sin(series.getItemCount()));
}
})。开始();
...
}
I m using JFreeChart to draw Running or Moving Sine Wave in java with netbeans. I write code for it , but it does not like moving or running sine wave graph. So if you have any idea regarding that then suggest me.My code is below
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);
}
}
解决方案
Starting from this example, substituting Math.sin()
for nextGaussian()
produced the illustration below. A javax.swing.Timer
paces the animation at 1 Hz.
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();
…
}
这篇关于如何绘制移动和使用JFree图表中的Java运行正弦波图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!