001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, 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-present, 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 extends CrosshairState { 051 052 /** 053 * The row key for the crosshair point. 054 */ 055 private Comparable rowKey; 056 057 /** 058 * The column key for the crosshair point. 059 */ 060 private Comparable columnKey; 061 062 /** 063 * Creates a new instance. 064 */ 065 public CategoryCrosshairState() { 066 this.rowKey = null; 067 this.columnKey = null; 068 } 069 070 /** 071 * Returns the row key. 072 * 073 * @return The row key. 074 */ 075 public Comparable getRowKey() { 076 return this.rowKey; 077 } 078 079 /** 080 * Sets the row key. 081 * 082 * @param key the row key. 083 */ 084 public void setRowKey(Comparable key) { 085 this.rowKey = key; 086 } 087 088 /** 089 * Returns the column key. 090 * 091 * @return The column key. 092 */ 093 public Comparable getColumnKey() { 094 return this.columnKey; 095 } 096 097 /** 098 * Sets the column key. 099 * 100 * @param key the key. 101 */ 102 public void setColumnKey(Comparable key) { 103 this.columnKey = key; 104 } 105 106 /** 107 * Evaluates a data point from a {@link CategoryItemRenderer} and if it is 108 * the closest to the anchor point it becomes the new crosshair point. 109 * 110 * @param rowKey the row key. 111 * @param columnKey the column key. 112 * @param value y coordinate (measured against the range axis). 113 * @param datasetIndex the dataset index for this point. 114 * @param transX x translated into Java2D space. 115 * @param transY y translated into Java2D space. 116 * @param orientation the plot orientation. 117 */ 118 public void updateCrosshairPoint(Comparable rowKey, Comparable columnKey, 119 double value, int datasetIndex, double transX, double transY, 120 PlotOrientation orientation) { 121 122 Point2D anchor = getAnchor(); 123 if (anchor != null) { 124 double xx = anchor.getX(); 125 double yy = anchor.getY(); 126 if (orientation == PlotOrientation.HORIZONTAL) { 127 double temp = yy; 128 yy = xx; 129 xx = temp; 130 } 131 double d = (transX - xx) * (transX - xx) 132 + (transY - yy) * (transY - yy); 133 134 if (d < getCrosshairDistance()) { 135 this.rowKey = rowKey; 136 this.columnKey = columnKey; 137 setCrosshairY(value); 138 setDatasetIndex(datasetIndex); 139 setCrosshairDistance(d); 140 } 141 } 142 143 } 144 145 /** 146 * Updates only the crosshair row and column keys (this is for the case 147 * where the range crosshair does NOT lock onto the nearest data value). 148 * 149 * @param rowKey the row key. 150 * @param columnKey the column key. 151 * @param datasetIndex the dataset axis index. 152 * @param transX the translated x-value. 153 * @param orientation the plot orientation. 154 */ 155 public void updateCrosshairX(Comparable rowKey, Comparable columnKey, 156 int datasetIndex, double transX, PlotOrientation orientation) { 157 158 Point2D anchor = getAnchor(); 159 if (anchor != null) { 160 double anchorX = anchor.getX(); 161 if (orientation == PlotOrientation.HORIZONTAL) { 162 anchorX = anchor.getY(); 163 } 164 double d = Math.abs(transX - anchorX); 165 if (d < getCrosshairDistance()) { 166 this.rowKey = rowKey; 167 this.columnKey = columnKey; 168 setDatasetIndex(datasetIndex); 169 setCrosshairDistance(d); 170 } 171 } 172 173 } 174 175}