0

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.

A.Mohamed Bilal
  • 115
  • 1
  • 13

2 Answers2

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • SwingUtilities.invokeLater(new MySQLClass()); SwingUtilities.invokeLater(new Graph()); I am calling those two threads like this. It will also work alternatively but produce result at the end. The Panel is not visible from the starting. please help me. I am stucking here. Is there any fault in my code??? – A.Mohamed Bilal Jan 03 '14 at 13:50
  • Showing results at the end a classic symptom of blocking the EDT with `sleep()`. I strongly recommend a `SwingWorker` to run the query. Does the example cited not work for you? – trashgod Jan 03 '14 at 16:09
1

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().

David Gilbert
  • 4,427
  • 14
  • 22
  • NumberAxis NAxis = (NumberAxis)Plot.getRangeAxis(); NAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); if i use this, the graph shows one current value in Y axis not any nearby values.... – A.Mohamed Bilal Jan 06 '14 at 10:55