Title: Graph not displaying but value change in jfreechart?
Tags: java;jfreechart;swingworker
Question: 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,.
Thanks in advance.
Comment: 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/).

Comment: 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.

Comment: 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.


Here is another answer: 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.
Comment for this answer: 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
Comment for this answer: oh thanks @david gilbert
Comment for this answer: 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.
Comment for this answer: 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.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:01/07/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><jfreechart><swingworker>', 'Title': 'Graph not displaying but value change in jfreechart?', 'ViewCount': 504, 'id': 20966205, 'user_AboutMe': '<p>I am a Fresher. Now only i am learning Java. I hope i will learn it in an effective manner with the help of you people.</p>\n', 'user_AccountId': 3799961.0, 'user_DisplayName': 'A.Mohamed Bilal', 'user_Reputation': 115.0, 'user_Views': 42.0, 'user_id': 3153879.0}}