0

Friends, in my application i am using swingworker to generate jfreechart alternatively for server counters. for that, values change in X,Y axis but graph not displaying. Is there any problem in my code? please check it friends..

And in "x,y" axis it shows float value but actual retrieval value from hashmap is int. if i am using,

Plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());

means, in Y axis just display the current value without its nearby values. Please solve there two problems friends. My Graph coding is,

   try
    {

        System.out.println("Graph Occur");

        MySQLClass.GraphLock=false;
        Panel1.removeAll();
        XYDataset Dataset;
        TimeSeries Series = new TimeSeries("Random Data");
        Second sec = new Second();
        ChartPanel CPanel;
        if(Operation_Combo.getSelectedItem().toString().equals("MySQL"))
        {
         if(MySQLClass.Map_MySql.get(""+MainWindow.SelectedNode+"").equals(null))
         {
             Value = 0;
         }
         else
         {
             Value = Integer.parseInt(MySQLClass.Map_MySql.get(""+MainWindow.SelectedNode+""));
         }
         System.out.println(Value);
        }
        if(Operation_Combo.getSelectedItem().toString().equals("SQL Server"))
        {
         if(SqlServerClass.Map_SQLServer.get(""+MainWindow.SelectedNode+"").equals(null))
         {
             Value = 0;
         }
         else
         {
             Value = Integer.parseInt(SqlServerClass.Map_SQLServer.get(""+MainWindow.SelectedNode+""));
         }
         System.out.println(Value);
        }
        String CounterName = MainWindow.SelectedNode.toString();
        Series.add(sec, Value);
        Dataset = new TimeSeriesCollection(Series);
        Chart = ChartFactory.createTimeSeriesChart(CounterName, "Time", "Range", Dataset, true, false, false);
        XYPlot Plot = (XYPlot)Chart.getPlot();
       // Plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        CPanel = new ChartPanel(Chart);
        Panel1.revalidate();
        Panel1.add(CPanel);
        Panel1.setBackground(Color.white);
        System.out.println("Chart Added");
        Panel1.validate();

        Thread.sleep(MainWindow.Intervel * 1000);
        System.out.println("Sleep="+(MainWindow.Intervel * 1000));
       CPanel.repaint();
        System.gc();
         if(Operation_Combo.getSelectedItem().toString().equals("MySQL"))
        {
        MySQLClass.SQLLock=true;
        new MySQLClass().execute();
        }
        if(Operation_Combo.getSelectedItem().toString().endsWith("SQL Server"))
        {
        SqlServerClass.SQLServerLock=true;
        new SqlServerClass().execute();
        }

    }

And the result is,.

enter image description here

Thanks in advance.

A.Mohamed Bilal
  • 115
  • 1
  • 13
  • You've posted the same fragments [here](http://stackoverflow.com/q/20884694/230513) and [here](http://stackoverflow.com/q/20944719/230513), with no attempt to follow the approach suggested: _Don't_ sleep on the EDT; _do_ see [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/). – trashgod Jan 07 '14 at 08:58
  • i just got the solution by using that swingworker. the problem is why the chart is not displaying here but the values in X,Y axis change in every iteration. i want to know whats the flaw in my code. – A.Mohamed Bilal Jan 07 '14 at 09:21
  • Whether you update this question or elect to ask a new question, please include an [sscce](http://sscce.org/) that exhibits the problem you describe and the approach taken. – trashgod Jan 07 '14 at 09:56

1 Answers1

1

You have created a dataset with a single data item. If you set the renderer to display shapes at each data point, you'll see a single item in the middle of the chart. There is no line, because you need at least two data points to connect.

Maybe you call this code in some loop and expect to see multiple items ... but in that case you should not create a new dataset and a new chart each time.

David Gilbert
  • 4,427
  • 14
  • 22
  • in my application i just select a node from the tree. For that node, i just retrieve data from hashmap at every iteration and put it in dataset. Actually i have only one value. What should i do to display jfreechart here. @david gilbert – A.Mohamed Bilal Jan 07 '14 at 09:19
  • If you have only one data item then I would create the chart using ChartFactory.createXYBarChart(...). Note that there is an additional parameter to indicate whether you want a DateAxis or NumberAxis for the x-axis. – David Gilbert Jan 07 '14 at 11:41
  • And for your y-axis, because there is only one value it is hard for JFreeChart to know what range to display on the y-axis. So you should either set it manually or call axis.setAutoRangeMinimumSize(2)...use whatever minimum size gives you the result you want. – David Gilbert Jan 07 '14 at 11:42