001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * --------------------
028 * ScrollHandlerFX.java
029 * --------------------
030 * (C) Copyright 2014, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes:
036 * --------
037 * 25-Jun-2014 : Version 1 (DG);
038 *
039 */
040
041package org.jfree.chart.fx.interaction;
042
043import java.awt.geom.Point2D;
044import javafx.scene.input.ScrollEvent;
045import org.jfree.chart.ChartRenderingInfo;
046import org.jfree.chart.JFreeChart;
047import org.jfree.chart.fx.ChartCanvas;
048import org.jfree.chart.plot.PiePlot;
049import org.jfree.chart.plot.Plot;
050import org.jfree.chart.plot.PlotRenderingInfo;
051import org.jfree.chart.plot.Zoomable;
052
053/**
054 * Handles scroll events (mouse wheel etc) on a {@link ChartCanvas}.
055 * 
056 * <p>THE API FOR THIS CLASS IS SUBJECT TO CHANGE IN FUTURE RELEASES.  This is
057 * so that we can incorporate feedback on the (new) JavaFX support in 
058 * JFreeChart.</p>
059 * 
060 * @since 1.0.18
061 */
062public class ScrollHandlerFX extends AbstractMouseHandlerFX 
063        implements MouseHandlerFX {
064
065    /** The zoom factor. */
066    private double zoomFactor = 0.1;
067    
068    /**
069     * Creates a new instance with the specified ID.
070     * 
071     * @param id  the handler ID (<code>null</code> not permitted).
072     */
073    public ScrollHandlerFX(String id) {
074        super(id, false, false, false, false);
075        this.zoomFactor = 0.1;
076    };
077
078    /**
079     * Returns the zoom factor.  The default value is 0.10 (ten percent).
080     * 
081     * @return The zoom factor. 
082     */
083    public double getZoomFactor() {
084        return this.zoomFactor;
085    }
086
087    /**
088     * Sets the zoom factor (a percentage amount by which the mouse wheel 
089     * movement will change the chart size).
090     * 
091     * @param zoomFactor  the zoom factor.
092     */
093    public void setZoomFactor(double zoomFactor) {
094        this.zoomFactor = zoomFactor;
095    }
096    
097    @Override
098    public void handleScroll(ChartCanvas canvas, ScrollEvent e) {
099        JFreeChart chart = canvas.getChart();
100        Plot plot = chart.getPlot();
101        if (plot instanceof Zoomable) {
102            Zoomable zoomable = (Zoomable) plot;
103            handleZoomable(canvas, zoomable, e);
104        }
105        else if (plot instanceof PiePlot) {
106            PiePlot pp = (PiePlot) plot;
107            pp.handleMouseWheelRotation((int) e.getDeltaY());
108        }
109    }
110    
111    /**
112     * Handle the case where a plot implements the {@link Zoomable} interface.
113     *
114     * @param zoomable  the zoomable plot.
115     * @param e  the mouse wheel event.
116     */
117    private void handleZoomable(ChartCanvas canvas, Zoomable zoomable, 
118            ScrollEvent e) {
119        // don't zoom unless the mouse pointer is in the plot's data area
120        ChartRenderingInfo info = canvas.getRenderingInfo();
121        PlotRenderingInfo pinfo = info.getPlotInfo();
122        Point2D p = new Point2D.Double(e.getX(), e.getY());
123        if (pinfo.getDataArea().contains(p)) {
124            Plot plot = (Plot) zoomable;
125            // do not notify while zooming each axis
126            boolean notifyState = plot.isNotify();
127            plot.setNotify(false);
128            int clicks = (int) e.getDeltaY();
129            double zf = 1.0 + this.zoomFactor;
130            if (clicks < 0) {
131                zf = 1.0 / zf;
132            }
133            if (true) { //this.chartPanel.isDomainZoomable()) {
134                zoomable.zoomDomainAxes(zf, pinfo, p, true);
135            }
136            if (true) { //this.chartPanel.isRangeZoomable()) {
137                zoomable.zoomRangeAxes(zf, pinfo, p, true);
138            }
139            plot.setNotify(notifyState);  // this generates the change event too
140        } 
141    }
142
143}