0

I am having two issues with JFreeChart.

I am using a line XYLineChart and updating the data set so that it refreshes the data periodically and adds more items. The issue is that, when I add more data the application window comes straight back in to the foreground so if i am typing in another window that window loses focus.

The second issue i am having is that, every time the graph updates, it resizes itself so i cant resize and maintain it.

Please could somebody help with this issue? Thanks

public static void main (String[] args){
          String val= args[0];
          LineChart chart = new LineChart("Name " + val);

          while(true){
             Main main = new Main();
             XMLData data = getMyData()         
             chart.addToDataSet(data);
             chart.pack();
               RefineryUtilities.centerFrameOnScreen(chart);
               chart.setVisible(true);
               LineChart.incremementxAxisTimePlot(true);
             try {
                Thread.sleep(5000);
             } catch (InterruptedException e) {
                e.printStackTrace();
             }
          }



package com.chart.graph;


import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import com.graph.logfiledata.XMLData;


/**
 * A simple demonstration application showing how to create a line chart using data from an
 * {@link XYDataset}.
 *
 */
public class LineChart extends ApplicationFrame {

    private static int xAxisTimePlot = 5;
    private XYSeriesCollection dataset = new XYSeriesCollection();

    public LineChart(final String title) {

        super(title);
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

        dataset.addSeries(series2);
        dataset.addSeries(series3);
        dataset.addSeries(series4);
        dataset.addSeries(series5);
        dataset.addSeries(series6);
        dataset.addSeries(series7);
        dataset.addSeries(series8);
        dataset.addSeries(series9);
        dataset.addSeries(series10);


    }

    public static void incremementxAxisTimePlot(boolean update){
        if(update){
            xAxisTimePlot+=5;
        }
    }
    XYSeries series2 = new XYSeries("1");
    XYSeries series3 = new XYSeries("2");
    XYSeries series4 = new XYSeries("3");
    XYSeries series5 = new XYSeries("4");
    XYSeries series6 = new XYSeries("5");
    XYSeries series7 = new XYSeries("6");
    XYSeries series8 = new XYSeries("7");
    XYSeries series9 = new XYSeries("8");
    XYSeries series10 = new XYSeries("9");
    public void addToDataSet(XMLData data){

        series2.add(xAxisTimePlot, data.getOne());
        series3.add(xAxisTimePlot, data.getTwo());
        series4.add(xAxisTimePlot, data.getThree());
        ...        

    }

    /**
     * Creates a chart.
     * 
     * @param dataset  the data for the chart.
     * 
     * @return a chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {

        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart("Line Chart","X","Y", dataset, PlotOrientation.VERTICAL,true,true, false);
        chart.setBackgroundPaint(Color.white);

        // get a reference to the plot for further customisation...
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesShapesVisible(1, false);
        plot.setRenderer(renderer);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        return chart;  
    }
}
Biscuit128
  • 5,218
  • 22
  • 89
  • 149

1 Answers1

3
  • while(true){ is endless loop withouth break; that all Objects are created (inside while(true){) on 5 seconds period

  • especially endless loop that creating bunch of a new GUI

meaning code

Main main = new Main();
XMLData data = getMyData()         
chart.addToDataSet(data);
chart.pack();
  • prepare GUI before, then if is there real reason to create loop, but to define boolean variable instread while(canRun){ of while(true){ or break;

  • for why reason is there Thread.sleep(5000);, use Swing Timer in the case that GUI is based on Swing / AWT container,

  • if is container based on Swing / AWT then you have got issue with Concurency in Swing, where all events must be done on EventDispatchTread,

  • your GUI never will be refreshed until (endless) loop ended, because Thread.sleep(5000); caused freeze of filckering of Swing GUI, caused locking EventDispatchTread

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Here's an [example](http://stackoverflow.com/a/13205322/230513) that updates a chart's model from a `SwingWorker` – trashgod Dec 21 '12 at 20:03