001/* ================================================
002 * JFreeChart-FX : JavaFX extensions for JFreeChart
003 * ================================================
004 *
005 * (C) Copyright 2017-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  https://github.com/jfree/jfreechart-fx
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 * CrosshairOverlayFX.java
029 * -----------------------
030 * (C) Copyright 2016-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.fx.overlay;
038
039import java.awt.Graphics2D;
040import java.awt.Shape;
041import java.awt.geom.Rectangle2D;
042import org.jfree.chart.JFreeChart;
043import org.jfree.chart.axis.ValueAxis;
044import org.jfree.chart.fx.ChartCanvas;
045import org.jfree.chart.fx.ChartViewer;
046import org.jfree.chart.panel.CrosshairOverlay;
047import org.jfree.chart.plot.Crosshair;
048import org.jfree.chart.plot.PlotOrientation;
049import org.jfree.chart.plot.XYPlot;
050import org.jfree.chart.ui.RectangleEdge;
051
052/**
053 * An overlay for a {@link ChartViewer} that draws crosshairs on a plot.
054 */
055public class CrosshairOverlayFX extends CrosshairOverlay implements OverlayFX {
056
057    /**
058     * Creates a new instance.
059     */
060    public CrosshairOverlayFX() {
061        super();
062    }
063
064    @Override
065    public void paintOverlay(Graphics2D g2, ChartCanvas chartCanvas) {
066        if (chartCanvas.getRenderingInfo() == null) {
067            return;
068        }
069        Shape savedClip = g2.getClip();
070        Rectangle2D dataArea = chartCanvas.getRenderingInfo().getPlotInfo().getDataArea();
071        g2.clip(dataArea);
072        JFreeChart chart = chartCanvas.getChart();
073        XYPlot plot = (XYPlot) chart.getPlot();
074        ValueAxis xAxis = plot.getDomainAxis();
075        RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
076        for (Crosshair ch : getDomainCrosshairs()) {
077            if (ch.isVisible()) {
078                double x = ch.getValue();
079                double xx = xAxis.valueToJava2D(x, dataArea, xAxisEdge);
080                if (plot.getOrientation() == PlotOrientation.VERTICAL) {
081                    drawVerticalCrosshair(g2, dataArea, xx, ch);
082                } else {
083                    drawHorizontalCrosshair(g2, dataArea, xx, ch);
084                }
085            }
086        }
087        ValueAxis yAxis = plot.getRangeAxis();
088        RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
089        for (Crosshair ch : getRangeCrosshairs()) {
090            if (ch.isVisible()) {
091                double y = ch.getValue();
092                double yy = yAxis.valueToJava2D(y, dataArea, yAxisEdge);
093                if (plot.getOrientation() == PlotOrientation.VERTICAL) {
094                    drawHorizontalCrosshair(g2, dataArea, yy, ch);
095                } else {
096                    drawVerticalCrosshair(g2, dataArea, yy, ch);
097                }
098            }
099        }
100        g2.setClip(savedClip);
101    }
102}