001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, by David Gilbert 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 * CategoryCrosshairState.java 029 * --------------------------- 030 * (C) Copyright 2008-2022, by David Gilbert and Contributors. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.chart.plot; 038 039import java.awt.geom.Point2D; 040 041import org.jfree.chart.renderer.category.CategoryItemRenderer; 042 043/** 044 * Represents state information for the crosshairs in a {@link CategoryPlot}. 045 * An instance of this class is created at the start of the rendering process, 046 * and updated as each data item is rendered. At the end of the rendering 047 * process, this class holds the row key, column key and value for the 048 * crosshair location. 049 */ 050public class CategoryCrosshairState<R extends Comparable<R>, C extends Comparable<C>> 051 extends CrosshairState { 052 053 /** 054 * The row key for the crosshair point. 055 */ 056 private R rowKey; 057 058 /** 059 * The column key for the crosshair point. 060 */ 061 private C columnKey; 062 063 /** 064 * Creates a new instance. 065 */ 066 public CategoryCrosshairState() { 067 this.rowKey = null; 068 this.columnKey = null; 069 } 070 071 /** 072 * Returns the row key. 073 * 074 * @return The row key. 075 */ 076 public R getRowKey() { 077 return this.rowKey; 078 } 079 080 /** 081 * Sets the row key. 082 * 083 * @param key the row key. 084 */ 085 public void setRowKey(R key) { 086 this.rowKey = key; 087 } 088 089 /** 090 * Returns the column key. 091 * 092 * @return The column key. 093 */ 094 public C getColumnKey() { 095 return this.columnKey; 096 } 097 098 /** 099 * Sets the column key. 100 * 101 * @param key the key. 102 */ 103 public void setColumnKey(C key) { 104 this.columnKey = key; 105 } 106 107 /** 108 * Evaluates a data point from a {@link CategoryItemRenderer} and if it is 109 * the closest to the anchor point it becomes the new crosshair point. 110 * 111 * @param rowKey the row key. 112 * @param columnKey the column key. 113 * @param value y coordinate (measured against the range axis). 114 * @param datasetIndex the dataset index for this point. 115 * @param transX x translated into Java2D space. 116 * @param transY y translated into Java2D space. 117 * @param orientation the plot orientation. 118 */ 119 public void updateCrosshairPoint(R rowKey, C columnKey, 120 double value, int datasetIndex, double transX, double transY, 121 PlotOrientation orientation) { 122 123 Point2D anchor = getAnchor(); 124 if (anchor != null) { 125 double xx = anchor.getX(); 126 double yy = anchor.getY(); 127 if (orientation == PlotOrientation.HORIZONTAL) { 128 double temp = yy; 129 yy = xx; 130 xx = temp; 131 } 132 double d = (transX - xx) * (transX - xx) 133 + (transY - yy) * (transY - yy); 134 135 if (d < getCrosshairDistance()) { 136 this.rowKey = rowKey; 137 this.columnKey = columnKey; 138 setCrosshairY(value); 139 setDatasetIndex(datasetIndex); 140 setCrosshairDistance(d); 141 } 142 } 143 144 } 145 146 /** 147 * Updates only the crosshair row and column keys (this is for the case 148 * where the range crosshair does NOT lock onto the nearest data value). 149 * 150 * @param rowKey the row key. 151 * @param columnKey the column key. 152 * @param datasetIndex the dataset axis index. 153 * @param transX the translated x-value. 154 * @param orientation the plot orientation. 155 */ 156 public void updateCrosshairX(R rowKey, C columnKey, 157 int datasetIndex, double transX, PlotOrientation orientation) { 158 159 Point2D anchor = getAnchor(); 160 if (anchor != null) { 161 double anchorX = anchor.getX(); 162 if (orientation == PlotOrientation.HORIZONTAL) { 163 anchorX = anchor.getY(); 164 } 165 double d = Math.abs(transX - anchorX); 166 if (d < getCrosshairDistance()) { 167 this.rowKey = rowKey; 168 this.columnKey = columnKey; 169 setDatasetIndex(datasetIndex); 170 setCrosshairDistance(d); 171 } 172 } 173 174 } 175 176}