JFreeChart在缩放时自动调整大小

时间:2013-07-28 13:04:54

标签: java jfreechart

我需要在XYPlot上绘制大量数据(150-250点/秒* 180秒)。所以,如果我使用autorange方法,它有点粗糙。如果我放大图,我只看到一系列的数据(例如10.25到14.50)..它很好,而且效果很好,但如果我看到全范围,但更好的分辨率会更好

是否有可能放大绘图,并另外调整绘图区域的大小(这样您有更多的空间来打印绘图),以便显示整个范围(例如从0到180秒)而不仅仅是部?

到目前为止我尝试的是有一个没有缩放的固定巨大的绘图,但它不可用(大小为1200x15000)。

提前致谢!

2 个答案:

答案 0 :(得分:3)

如图所示here,覆盖getPreferredSize()以返回所需大小的ChartPanelpack()封闭框架。通过在JFrame.MAXIMIZED_BOTH中应用setExtendedState()获得的结果将最大限度地使用用户所选分辨率的屏幕,但如果可用,您可以尝试使用其他display mode

答案 1 :(得分:1)

我使用了getPreferredSize()方法作为trashgod提议。我从org.jfree.chart.MouseWheelHandler扩展并实现了一个新的handleZoomable方法,如下所示。


package org.jfree.chart;

import java.awt.Dimension;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Point2D;
import java.io.Serializable;

import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.Zoomable;

/** http://stackoverflow.com/questions/17908498/jfreechart-auto-resize-on-zoom */
class MouseWheelHandlerResize extends MouseWheelHandler {

    /** The chart panel. */
    private ChartPanel chartPanel;

    /** The zoom factor. */
    double zoomFactor;

    /** minimum size */
    final int MIN_SIZE = 300;

    /** maximal size */
    final int MAX_SIZE = 20000;

    public MouseWheelHandlerResize(ChartPanel chartPanel) {
        super(chartPanel);
        this.chartPanel = chartPanel;
        this.zoomFactor = 0.05;
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        JFreeChart chart = this.chartPanel.getChart();
        if (chart == null) {
            return;
        }
        Plot plot = chart.getPlot();
        if (plot instanceof Zoomable) {
            Zoomable zoomable = (Zoomable) plot;
            handleZoomable(zoomable, e);
        }
        else if (plot instanceof PiePlot) {
            PiePlot pp = (PiePlot) plot;
            pp.handleMouseWheelRotation(e.getWheelRotation());
        }
    }

    private void handleZoomable(Zoomable zoomable, MouseWheelEvent e) {
        // don't zoom unless the mouse pointer is in the plot's data area
        ChartRenderingInfo info = this.chartPanel.getChartRenderingInfo();
        PlotRenderingInfo pinfo = info.getPlotInfo();
        Point2D p = this.chartPanel.translateScreenToJava2D(e.getPoint());
        if (!pinfo.getDataArea().contains(p)) {
            return;
        }

        Plot plot = (Plot) zoomable;
        // do not notify while zooming each axis
        boolean notifyState = plot.isNotify();
        plot.setNotify(false);
        int clicks = e.getWheelRotation();
        double zf = 1.0 + this.zoomFactor;
        if (clicks < 0) {
            zf = 1.0 / zf;
        }
        final Dimension dim = this.chartPanel.getPreferredSize();
        this.chartPanel.setPreferredSize(new Dimension((int)(Math.min(Math.max(MIN_SIZE, dim.width)*zf, MAX_SIZE)), (int)(dim.height)));        
        this.chartPanel.validate();
        this.chartPanel.updateUI();
        plot.setNotify(notifyState);  // this generates the change event too
    }

}

org.jfree.chart.ChartPanel班级中,我刚刚将setMouseWheelEnabled方法修改为:


public void setMouseWheelEnabled(boolean flag) {
        if (flag && this.mouseWheelHandler == null) {
            this.mouseWheelHandler = new MouseWheelHandlerResize(this);
        }
        else if (!flag && this.mouseWheelHandler != null) {
            removeMouseWheelListener(this.mouseWheelHandler);
            this.mouseWheelHandler = null;
        }
    }

现在,位于scrollview中的CharPanel会调整大小并放大。