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