Denis
Denis

Reputation: 503

JFreeChart Java - How to create it is a lot of series in a cycle

I have a spline graphic that get coordinates from InitP file. I plan to expand a code to make loading of graphics of several files (for each graphic there is the file with a set of coordinates).

On an input the line with names of files. In a cycle in each series for dataset coordinates for the appropriate diagram will be skidded. Difficulty arises in creation of new series, I don't know as to create a new series when reading the new file (I don't know how to create series2, series3 in cycle because I dont know how many series will be). There is a code for one graphic.

        series1 = new XYSeries("Graph");
        try {
                Scanner s = new Scanner(new File("InitP"));
                System.out.println("Open success!");
                float xpol =  0, ypol = 0;
                s.useLocale(Locale.US);
                while (s.hasNext()) {
                    if (s.hasNextFloat()) {
                        xpol = s.nextFloat();
                        ypol = s.nextFloat();
                        System.out.println("x = " + xpol + ", y = " + ypol);
                        series1.add(xpol, ypol);

                    } else {
                        s.next();
                    }
                }
                dataset.addSeries(series1);
            } catch (FileNotFoundException ex) {
                System.out.println("Can't find file!");
            }
        XYSeriesCollection dataset = new XYSeriesCollection();

Upvotes: 2

Views: 991

Answers (2)

trashgod
trashgod

Reputation: 205775

Create your XYSeriesCollection dataset before looping through the files. For each file, create a new XYSeries and use dataset.addSeries() to add the new series to the existing dataset. The listening chart will update itself, as shown here. Because file access is inherently asynchronous, loop through the files in your implementation of the doInBackground() method of a SwingWorker, as shown here.

Addendum: I need to create seriesN, where N is a digit.

As a concrete example, the createDataset() method below adds a series for each file in "user.dir". It uses the file's hashcode() for numbering, but you can increment a counter, etc.

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.File;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/** @see https://stackoverflow.com/a/21953170/230513 */
public class ChartPanelXYTest {

    private static final Random random = new Random();

    private static XYDataset createDataset() {
        XYSeriesCollection dataset = new XYSeriesCollection();
        File[] files = new File(System.getProperty("user.dir")).listFiles();
        for (File f : files) {
            XYSeries series = new XYSeries("f" + f.hashCode());
            for (int i = 0; i < f.getName().length(); i++) {
                series.add(i, random.nextGaussian());
            }
            dataset.addSeries(series);
        }
        return dataset;
    }

    private static JFreeChart createChart(final XYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYLineChart("Test", "Domain",
            "Range", dataset, PlotOrientation.VERTICAL, true, false, false);
        return chart;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                XYDataset dataset = createDataset();
                JFreeChart chart = createChart(dataset);
                ChartPanel chartPanel = new ChartPanel(chart) {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(640, 480);
                    }
                };
                f.add(chartPanel);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}

Upvotes: 3

ErstwhileIII
ErstwhileIII

Reputation: 4843

You might use the following strategy:

  • Outer loop: read a file (or process input arguments) to get the names of each separate file of coordinates
  • For each file name, create a Series and add it to a Vector
  • Process the series from the file name (don't forget to close each file when you have finished)
  • Cycle

Upvotes: 0

Related Questions