1

I need to plot a lot of data (150-250 points/seconds * 180secs) on a XYPlot. So if i use the autorange method, its a bit to coarse. 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.

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 (e.g. from 0 to 180sec) and not just a section?

What i tried so far, is to have a fixed huge plot without zooming, but it was not usable (the size was 1200x15000).

Thanks in advance!

lhlmgr
  • 2,087
  • 1
  • 22
  • 38

2 Answers2

4

As shown here, override getPreferredSize() to return your desired size for the ChartPanel and pack() the enclosing frame. 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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • thanks for your answer and the links. I'm going to try to resize the plot in the zooming-event - that could be possible. Thanks! – lhlmgr Jul 29 '13 at 08:55
1

I used the getPreferredSize() method as trashgod proposed. I extended from the org.jfree.chart.MouseWheelHandler and implemented a new handleZoomable-method as follows.


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:


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.

lhlmgr
  • 2,087
  • 1
  • 22
  • 38