1 19 package jcckit.plot; 20 21 import jcckit.graphic.BasicGraphicAttributes; 22 import jcckit.graphic.GraphicalElement; 23 import jcckit.graphic.GraphicalComposite; 24 import jcckit.graphic.GraphicAttributes; 25 import jcckit.graphic.GraphPoint; 26 import jcckit.graphic.Polygon; 27 import jcckit.graphic.Rectangle; 28 import jcckit.graphic.ShapeAttributes; 29 import jcckit.graphic.Text; 30 import jcckit.graphic.TextAttributes; 31 import jcckit.util.ConfigData; 32 import jcckit.util.ConfigParameters; 33 import jcckit.util.ConfigParametersBasedConfigData; 34 import jcckit.util.Factory; 35 import jcckit.util.PropertiesBasedConfigData; 36 37 import java.util.Properties ; 38 39 40 45 public class Legend { 46 47 public static final String UPPER_RIGHT_CORNER_KEY = "upperRightCorner", 48 BOX_WIDTH_KEY = "boxWidth", 49 BOX_HEIGHT_KEY = "boxHeight", 50 BOX_ATTRIBUTES_KEY = "boxAttributes", 51 TITLE_KEY = "title", 52 TITLE_DISTANCE_KEY = "titleDistance", 53 TITLE_ATTRIBUTES_KEY = "titleAttributes", 54 LEFT_DISTANCE_KEY = "leftDistance", 55 BOTTOM_DISTANCE_KEY = "bottomDistance", 56 TOP_DISTANCE_KEY = "topDistance", 57 LINE_LENGTH_KEY = "lineLength", 58 SYMBOL_SIZE_KEY = "symbolSize", 59 CURVE_TITLE_DISTANCE_KEY = "curveTitleDistance", 60 CURVE_TITLE_ATTRIBUTES_KEY 61 = "curveTitleAttributes"; 62 63 private final GraphicalComposite _box; 64 private final TextAttributes _curveTitleAttributes; 65 private final double _xSymbol; 66 private final double _xText; 67 private final double _yBase; 68 private final double _yLastRow; 69 private final double _length; 70 private final double _size; 71 72 134 public Legend(ConfigParameters config) { 135 config = mergeWithDefaultConfig(config); 136 GraphPoint corner 137 = new GraphPoint(config.getDoubleArray(UPPER_RIGHT_CORNER_KEY, 138 new double[] {0.94, 0.54})); 139 double width = config.getDouble(BOX_WIDTH_KEY, 0.2); 140 double height = config.getDouble(BOX_HEIGHT_KEY, 0.1); 141 _curveTitleAttributes = (TextAttributes) Factory.create( 142 config.getNode(CURVE_TITLE_ATTRIBUTES_KEY)); 143 _xSymbol = corner.getX() - width 144 + config.getDouble(LEFT_DISTANCE_KEY, 0.01); 145 _yBase = corner.getY() - config.getDouble(TOP_DISTANCE_KEY, 0.04); 146 _yLastRow = corner.getY() - height 147 + config.getDouble(BOTTOM_DISTANCE_KEY, 0.02); 148 _length = config.getDouble(LINE_LENGTH_KEY, 0.035); 149 _size = config.getDouble(SYMBOL_SIZE_KEY, 0.01); 150 _xText = _xSymbol + _length 151 + config.getDouble(CURVE_TITLE_DISTANCE_KEY, 0.005); 152 153 _box = new GraphicalComposite(null); 154 double xCenter = corner.getX() - width / 2; 155 _box.addElement(new Rectangle( 156 new GraphPoint(xCenter, corner.getY() - height / 2), width, height, 157 (GraphicAttributes) Factory.create( 158 config.getNode(BOX_ATTRIBUTES_KEY)))); 159 _box.addElement(new Text( 160 new GraphPoint(xCenter, corner.getY() 161 - config.getDouble(TITLE_DISTANCE_KEY, 0.005)), 162 config.get(TITLE_KEY, "Legend"), 163 (TextAttributes) Factory.create( 164 config.getNode(TITLE_ATTRIBUTES_KEY)))); 165 } 166 167 private ConfigParameters mergeWithDefaultConfig(ConfigParameters config) { 168 Properties p = new Properties (); 169 p.put(BOX_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 170 ShapeAttributes.class.getName()); 171 p.put(BOX_ATTRIBUTES_KEY + '/' 172 + ShapeAttributes.FILL_COLOR_KEY, "0xffffff"); 173 p.put(BOX_ATTRIBUTES_KEY + '/' 174 + ShapeAttributes.LINE_COLOR_KEY, "0"); 175 p.put(TITLE_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 176 BasicGraphicAttributes.class.getName()); 177 p.put(TITLE_ATTRIBUTES_KEY + '/' 178 + BasicGraphicAttributes.HORIZONTAL_ANCHOR_KEY, "center"); 179 p.put(TITLE_ATTRIBUTES_KEY + '/' 180 + BasicGraphicAttributes.VERTICAL_ANCHOR_KEY, "top"); 181 p.put(CURVE_TITLE_ATTRIBUTES_KEY + '/' + Factory.CLASS_NAME_KEY, 182 BasicGraphicAttributes.class.getName()); 183 ConfigData cd = new PropertiesBasedConfigData(p); 184 cd = new ConfigParametersBasedConfigData(config, new ConfigParameters(cd)); 185 return new ConfigParameters(cd); 186 } 187 188 192 public GraphicalElement getBox() { 193 return _box; 194 } 195 196 208 public GraphicalElement createSymbol(int curveIndex, int numberOfCurves, 209 SymbolFactory factory, 210 boolean withLine, 211 GraphicAttributes lineAttributes) { 212 GraphicalComposite result = new GraphicalComposite(null); 213 double y = calculateBaseLine(curveIndex, numberOfCurves); 214 if (withLine) { 215 Polygon line = new Polygon(lineAttributes, false); 216 line.addPoint(new GraphPoint(_xSymbol, y)); 217 line.addPoint(new GraphPoint(_xSymbol + _length, y)); 218 result.addElement(line); 219 } 220 if (factory != null) { 221 result.addElement(factory.createLegendSymbol( 222 new GraphPoint(_xSymbol + _length / 2, y), _size)); 223 } 224 return result; 225 } 226 227 private double calculateBaseLine(int curveIndex, int numberOfCurves) { 228 if (numberOfCurves > 1) { 229 return _yBase + ((_yLastRow - _yBase) / (numberOfCurves - 1)) 230 * curveIndex; 231 } else { 232 return 0.5 * (_yBase + _yLastRow); 233 } 234 } 235 236 244 public GraphicalElement createCurveTitle(int curveIndex, int numberOfCurves, 245 String title) { 246 return new Text(new GraphPoint(_xText, calculateBaseLine(curveIndex, 247 numberOfCurves)), 248 title, _curveTitleAttributes); 249 } 250 } 251 | Popular Tags |