1 45 46 package org.jfree.chart.labels; 47 48 import java.io.Serializable ; 49 import java.text.MessageFormat ; 50 import java.text.NumberFormat ; 51 52 import org.jfree.data.general.DatasetUtilities; 53 import org.jfree.data.general.PieDataset; 54 55 58 public class AbstractPieItemLabelGenerator implements Serializable { 59 60 61 private static final long serialVersionUID = 7347703325267846275L; 62 63 64 private String labelFormat; 65 66 67 private NumberFormat numberFormat; 68 69 70 private NumberFormat percentFormat; 71 72 82 protected AbstractPieItemLabelGenerator(String labelFormat, 83 NumberFormat numberFormat, 84 NumberFormat percentFormat) { 85 86 if (labelFormat == null) { 87 throw new IllegalArgumentException ("Null 'labelFormat' argument."); 88 } 89 if (numberFormat == null) { 90 throw new IllegalArgumentException ("Null 'numberFormat' argument."); 91 } 92 if (percentFormat == null) { 93 throw new IllegalArgumentException ( 94 "Null 'percentFormat' argument." 95 ); 96 } 97 this.labelFormat = labelFormat; 98 this.numberFormat = numberFormat; 99 this.percentFormat = percentFormat; 100 101 } 102 103 108 public String getLabelFormat() { 109 return this.labelFormat; 110 } 111 112 117 public NumberFormat getNumberFormat() { 118 return this.numberFormat; 119 } 120 121 126 public NumberFormat getPercentFormat() { 127 return this.percentFormat; 128 } 129 130 146 protected Object [] createItemArray(PieDataset dataset, Comparable key) { 147 Object [] result = new Object [4]; 148 double total = DatasetUtilities.calculatePieDatasetTotal(dataset); 149 result[0] = key.toString(); 150 Number value = dataset.getValue(key); 151 if (value != null) { 152 result[1] = this.numberFormat.format(value); 153 } 154 else { 155 result[1] = "null"; 156 } 157 double percent = 0.0; 158 if (value != null) { 159 double v = value.doubleValue(); 160 if (v > 0.0) { 161 percent = v / total; 162 } 163 } 164 result[2] = this.percentFormat.format(percent); 165 result[3] = this.numberFormat.format(total); 166 return result; 167 } 168 169 177 protected String generateSectionLabel(PieDataset dataset, Comparable key) { 178 String result = null; 179 if (dataset != null) { 180 Object [] items = createItemArray(dataset, key); 181 result = MessageFormat.format(this.labelFormat, items); 182 } 183 return result; 184 } 185 186 193 public boolean equals(Object obj) { 194 if (obj == this) { 195 return true; 196 } 197 if (!(obj instanceof AbstractPieItemLabelGenerator)) { 198 return false; 199 } 200 201 AbstractPieItemLabelGenerator that 202 = (AbstractPieItemLabelGenerator) obj; 203 if (!this.labelFormat.equals(that.labelFormat)) { 204 return false; 205 } 206 if (!this.numberFormat.equals(that.numberFormat)) { 207 return false; 208 } 209 if (!this.percentFormat.equals(that.percentFormat)) { 210 return false; 211 } 212 return true; 213 214 } 215 216 223 public Object clone() throws CloneNotSupportedException { 224 AbstractPieItemLabelGenerator clone 225 = (AbstractPieItemLabelGenerator) super.clone(); 226 if (this.numberFormat != null) { 227 clone.numberFormat = (NumberFormat ) this.numberFormat.clone(); 228 } 229 if (this.percentFormat != null) { 230 clone.percentFormat = (NumberFormat ) this.percentFormat.clone(); 231 } 232 return clone; 233 } 234 235 } 236 | Popular Tags |