Java Swing - JFreeChart app works slowly

断了今生、忘了曾经 提交于 2019-12-02 02:37:40

问题


I create JFreeChart program that can:

  • move points of splines
  • don't allow to cross black splines (boundary splines)
  • create new splines in real-time (as Grapher)
  • mouse wheel zoom

For adding new series to dataset I use this function:

   public static XYSeriesCollection createSplineDataset(File[] polFiles) {
        dataset = new XYSeriesCollection();
        for (File polFile : polFiles) {
            XYSeries series = new XYSeries(polFile.getName());
            Scanner s = null;
            try {
                s = new Scanner(new File(polFile.getAbsolutePath()));
            }catch (FileNotFoundException ex) {
                System.out.println("Scanner error!");
            }
            s.useLocale(Locale.US);
            while (s.hasNext()) {
                float x = s.nextFloat();
                float y = s.nextFloat();
                series.add(x, y);
            }
            dataset.addSeries(series);
        }
        return dataset;
    }

Main program (there 500+ strings of code, so this is part of it):

public class SplineDemo {
    // declaration of variables
    private static void display(){
        final File[] polFiles = new File("FORPLOT").listFiles();
        polFiles[0] = new File("FORPLOT/InitPolin1");
        polFiles[1] = new File("FORPLOT/InitPolin0");
        for (int i = 2; i <= 36; i++)
            polFiles[i] = new File("FORPLOT/P"+(i-2));
        dataset = JFunc.createSplineDataset(polFiles); // create dataset
        // --------some code-----------
        NumberAxis domain = new NumberAxis("\u03C1");
        NumberAxis range = new NumberAxis("g(\u03C1)");
        SplineRenderer r = new SplineRenderer(20);
        xyplot = new XYPlot(dataset, domain, range, r);
        final XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) xyplot.getRenderer();
        render.setBaseShapesVisible(true);
        final JFreeChart chart = new JFreeChart(xyplot);
        // --------some code-----------            
        chartPanel = new ChartPanel(chart){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };        
        chart.removeLegend();
        chartPanel.addMouseListener(new MouseListener() {
        //------ for creating new splines and to move points of splines ---------
        });

        chartPanel.addMouseWheelListener(new MouseWheelListener() {
        //--------- zoom ------------
        });

        chartPanel.addMouseMotionListener(new MotionListener());

        chartPanel.addChartMouseListener(new ChartMouseListener() {
        //------ for creating new splines and to move points of splines ---------
        });

        chartPanel.setDomainZoomable(false);
        chartPanel.setRangeZoomable(false);
        chartPanel.setPopupMenu(null);
        frame = new JFrame(Title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(chartPanel);

        //------ buttons -------

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.addComponentListener(new ComponentListener() {

            @Override
            public void componentResized(ComponentEvent ce) {
            // ---- to move points when window was resized
            }
        });
    }

    public static class MotionListener implements MouseMotionListener {
    //------ to move points -----------
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                display();
            }
        });
    }
}

So, @trashgod adviced here to modify useBuffer but it didn't help me. So, my problem is that when at there are 1-5 splines at the same time plotted, everything works ideally quickly. When them becomes more than 30 splines as on a screenshot, working is decelerated (for example, points aren't in time behind a mouse in case of moving, zoom works slower, etc.). In what the problem can consist? Here the report from YourKit, but I don't understand it. Slowly the new draw of all diagrams or what works?

I don't understand how 30 diagrams can already brake so. What will be in case of 100+? If it is necessary, I can throw off a full code and project in zip archive


回答1:


The XYSplineRenderer "connects data points with natural cubic splines." Not unexpectedly, its performance scales poorly for thousands points. If the goal is to render smoothed data, it may be advantageous to do the interpolation in the background, as suggested here, and revert to the parent XYLineAndShapeRenderer for rendering only.

In addition, scores of curves, each having hundreds of points, may be difficult to distinguish visually. Consider controlling the visibility of related series, a shown in this example that uses JCheckBox to toggle the display of individual series.



来源:https://stackoverflow.com/questions/27136131/java-swing-jfreechart-app-works-slowly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!