JFreeChart does not show the graph at every iteration on thread?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I am getting counter values in one class using a thread and writing JFreeChart in another thread. While executing, it works alternatively, but only shows the graph at the end. It also displays the y axis label value as a Float but the actual retrieval value is int. How can I solve these issues?

        XYDataset Dataset;         TimeSeries Series = new TimeSeries("Random Data");         Second sec = new Second();         ChartPanel CPanel;         Value = Integer.parseInt(MySQLClass.Map_MySql.get(""+MainWindow.SelectedNode+""));          String CounterName = MainWindow.SelectedNode.toString();         Series.add(sec, Value);         Dataset = new TimeSeriesCollection(Series);         System.out.println("Ds="+Dataset);         Chart = ChartFactory.createTimeSeriesChart(CounterName, "Time", "Range", Dataset, true, false, false);         System.out.println("Chart Created");         XYPlot Plot = (XYPlot)Chart.getPlot();         Plot.setBackgroundPaint(Color.LIGHT_GRAY);         Plot.setDomainGridlinePaint(Color.WHITE);         Plot.setRangeGridlinePaint(Color.RED);         Panel1.revalidate();         CPanel = new ChartPanel(Chart);         CPanel.setVisible(true);         Panel1.add(CPanel);         System.out.println("Chart Added");         Panel1.validate();          Thread.sleep((int)MainWindow.Interval_Combo.getSelectedItem() * 1000);         System.gc(); 

This thread for accessing those two class

        while(true)         {             MySQLClass m = new MySQLClass();             Thread t1 = new Thread(m);             t1.start();             t1.join();             Graph g = new Graph();             Thread t2 = new Thread(g);             t2.start();             t2.join();         } 

In MySql class, i hust get the counter name and value and store it in Hashmap called Map_Mysql in followiung manner.

   while(rs.next())    {      Map_MySql.put(rs.getString(1), rs.getString(2));    } 

I dont know what the actual problem, please solve this. Output look like,

MySql Occur
com.mysql.jdbc.JDBC4Connection@2c8ab0
Graph Occur
42913
Ds=org.jfree.data.time.TimeSeriesCollection@c204e809
MySql Occur
com.mysql.jdbc.JDBC4Connection@1930b4b
Graph Occur
44217

At the end show the graph with float value in Y axis for last value with no graphical representation.

回答1:

Don't sleep on the event dispatch thread (EDT). As shown in Concurrency in Swing, use a worker thread to update the dataset on the EDT in process(). A complete example is shown here.



回答2:

For your second question (integer axis labels instead of float), this can be handled by calling the setStandardTickUnits() method on the axis. You can pass any TickUnitSource, but easiest for you is probably NumberAxis.createIntegerTickUnits().



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!