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 * IntervalCategoryItemLabelGenerator.java 029 * --------------------------------------- 030 * (C) Copyright 2004-present, by David Gilbert. 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.DateFormat; 041import java.text.NumberFormat; 042import org.jfree.chart.util.PublicCloneable; 043 044import org.jfree.data.category.CategoryDataset; 045import org.jfree.data.category.IntervalCategoryDataset; 046 047/** 048 * A label generator for plots that use data from an 049 * {@link IntervalCategoryDataset}. 050 */ 051public class IntervalCategoryItemLabelGenerator 052 extends StandardCategoryItemLabelGenerator 053 implements CategoryItemLabelGenerator, PublicCloneable, Cloneable, 054 Serializable { 055 056 /** For serialization. */ 057 private static final long serialVersionUID = 5056909225610630529L; 058 059 /** The default format string. */ 060 public static final String DEFAULT_LABEL_FORMAT_STRING 061 = "({0}, {1}) = {3} - {4}"; 062 063 /** 064 * Creates a new generator with a default number formatter. 065 */ 066 public IntervalCategoryItemLabelGenerator() { 067 super(DEFAULT_LABEL_FORMAT_STRING, NumberFormat.getInstance()); 068 } 069 070 /** 071 * Creates a new generator with the specified number formatter. 072 * 073 * @param labelFormat the label format string ({@code null} not 074 * permitted). 075 * @param formatter the number formatter ({@code null} not permitted). 076 */ 077 public IntervalCategoryItemLabelGenerator(String labelFormat, 078 NumberFormat formatter) { 079 super(labelFormat, formatter); 080 } 081 082 /** 083 * Creates a new generator with the specified date formatter. 084 * 085 * @param labelFormat the label format string ({@code null} not 086 * permitted). 087 * @param formatter the date formatter ({@code null} not permitted). 088 */ 089 public IntervalCategoryItemLabelGenerator(String labelFormat, 090 DateFormat formatter) { 091 super(labelFormat, formatter); 092 } 093 094 /** 095 * Creates the array of items that can be passed to the 096 * {@code MessageFormat} class for creating labels. 097 * 098 * @param dataset the dataset ({@code null} not permitted). 099 * @param row the row index (zero-based). 100 * @param column the column index (zero-based). 101 * 102 * @return The items (never {@code null}). 103 */ 104 @Override 105 protected Object[] createItemArray(CategoryDataset dataset, 106 int row, int column) { 107 Object[] result = new Object[5]; 108 result[0] = dataset.getRowKey(row).toString(); 109 result[1] = dataset.getColumnKey(column).toString(); 110 Number value = dataset.getValue(row, column); 111 if (getNumberFormat() != null) { 112 result[2] = getNumberFormat().format(value); 113 } 114 else if (getDateFormat() != null) { 115 result[2] = getDateFormat().format(value); 116 } 117 118 if (dataset instanceof IntervalCategoryDataset) { 119 IntervalCategoryDataset icd = (IntervalCategoryDataset) dataset; 120 Number start = icd.getStartValue(row, column); 121 Number end = icd.getEndValue(row, column); 122 if (getNumberFormat() != null) { 123 result[3] = getNumberFormat().format(start); 124 result[4] = getNumberFormat().format(end); 125 } 126 else if (getDateFormat() != null) { 127 result[3] = getDateFormat().format(start); 128 result[4] = getDateFormat().format(end); 129 } 130 } 131 return result; 132 } 133 134}