使用JFreeChart在XYPlot中创建多个系列的问题

时间:2013-06-14 18:32:32

标签: java dataset jfreechart

以下示例代码创建仅包含一个数据系列的图表。其他两个数据系列将不会被创建。

main() - 方法包含数据系列。

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

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.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;

public class MultipleChartFactory {
    private XYPlot plot;

    private ChartPanel chartPanel;

    // datasetIndex is used for multiple datasets
    private int datasetIndex = 0;
    private ArrayList<XYSeriesCollection> seriesArrayList = new ArrayList<XYSeriesCollection>();

    public MultipleChartFactory(String title, String xAxis) {
        super();
        String yAxis = title;

        XYSeriesCollection dataset = createDataset("Series");
        JFreeChart chart = ChartFactory.createXYLineChart("", xAxis, yAxis, dataset, PlotOrientation.VERTICAL, false, false, false);
        chart.setBackgroundPaint(Color.white);

        plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);

        NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2");
        rangeAxis2.setAutoRangeIncludesZero(false);

        JPanel content = new JPanel(new BorderLayout());
        chartPanel = new ChartPanel(chart);
        content.add(chartPanel);
    }

    private XYSeriesCollection createDataset(String name) {
        XYSeries series = new XYSeries(name);

        return new XYSeriesCollection(series);
    }

    public ChartPanel getChart() {
        return chartPanel;
    }

    public void createAdditionalDataset() {
        seriesArrayList.add(createDataset("S" + this.datasetIndex));

        this.datasetIndex++;
        this.plot.setDataset(
                this.datasetIndex, seriesArrayList.get(0));
        this.plot.setRenderer(this.datasetIndex, new StandardXYItemRenderer());
    }

    public XYSeriesCollection getXYSeries(int datasetIndex) {
        return seriesArrayList.get(datasetIndex);
    }

    public int getDatasetCount() {
        return this.plot.getDatasetCount();
    }

    public static void main(String [] args) throws InterruptedException {
        MultipleChartFactory mcf = new MultipleChartFactory("Battery","Time");

        JFrame frame = new JFrame();
        frame.setSize(700, 700);
        JPanel panel = new JPanel();
        panel.add(mcf.getChart());
        frame.add(panel);
        frame.setVisible(true);

        // on closing window
        frame.addWindowListener(new WindowAdapter() {
            public synchronized void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

        mcf.createAdditionalDataset();
        mcf.createAdditionalDataset();
        mcf.createAdditionalDataset();
        mcf.getXYSeries(0).getSeries(0).add(1, 10);
        mcf.getXYSeries(0).getSeries(0).add(2, 100);
        Thread.sleep(3000);
        System.out.println("first");
        mcf.getXYSeries(1).getSeries(0).add(1, 20);
        mcf.getXYSeries(1).getSeries(0).add(2, 200);
        Thread.sleep(3000);
        System.out.println("second");
        mcf.getXYSeries(2).getSeries(0).add(1, 30);
        mcf.getXYSeries(2).getSeries(0).add(2, 300);
    }
}

调试器在mcf.getXYSeries(0).getSeries(0).add(1, 10);之后停止。我真的不明白为什么它会停在那里。

您是否有建议在图表中显示每个数据系列?

1 个答案:

答案 0 :(得分:6)

在访问datasetIndex

时,您需要使用seriesArrayList
public void createAdditionalDataset() {
    seriesArrayList.add(createDataset("S" + this.datasetIndex));
    this.plot.setDataset(this.datasetIndex, seriesArrayList.get(datasetIndex));
    this.plot.setRenderer(this.datasetIndex, new StandardXYItemRenderer());
    this.datasetIndex++;
}

image

注意:

  • 编程到界面,例如List<XYSeriesCollection>

  • 在封闭的pack()上使用Window以获取首选的组件大小。

  • 另请参阅Initial Threads

SSCCE:

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
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.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;

public class MultipleChartFactory {

    private XYPlot plot;
    private ChartPanel chartPanel;
    private int datasetIndex = 0;
    private List<XYSeriesCollection> seriesArrayList = new ArrayList<XYSeriesCollection>();

    public MultipleChartFactory(String title, String xAxis) {
        super();
        String yAxis = title;
        XYSeriesCollection dataset = createDataset("Series");
        JFreeChart chart = ChartFactory.createXYLineChart("", xAxis, yAxis,
            dataset, PlotOrientation.VERTICAL, false, false, false);
        chart.setBackgroundPaint(Color.white);
        plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2");
        rangeAxis2.setAutoRangeIncludesZero(false);
        JPanel content = new JPanel(new BorderLayout());
        chartPanel = new ChartPanel(chart);
        content.add(chartPanel);
    }

    private XYSeriesCollection createDataset(String name) {
        XYSeries series = new XYSeries(name);
        return new XYSeriesCollection(series);
    }

    public ChartPanel getChart() {
        return chartPanel;
    }

    public void createAdditionalDataset() {
        seriesArrayList.add(createDataset("S" + this.datasetIndex));
        this.plot.setDataset(this.datasetIndex, seriesArrayList.get(datasetIndex));
        this.plot.setRenderer(this.datasetIndex, new StandardXYItemRenderer());
        this.datasetIndex++;
    }

    public XYSeriesCollection getXYSeries(int datasetIndex) {
        return seriesArrayList.get(datasetIndex);
    }

    public int getDatasetCount() {
        return this.plot.getDatasetCount();
    }

    public static void main(String[] args) throws InterruptedException {
        MultipleChartFactory mcf = new MultipleChartFactory("Battery", "Time");
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.add(mcf.getChart());
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        mcf.createAdditionalDataset();
        mcf.createAdditionalDataset();
        mcf.createAdditionalDataset();
        mcf.getXYSeries(0).getSeries(0).add(1, 10);
        mcf.getXYSeries(0).getSeries(0).add(2, 100);
        System.out.println("first");
        mcf.getXYSeries(1).getSeries(0).add(1, 20);
        mcf.getXYSeries(1).getSeries(0).add(2, 200);
        System.out.println("second");
        mcf.getXYSeries(2).getSeries(0).add(1, 30);
        mcf.getXYSeries(2).getSeries(0).add(2, 300);
    }
}