jfreechart:你如何排列两个情节?

时间:2011-02-07 16:45:38

标签: java user-interface plot jfreechart

我有两个情节。我需要他们排队,因为这两个图有需要比较的数据。因此,当你排列它们时,比较很容易。

这是迄今为止两幅地块的图片。图片有一些红色注释。红色注释显示现在有一个“三角洲”。 “delta”指的是两个图表的错位。

enter image description here

如何使两个图表完全对齐?

到目前为止,我添加了一个“ChartChangeListener”,以便当顶部图更改时,我可以获得必要的信息......并调整底部图。但是我找不到“什么是必要的信息”,以及如何使用它来改变底部图表。

更新:评论者要求提供一些代码。所以IMO就是必要的代码。

public class TmpChartPanel extends JPanel implements ChartChangeListener {

    ...

    public void addComponentsToContainer(IqDataset iqDataset) {
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;

        // Primarily, the main chart
        ChartPanel chartPanel = new ChartPanel(createChart(this.seriesCollection1));
        c.gridy = 0;
        c.gridx = 0;
        this.add(chartPanel, c);

        // Secondarily, the second plot. TODO: link the two together so that the user sees the data aligned.
        chartPanel = new ChartPanel(createHistogramChart(this.data));
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weighty = 0;
        c.gridy = 1;
        c.gridx = 0;
        this.add(chartPanel, c);
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;
    }

    public void chartChanged(ChartChangeEvent event) {
        // NOTE: event is from the PRIMARY or top chart

        /*------ Take the value from the top chart and set the bottom plot ------*/
        // this part works
        XYPlot plot = (XYPlot)event.getChart().getPlot();
        NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
        Range tmp2 = xAxis.getRange();
        ((XYPlot)this.SECONDARY.getPlot()).getDomainAxis().setRange(tmp2);

        /*------ Make it so that the secondary chart plots the same way directly under the primary chart ------*/
        // can't figure out...
    }

    ...

}

1 个答案:

答案 0 :(得分:2)

看起来它可能只是LayoutManager的一个问题。 (没有完整的例子很难说)。尝试使用GridLayout而不是GridBagLayout。

更好的解决方案可能是对两个图使用单个组合图。我假设,因为你希望它们是相同的大小,一个在另一个之上,那么他们可能能够共享一个轴。 (查看JFreeChart演示应用程序的Combined Axis Charts文件夹)。