开发者

jfreechart: how do you line up two plots?

I have two plots. I need them to line up because the two plots have data that needs to be compared. So when you line them up the comparison is easy.

Here is a picture of the two plots so far. The picture has some red annotations. The red annotations show that right now there is a "delta". The "delta开发者_开发知识库" refers to the misalignment of the two charts.

jfreechart: how do you line up two plots?

How do I make the two charts line up exactly?

So far I added a "ChartChangeListener" so that when the top plot changes I can get the necessary information... and adjust the bottom plot. But I can't find out "what is the necessary information" and how do I use that to change the bottom chart.

UPDATE: Commenter asked for some code. So here is what is IMO the necessary piece of code.

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...
    }

    ...

}


It looks like it might be just an issue with the LayoutManager. (Hard to tell without complete example). Try it with a GridLayout instead of GridBagLayout.

A better solution entirely might be to use a single combined plot for both graphs. I am assuming that since you want them to be the same size, one on top of the other, then they might be able to share an Axis. (Look at the Combined Axis Charts folder of the JFreeChart Demo App).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜