[英]How generate dynamic graph from massive Java
I have a job in the construction of the dynamic graphics in real time, for example, I found a JFreeChart link below will bring, but I can't figure out how can I use this example, can tell where to place the array with data and then for example to make the derivation of the odds-cycle delay of 3 seeundy and build a graph using data from the array. 我有实时构建动态图形的工作,例如,我发现下面将提供一个JFreeChart链接,但是我不知道如何使用此示例,可以告诉将数据放置在哪里然后例如推导3的奇数周期延迟,并使用数组中的数据构建图。
http://www.java2s.com/Code/Java/Chart/JFreeChartDynamicDataDemo.htm http://www.java2s.com/Code/Java/Chart/JFreeChartDynamicDataDemo.htm
In particular, Where must I write cycle for and with what to see the value from massive on the graph? 特别是,我必须在何处编写周期,并从图中看到大量的值?
The MVC pattern lies at the core of dynamic charts in JFreeChart— if you update the model , the listening view will update itself accordingly. MVC模式是JFreeChart中动态图表的核心-如果更新模型 ,则侦听视图也会相应地更新。 In the example cited, a Swing
Timer
evokes the implementation of ActionListener
to periodically add()
a new value to the TimeSeries
. 在引用的示例中,Swing
Timer
唤起了ActionListener
的实现,以便定期向TimeSeries
add()
新值。 Your code would do likewise. 您的代码也将这样做。 A similar approach is taken here with a
DynamicTimeSeriesCollection
. 这里对
DynamicTimeSeriesCollection
采取了类似的方法。 To process data in the background, use a SwingWoker
, illustrated here . 在后台处理数据,利用
SwingWoker
,说明这里 。
JFreeChart implements org.jfree.ui.Drawable so you can create your custom JPanel, override public void paint(Graphics g) and in this method you can create JFreeChart, then you can call method draw of JfreeChart object with arg Graphic. JFreeChart实现org.jfree.ui.Drawable,因此您可以创建自定义的JPanel,覆盖公共无效涂料(Graphics g),并在此方法中可以创建JFreeChart,然后可以使用arg Graphic调用JfreeChart对象的方法draw。
When data will change repaint this JPanel 当数据将改变重绘此JPanel
below is example as you wish: 以下是您希望的示例:
/** Your custom JPanel */
public class MyPanel extends JPanel{
Random random =new Random(System.currentTimeMillis()); //this is only to simulate change data
public MyPanel(){
//simulation change data
new Thread(){
public void run(){
while(true){
try {
Thread.sleep(3000l);//in every 3 sec refresh
Thread.yield(); // release processor
repaint(); //repaint panel with new data
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}.start();
}
public void paint(Graphics g){
//paint panel
super.paint(g);
// create chart
JFreeChart lineChart = ChartFactory.createLineChart(
"My Title",
"Years","Number of Schools",
createDataset(),
PlotOrientation.VERTICAL,
true,true,false);
//draw chart on panel
lineChart.draw((Graphics2D) g, this.getVisibleRect());
}
/** create data for chart */
private DefaultCategoryDataset createDataset( )
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
dataset.addValue( random.nextInt(100) , "schools" , "1970" );
dataset.addValue( random.nextInt(100) , "schools" , "1980" );
dataset.addValue( random.nextInt(100) , "schools" , "1990" );
dataset.addValue( random.nextInt(100) , "schools" , "2000" );
dataset.addValue( random.nextInt(100) , "schools" , "2010" );
dataset.addValue( random.nextInt(100) , "schools" , "2014" );
return dataset;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.