简体   繁体   English

JFreeChart在缩放时自动调整大小

[英]JFreeChart auto resize on zoom

I need to plot a lot of data (150-250 points/seconds * 180secs) on a XYPlot. 我需要在XYPlot上绘制大量数据(150-250点/秒* 180秒)。 So if i use the autorange method, its a bit to coarse. 所以,如果我使用autorange方法,它有点粗糙。 If i zoom into the plot, i just see a range of the data (eg. 10.25 to 14.50).. its good, and it works very well, but it would be better if i see the full range, but in an better resolution. 如果我放大图,我只看到一系列的数据(例如10.25到14.50)..它很好,而且效果很好,但如果我看到全范围,但更好的分辨率会更好。

Is there a possibility to zoom in the plot, and additionally resize the plot-area (so you have more space to print the plot), so that the full range is displayed (eg from 0 to 180sec) and not just a section? 是否有可能放大绘图,并另外调整绘图区域的大小(因此您有更多的空间来打印绘图),以便显示整个范围(例如从0到180秒)而不仅仅是一个部分?

What i tried so far, is to have a fixed huge plot without zooming, but it was not usable (the size was 1200x15000). 我到目前为止尝试的是,有一个固定的巨大的情节没有缩放,但它不可用(大小是1200x15000)。

Thanks in advance! 提前致谢!

As shown here , override getPreferredSize() to return your desired size for the ChartPanel and pack() the enclosing frame. 如图所示这里 ,覆盖getPreferredSize()返回你想要的大小为ChartPanelpack()围框。 The result obtained by applying JFrame.MAXIMIZED_BOTH in setExtendedState() will maximize use of the screen at the user's chosen resolution, but you can try a different display mode , if available. 通过在setExtendedState()应用JFrame.MAXIMIZED_BOTH获得的结果将最大限度地使用用户所选分辨率的屏幕,但您可以尝试使用不同的显示模式 (如果可用)。

I used the getPreferredSize() method as trashgod proposed. 我使用getPreferredSize()方法作为trashgod提议。 I extended from the org.jfree.chart.MouseWheelHandler and implemented a new handleZoomable-method as follows. 我从扩展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
    }

}

And in the org.jfree.chart.ChartPanel Class, i just modified the setMouseWheelEnabled Method to: 而在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;
        }
    }

And now, the CharPanel which is located in a scrollview resizes, and zooms in. 现在,位于scrollview中的CharPanel会调整大小并放大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM