1 43 44 package org.jfree.chart.plot; 45 46 import java.util.ArrayList ; 47 import java.util.Collections ; 48 import java.util.List ; 49 50 54 public class PieLabelDistributor { 55 56 57 private List labels; 58 59 60 private double minGap = 4.0; 61 62 67 public PieLabelDistributor(int labelCount) { 68 this.labels = new ArrayList (labelCount); 69 } 70 71 78 public PieLabelRecord getPieLabelRecord(int index) { 79 return (PieLabelRecord) this.labels.get(index); 80 } 81 82 87 public void addPieLabelRecord(PieLabelRecord record) { 88 this.labels.add(record); 89 } 90 91 96 public int getItemCount() { 97 return this.labels.size(); 98 } 99 100 106 public void distributeLabels(double minY, double height) { 107 sort(); 108 if (isOverlap()) { 109 adjustInwards(); 110 } 111 112 if (isOverlap()) { 114 adjustDownwards(minY, height); 115 } 116 117 if (isOverlap()) { 118 adjustUpwards(minY, height); 119 } 120 121 if (isOverlap()) { 122 spreadEvenly(minY, height); 123 } 124 125 } 126 127 133 private boolean isOverlap() { 134 double y = 0.0; 135 for (int i = 0; i < this.labels.size(); i++) { 136 PieLabelRecord plr = getPieLabelRecord(i); 137 if (y > plr.getLowerY()) { 138 return true; 139 } 140 y = plr.getUpperY(); 141 } 142 return false; 143 } 144 145 149 protected void adjustInwards() { 150 int lower = 0; 151 int upper = this.labels.size() - 1; 152 while (upper > lower) { 153 if (lower < upper - 1) { 154 PieLabelRecord r0 = getPieLabelRecord(lower); 155 PieLabelRecord r1 = getPieLabelRecord(lower + 1); 156 if (r1.getLowerY() < r0.getUpperY()) { 157 double adjust = r0.getUpperY() - r1.getLowerY() 158 + this.minGap; 159 r1.setAllocatedY(r1.getAllocatedY() + adjust); 160 } 161 } 162 PieLabelRecord r2 = getPieLabelRecord(upper - 1); 163 PieLabelRecord r3 = getPieLabelRecord(upper); 164 if (r2.getUpperY() > r3.getLowerY()) { 165 double adjust = (r2.getUpperY() - r3.getLowerY()) + this.minGap; 166 r2.setAllocatedY(r2.getAllocatedY() - adjust); 167 } 168 lower++; 169 upper--; 170 } 171 } 172 173 180 protected void adjustDownwards(double minY, double height) { 181 for (int i = 0; i < this.labels.size() - 1; i++) { 182 PieLabelRecord record0 = getPieLabelRecord(i); 183 PieLabelRecord record1 = getPieLabelRecord(i + 1); 184 if (record1.getLowerY() < record0.getUpperY()) { 185 record1.setAllocatedY( 186 Math.min( 187 minY + height, 188 record0.getUpperY() + this.minGap 189 + record1.getLabelHeight() / 2.0 190 ) 191 ); 192 } 193 } 194 } 195 196 203 protected void adjustUpwards(double minY, double height) { 204 for (int i = this.labels.size() - 1; i > 0; i--) { 205 PieLabelRecord record0 = getPieLabelRecord(i); 206 PieLabelRecord record1 = getPieLabelRecord(i - 1); 207 if (record1.getUpperY() > record0.getLowerY()) { 208 record1.setAllocatedY( 209 Math.max( 210 minY, 211 record0.getLowerY() - this.minGap 212 - record1.getLabelHeight() / 2.0 213 ) 214 ); 215 } 216 } 217 } 218 219 226 protected void spreadEvenly(double minY, double height) { 227 double y = minY; 228 double sumOfLabelHeights = 0.0; 229 for (int i = 0; i < this.labels.size(); i++) { 230 sumOfLabelHeights += getPieLabelRecord(i).getLabelHeight(); 231 } 232 double gap = height - sumOfLabelHeights; 233 if (this.labels.size() > 1) { 234 gap = gap / (this.labels.size() - 1); 235 } 236 for (int i = 0; i < this.labels.size(); i++) { 237 PieLabelRecord record = getPieLabelRecord(i); 238 y = y + record.getLabelHeight() / 2.0; 239 record.setAllocatedY(y); 240 y = y + record.getLabelHeight() / 2.0 + gap; 241 } 242 } 243 244 247 public void sort() { 248 Collections.sort(this.labels); 249 } 250 251 257 public String toString() { 258 StringBuffer result = new StringBuffer (); 259 for (int i = 0; i < this.labels.size(); i++) { 260 result.append(getPieLabelRecord(i).toString()).append("\n"); 261 } 262 return result.toString(); 263 } 264 265 } 266 | Popular Tags |