1 38 39 package org.jfree.chart; 40 41 import java.util.Iterator ; 42 43 48 public class StandardLegendItemLayout implements LegendItemLayout { 49 50 51 public static final int VERTICAL = 0; 52 53 54 public static final int HORIZONTAL = 1; 55 56 57 private int orientation; 58 59 60 private double dimension; 61 62 68 public StandardLegendItemLayout(int orientation, double dimension) { 69 70 this.orientation = orientation; 71 this.dimension = dimension; 72 73 } 74 75 80 public void layoutLegendItems(LegendItemCollection collection) { 81 82 if (orientation == HORIZONTAL) { 83 doHorizontalLayout(collection); 84 } 85 else if (orientation == VERTICAL) { 86 doVerticalLayout(collection); 87 } 88 89 } 90 91 95 private void doHorizontalLayout(LegendItemCollection collection) { 96 97 Iterator iterator = collection.iterator(); 100 101 boolean first = true; 104 double currentRowX = 0.0; 105 double currentRowY = 0.0; 106 double currentRowHeight = 0.0; 107 108 while (iterator.hasNext()) { 109 DrawableLegendItem item = (DrawableLegendItem) iterator.next(); 110 if ((first) || (item.getWidth() < (this.dimension - currentRowX))) { 111 item.setX(currentRowX); 112 item.setY(currentRowY); 113 currentRowX = currentRowX + item.getWidth(); 114 currentRowHeight = Math.max(currentRowHeight, item.getHeight()); 115 first = false; 116 } 117 else { currentRowY = currentRowY + currentRowHeight; 119 currentRowHeight = item.getHeight(); 120 item.setX(0.0); 121 currentRowX = item.getWidth(); 122 } 123 124 } 125 126 } 127 128 132 private void doVerticalLayout(LegendItemCollection collection) { 133 134 Iterator iterator = collection.iterator(); 137 138 boolean first = true; 141 double currentColumnX = 0.0; 142 double currentColumnY = 0.0; 143 double currentColumnWidth = 0.0; 144 145 while (iterator.hasNext()) { 146 DrawableLegendItem item = (DrawableLegendItem) iterator.next(); 147 if ((first) || (item.getHeight() < (this.dimension - currentColumnY))) { 148 item.setX(currentColumnX); 149 item.setY(currentColumnY); 150 currentColumnY = currentColumnY + item.getHeight(); 151 currentColumnWidth = Math.max(currentColumnWidth, item.getWidth()); 152 first = false; 153 } 154 else { currentColumnX = currentColumnX + currentColumnWidth; 156 currentColumnWidth = item.getWidth(); 157 item.setY(0.0); 158 currentColumnY = item.getHeight(); 159 } 160 161 } 162 } 163 164 } 165 | Popular Tags |