有没有人用一个JSplitPane中的每个图而不是一个JPanel中的所有图实现了CombinedXYPlot类型的图?
这样,可以在不影响其他图的情况下修改每个图的尺寸尺寸之一(例如,如果每个图位于分隔窗格中,则可以通过拖动JSplitPane分隔线手动调整每个图的垂直高度)。

示例代码如下:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JSplitPane;

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

public class SplitPaneCharts extends JFrame {

    private static final int dataPoints = 100;

    public SplitPaneCharts() {
        super();
        this.getContentPane().setLayout(new FlowLayout());
        Panel panel1 = new Panel();
        ChartPanel chartPanel1 = createDemoPanel("Chart1");
        panel1.add(chartPanel1);
        Panel panel2 = new Panel();
        ChartPanel chartPanel2 = createDemoPanel("Chart2");
        panel2.add(chartPanel2);
        XYPlot plot1 = chartPanel1.getChart().getXYPlot();
        XYPlot plot2 = chartPanel2.getChart().getXYPlot();
        plot2.setDomainAxis(plot1.getDomainAxis());
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
        this.getContentPane().add(splitPane);

    }

    private ChartPanel createDemoPanel(String title) {
        JFreeChart jfreechart = ChartFactory.createXYLineChart(title, "X", "Y", createSampleData(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.red);
        NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
        domain.setRange(0.00, 1.00);
        domain.setTickUnit(new NumberTickUnit(0.2));
        domain.setVerticalTickLabels(true);
        NumberAxis range = (NumberAxis) xyPlot.getRangeAxis();
        range.setRange(0.0, 1.0);
        range.setTickUnit(new NumberTickUnit(0.2));
        return new ChartPanel(jfreechart);
    }

    private XYDataset createSampleData() {
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("Data");
        Random rand = new Random();
        for (int i = 0; i < dataPoints; i++) {
            double x = rand.nextDouble();
            double y = rand.nextDouble();
            series.add(x, y);
        }
        xySeriesCollection.addSeries(series);
        return xySeriesCollection;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SplitPaneCharts splitPaneCharts = new SplitPaneCharts();
                splitPaneCharts.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                splitPaneCharts.pack();
                splitPaneCharts.setLocationRelativeTo(null);
                splitPaneCharts.setVisible(true);
            }
        });
    }
}


我遇到的几个问题:

1)当窗格的面积小于图的面积时,图表不会调整大小。

2)移动JSplitPane分隔符后,有时会隐藏弹出菜单/在RMB上不显示弹出菜单。

3)理想情况下,我想将X轴放在一个单独的窗格中,并仅在另外两个窗格(即3 x SplitPanes)中显示数据。事实证明,这很难解决如何实现-我查看了一个XYPlot,其中将dataArea设置为细水平线-因此仅绘制了X轴。但是,关于如何最好地实现这一点,我不确定。

最佳答案

要使图表随框架调整大小,请指定忽略图表首选尺寸的布局,例如GridLayout和框架的BorderLayout.CENTER;不要不必要地嵌套面板。

ChartPanel chartPanel1 = createDemoPanel("Chart1");
ChartPanel chartPanel2 = createDemoPanel("Chart2");
XYPlot plot1 = chartPanel1.getChart().getXYPlot();
XYPlot plot2 = chartPanel2.getChart().getXYPlot();
plot2.setDomainAxis(plot1.getDomainAxis());
chartPanel1.setLayout(new GridLayout());
chartPanel2.setLayout(new GridLayout());
JSplitPane splitPane = new JSplitPane(
    JSplitPane.VERTICAL_SPLIT, chartPanel1, chartPanel2);
add(splitPane, BorderLayout.CENTER);

上下文菜单似乎可以与GridLayout一起使用。
您可以尝试使用CombinedDomainXYPlot,如here所示。

09-07 05:49