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 * BoxAndWhiskerToolTipGenerator.java 029 * ------------------------------------ 030 * (C) Copyright 2004-present, by David Browning and Contributors. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.chart.labels; 038 039import java.io.Serializable; 040import java.text.MessageFormat; 041import java.text.NumberFormat; 042import org.jfree.chart.util.PublicCloneable; 043 044import org.jfree.data.category.CategoryDataset; 045import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset; 046 047/** 048 * An item label generator for plots that use data from a 049 * {@link BoxAndWhiskerCategoryDataset}. 050 * <P> 051 * The tooltip text and item label text are composed using a 052 * {@link java.text.MessageFormat} object, that can aggregate some or all of 053 * the following string values into a message. 054 * <ul> 055 * <li>0 : Series Name</li> 056 * <li>1 : X (value or date)</li> 057 * <li>2 : Mean</li> 058 * <li>3 : Median</li> 059 * <li>4 : Minimum</li> 060 * <li>5 : Maximum</li> 061 * <li>6 : Quartile 1</li> 062 * <li>7 : Quartile 3</li> 063 * </ul> 064 */ 065public class BoxAndWhiskerToolTipGenerator 066 extends StandardCategoryToolTipGenerator 067 implements CategoryToolTipGenerator, Cloneable, PublicCloneable, 068 Serializable { 069 070 /** For serialization. */ 071 private static final long serialVersionUID = -6076837753823076334L; 072 073 /** The default tooltip format string. */ 074 public static final String DEFAULT_TOOL_TIP_FORMAT 075 = "X: {1} Mean: {2} Median: {3} Min: {4} Max: {5} Q1: {6} Q3: {7} "; 076 077 /** 078 * Creates a default tool tip generator. 079 */ 080 public BoxAndWhiskerToolTipGenerator() { 081 super(DEFAULT_TOOL_TIP_FORMAT, NumberFormat.getInstance()); 082 } 083 084 /** 085 * Creates a tool tip formatter. 086 * 087 * @param format the tool tip format string. 088 * @param formatter the formatter. 089 */ 090 public BoxAndWhiskerToolTipGenerator(String format, 091 NumberFormat formatter) { 092 super(format, formatter); 093 } 094 095 /** 096 * Creates the array of items that can be passed to the 097 * {@link MessageFormat} class for creating labels. 098 * 099 * @param dataset the dataset ({@code null} not permitted). 100 * @param series the series (zero-based index). 101 * @param item the item (zero-based index). 102 * 103 * @return The items (never {@code null}). 104 */ 105 @Override 106 protected Object[] createItemArray(CategoryDataset dataset, int series, 107 int item) { 108 Object[] result = new Object[8]; 109 result[0] = dataset.getRowKey(series); 110 Number y = dataset.getValue(series, item); 111 NumberFormat formatter = getNumberFormat(); 112 result[1] = formatter.format(y); 113 if (dataset instanceof BoxAndWhiskerCategoryDataset) { 114 BoxAndWhiskerCategoryDataset d 115 = (BoxAndWhiskerCategoryDataset) dataset; 116 result[2] = formatter.format(d.getMeanValue(series, item)); 117 result[3] = formatter.format(d.getMedianValue(series, item)); 118 result[4] = formatter.format(d.getMinRegularValue(series, item)); 119 result[5] = formatter.format(d.getMaxRegularValue(series, item)); 120 result[6] = formatter.format(d.getQ1Value(series, item)); 121 result[7] = formatter.format(d.getQ3Value(series, item)); 122 } 123 return result; 124 } 125 126 /** 127 * Tests if this object is equal to another. 128 * 129 * @param obj the other object. 130 * 131 * @return A boolean. 132 */ 133 @Override 134 public boolean equals(Object obj) { 135 if (obj == this) { 136 return true; 137 } 138 if (obj instanceof BoxAndWhiskerToolTipGenerator) { 139 return super.equals(obj); 140 } 141 return false; 142 } 143 144}