0

JFreeChart 有两个问题。

我正在使用一条线 XYLineChart 并更新数据集,以便它定期刷新数据并添加更多项目。问题是,当我添加更多数据时,应用程序窗口会直接回到前台,所以如果我在另一个窗口中输入,该窗口就会失去焦点。

我遇到的第二个问题是,每次图表更新时,它都会自行调整大小,因此我无法调整和维护它。

请问有人可以帮忙解决这个问题吗?谢谢

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;  
    }
}
4

1 回答 1

3
  • while(true){是无限循环,没有在 5 秒内创建(内部)break;所有内容Objectswhile(true){

  • 特别是无限循环,创建了一堆新的 GUI

意义代码

Main main = new Main();
XMLData data = getMyData()         
chart.addToDataSet(data);
chart.pack();
  • 之前准备 GUI,然后如果有真正的理由创建循环,但定义布尔变量while(canRun){instreadwhile(true){break;

  • 为什么有原因,在GUI基于的情况下Thread.sleep(5000);使用,Swing TimerSwing / AWT container

  • 如果是基于Swing / AWT的容器,那么您在 Swing 中遇到了 Concurency问题,所有事件都必须在其中完成EventDispatchTread

  • 在(无限)循环结束之前,您的 GUI 永远不会刷新,因为Thread.sleep(5000);导致 Swing GUI 的闪烁冻结,导致锁定EventDispatchTread

于 2012-12-21T19:32:55.383 回答