JFreeChart的。组合图表bug里面的子图的高度?

时间:2013-03-26 09:45:28

标签: java jfreechart

我正在使用jFreeChart来显示这种情节: chart example

嗯,问题是当程序在高度> 800px的显示器上运行时,暗阴影和45虚线未正确绘制。

我发现问题是由以下错误值返回引起的:

chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0).getDataArea()

dataArea不正确,高度错误,因此阴影不会在预期的位置绘制。但这只有在将窗口大小调整到大约800px以上时才会发生。

我设法用一个小的SSCCE(需要jcommon和jfreechart)重现错误:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;

public class Window extends JFrame implements ComponentListener {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private XYPlot subplotTop;
    private XYPlot subplotBottom;
    private CombinedDomainXYPlot plot;
    private JFreeChart chart;
    private ChartPanel chartPanel;
    private JLabel label;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Window frame = new Window();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Window() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 700, 500);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel example = new JPanel();
        label = new JLabel("Resize this window (do not use the \"maximize\" button");
        example.add( label );
        contentPane.add( example, BorderLayout.NORTH );

        createTopChart();
        createBottomChart();
        createCombinedChart();
        chartPanel = new ChartPanel(chart);
        this.add(chartPanel, BorderLayout.CENTER);

        //window resize listener
        this.addComponentListener(this);
    }

    private void createTopChart() {
        subplotTop = new XYPlot();
        subplotTop.setDomainPannable(true);
        subplotTop.setBackgroundPaint(Color.lightGray);
        subplotTop.setDomainGridlinePaint(Color.white);
        subplotTop.setRangeGridlinePaint(Color.white);

        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        subplotTop.setRenderer(renderer);

        NumberAxis range = new NumberAxis("dBA");
        range.setRange(30, 120);
        range.setAutoRange(false);
        subplotTop.setRangeAxis(range);

        TimeSeriesCollection dataset_L = randomDataset(30,120);
        subplotTop.setDataset(dataset_L);
    }

    private void createBottomChart(){
        subplotBottom = new XYPlot();
        subplotBottom.setDomainPannable(true);
        subplotBottom.setBackgroundPaint(Color.lightGray);
        subplotBottom.setDomainGridlinePaint(Color.white);
        subplotBottom.setRangeGridlinePaint(Color.white);

        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
        subplotBottom.setRenderer(renderer);

        NumberAxis range = new NumberAxis("%");
        range.setRange(-10, 100);
        range.setAutoRange(false);
        subplotBottom.setRangeAxis(range);

        TimeSeriesCollection dataset_P = randomDataset(0,100);
        subplotBottom.setDataset(dataset_P);
    }

    private void createCombinedChart() {    
        plot = new CombinedDomainXYPlot();
        plot.setGap(30);
        plot.add(subplotTop, 4);
        plot.add(subplotBottom, 1);
        plot.setOrientation(PlotOrientation.VERTICAL);

        chart = new JFreeChart("Title", new Font("Arial", Font.BOLD, 18), plot, true);
        chart.getLegend().setVisible(false);
        chart.getLegend().setPosition(RectangleEdge.RIGHT);
        chart.getTitle().setMargin(new RectangleInsets(15,5,15,5));
        chart.getTitle().setPaint(Color.BLACK);
    }

    private TimeSeriesCollection randomDataset(float min, float max) {
        return null;
    }

    private void showAreas() {
        Rectangle2D screenDataAreaChartPanel = this.chartPanel.getScreenDataArea();
        System.out.println("screenDataAreaChartPanel: "+screenDataAreaChartPanel);

        Rectangle2D plotArea = this.chartPanel.getChartRenderingInfo().getPlotInfo().getPlotArea();
        System.out.println("getPlotArea: "+plotArea.toString());

        Rectangle2D dataAreaPlot = this.chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
        System.out.println("getDataArea: "+dataAreaPlot.toString());

        Rectangle2D dataAreaSubplotTop = this.chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0).getDataArea();
        Rectangle2D dataAreaSubplotBottom = this.chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(1).getDataArea();
        System.out.println("dataAreaSubplotTop: "+dataAreaSubplotTop.toString());
        System.out.println("dataAreaSubplotBottom: "+dataAreaSubplotBottom.toString());

        System.out.println();

        label.setText("ScreenDataArea.height="+Math.round(screenDataAreaChartPanel.getHeight())+
                ". SubPlotTop.heigth="+Math.round(dataAreaSubplotTop.getHeight())+
                ". SubPlotBottom.heigth="+Math.round(dataAreaSubplotBottom.getHeight()));
        repaint();
    }

    @Override
    public void componentHidden(ComponentEvent e) {}

    @Override
    public void componentMoved(ComponentEvent e) { }

    @Override
    public void componentResized(ComponentEvent e) {
        chartPanel.repaint();
        chartPanel.revalidate();
        this.repaint();
        showAreas();
    }

    @Override
    public void componentShown(ComponentEvent e) { }
}

我使用过jfreechart v1.0.14和jcommon v1.0.18。 在该示例中,图表上方有三个值。当你使窗口变大时,你会看到三个值增长,但是在高度约800px时,第一个值将继续增长而其他值则不会增长。为什么呢?
如果你没有大屏幕分辨率,你可能不会看到错误,因为它发生在我身上。

我做错了吗?这是一个错误吗?

2 个答案:

答案 0 :(得分:1)

一些注意事项:

  • 您可能正在寻找其中一个org.jfree.chart.annotations,其中“在数据空间中指定了坐标”;我们可以看到一些例子here

  • JFreeChart不是组件; ChartPanel是一个重新组织面板以响应各种图表事件的组件; ComponentListener很少有必要保证。

  • 不要使用setBounds();相反,pack()封闭的Window;如果默认值不合格,请覆盖getPreferredSize()中的ChartPanel

  • event dispatch thread上构建和操作仅的Swing GUI对象。

答案 1 :(得分:1)

我自己也有类似的问题。

问题似乎源于图表面板限制其默认最大尺寸,导致子图数据区域无法正确更新。

增加最大高度/宽度可以解决您的问题。

panel.setMaximumHeight(2000);
panel.setMaximumSize(1500);

有关详细信息,请参阅此错误帖子。 http://sourceforge.net/p/jfreechart/bugs/911/