1 20 package org.openi.chart; 21 22 import org.jfree.chart.labels.CategoryLabelGenerator; 23 import org.jfree.data.category.CategoryDataset; 24 import java.text.NumberFormat ; 25 26 27 33 public class LabelGenerator implements CategoryLabelGenerator { 34 38 private Integer category; 39 40 41 private NumberFormat formatter = NumberFormat.getPercentInstance(); 42 43 50 public LabelGenerator(final int category) { 51 this(new Integer (category)); 52 } 53 54 62 public LabelGenerator(Integer category) { 63 this.category = category; 64 } 65 66 76 public String generateLabel(CategoryDataset dataset, int series, 77 int category) { 78 String result = null; 79 double base = 0.0; 80 81 if (this.category != null) { 82 final Number b = dataset.getValue(series, this.category.intValue()); 83 base = b.doubleValue(); 84 } else { 85 base = calculateSeriesTotal(dataset, series); 86 } 87 88 Number value = dataset.getValue(series, category); 89 90 if (value != null) { 91 final double v = value.doubleValue(); 92 result = value.toString() + " (" + this.formatter.format(v / base) 94 + ")"; 95 } 96 97 return result; 98 } 99 100 108 private double calculateSeriesTotal(CategoryDataset dataset, int series) { 109 double result = 0.0; 110 111 for (int i = 0; i < dataset.getColumnCount(); i++) { 112 Number value = dataset.getValue(series, i); 113 114 if (value != null) { 115 result = result + value.doubleValue(); 116 } 117 } 118 119 return result; 120 } 121 } 122 | Popular Tags |