gpt4 book ai didi

java - jfreechart JSplitPane 中的每个图

转载 作者:行者123 更新时间:2023-11-30 03:53:56 25 4
gpt4 key购买 nike

是否有人实现了 JointXYPlot 类型的绘图,其中每个绘图都在 JSplitPane 中,而不是所有绘图都在一个 JPanel 中?这样,可以修改每个图的尺寸之一,而不会影响其他图(例如,如果每个图位于分割 Pane 中,则可以通过拖动 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) 当 Pane 的面积小于绘图时,图表的大小不会调整。

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

3) 理想情况下,我希望 X 轴位于其自己的单独 Pane 中,并仅在另外两个 Pane (即 3 x SplitPanes)中显示数据。事实证明,很难弄清楚如何实现 - 我研究了显示 XYPlot,其中我将 dataArea 设置为细水平线 - 因此只绘制 X 轴。但至于如何最好地实现这一点,我不确定。

最佳答案

  1. 要使图表随框架调整大小,请指定忽略图表首选大小的布局,例如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);
  2. 上下文菜单似乎可以与 GridLayout 配合使用。

  3. 您可以尝试使用 CombinedDomainXYPlot,如图 here 所示。 .

关于java - jfreechart JSplitPane 中的每个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com