1 17 18 package org.pentaho.plugin.jfreechart; 19 20 import org.dom4j.Document; 21 import org.dom4j.Element; 22 import org.dom4j.Node; 23 import org.jfree.chart.plot.DialShape; 24 import org.jfree.chart.plot.MeterInterval; 25 import org.jfree.data.Range; 26 import org.jfree.ui.RectangleEdge; 27 import org.pentaho.core.connection.IPentahoResultSet; 28 import org.pentaho.core.connection.PentahoDataTransmuter; 29 import org.pentaho.core.session.IPentahoSession; 30 31 import java.awt.BasicStroke ; 32 import java.awt.Color ; 33 import java.awt.Font ; 34 import java.awt.Image ; 35 import java.awt.Paint ; 36 import java.awt.Stroke ; 37 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 41 72 public class DialWidgetDefinition extends WidgetDefinition implements ChartDefinition { 73 74 77 private static final long serialVersionUID = 2232742163326878608L; 78 79 private ArrayList intervals = new ArrayList (); 80 81 private RectangleEdge titlePosition = RectangleEdge.TOP; 82 83 private Paint chartBackgroundPaint = Color.WHITE; 84 85 private Paint plotBackgroundPaint = Color.GRAY; 86 87 private Paint needlePaint = Color.blue; 88 89 private DialShape dialShape = DialShape.CHORD; 90 91 private Font titleFont; 92 93 private List subTitles = new ArrayList (); 94 95 private boolean rangeLimited; 96 97 private int tickSize = 5; 98 99 private Paint tickPaint = Color.blue; 100 101 private Paint valuePaint = Color.BLUE; 102 103 private Font valueFont; 104 105 private String units; 106 107 109 public DialWidgetDefinition(double value, double minimum, double maximum, boolean rangeLimited) { 110 super(value, minimum, maximum); 111 this.rangeLimited = rangeLimited; 112 } 113 114 public DialWidgetDefinition(IPentahoResultSet data, boolean byRow, Node chartAttributes, int width, int height, IPentahoSession session) { 115 this(0.0, Double.MIN_VALUE, Double.MAX_VALUE, false); 116 117 if (byRow) { 118 setDataByRow(data); 119 } else { 120 setDataByColumn(data); 121 } 122 123 createDial( this, chartAttributes, width, height, session ); 124 } 125 126 public DialWidgetDefinition( Document document, double value, int width, int height, IPentahoSession session) { 127 this(value, Double.MIN_VALUE, Double.MAX_VALUE, false); 128 129 Node dialNode = document.selectSingleNode("//dial"); 132 double min = 0; 133 double max = 100; 134 Node node = dialNode.selectSingleNode("range-limited"); rangeLimited = (node == null) || ("true".equalsIgnoreCase(node.getText())); 137 if (!rangeLimited) { 138 } else { 139 max = 0.1; 140 double absValue = Math.abs(value); 141 while (max < absValue) { 144 max *= 2; 145 if (max < absValue) { 146 min *= 2.5; 147 max *= 2.5; 148 } 149 if (max < absValue) { 150 min *= 2; 151 max *= 2; 152 } 153 } 154 if (value > 0) { 155 min = 0; 156 } else { 157 min = -max; 158 } 159 setMaximum( max ); 160 setMinimum( min ); 161 } 162 createDial( this, dialNode, width, height, session ); 164 } 165 166 171 178 public static void createDial(DialWidgetDefinition widgetDefinition, Node dialNode, int width, int height, IPentahoSession session) { 179 180 Node node = dialNode.selectSingleNode("units"); if( node != null ) { 182 String units = node.getText(); 183 widgetDefinition.setUnits( units ); 184 } 185 186 Paint paint = JFreeChartEngine.getPaint(dialNode.selectSingleNode("background-color")); if (paint == null) { 189 Element backgroundNode = (Element) dialNode.selectSingleNode("chart-background"); if (backgroundNode != null) { 191 String backgroundType = backgroundNode.attributeValue("type"); if ("texture".equals(backgroundType)) { paint = JFreeChartEngine.getTexturePaint(backgroundNode, width, height, session); 194 } else if ("gradient".equals(backgroundType)) { paint = JFreeChartEngine.getGradientPaint(backgroundNode, width, height); 196 } 197 } 198 } 199 200 if (paint != null) { 201 widgetDefinition.setChartBackgroundPaint(paint); 202 } 203 204 paint = JFreeChartEngine.getPaint(dialNode.selectSingleNode("plot-background-color")); if (paint == null) { 207 Element backgroundNode = (Element) dialNode.selectSingleNode("plot-background"); if (backgroundNode != null) { 209 String backgroundType = backgroundNode.attributeValue("type"); if ("texture".equals(backgroundType)) { paint = JFreeChartEngine.getTexturePaint(backgroundNode, width, height, session); 212 } else if ("gradient".equals(backgroundType)) { paint = JFreeChartEngine.getGradientPaint(backgroundNode, width, height); 214 } 215 } 216 } 217 if (paint != null) { 218 widgetDefinition.setPlotBackgroundPaint(paint); 219 } 220 221 paint = JFreeChartEngine.getPaint(dialNode.selectSingleNode("needle-color")); if (paint != null) { 224 widgetDefinition.setNeedlePaint(paint); 225 } 226 227 paint = JFreeChartEngine.getPaint(dialNode.selectSingleNode("tick-color")); if (paint != null) { 230 widgetDefinition.setTickPaint(paint); 231 } 232 233 Node tmpNode = dialNode.selectSingleNode("tick-interval"); if (tmpNode != null) { 235 widgetDefinition.setTickSize(Integer.parseInt(dialNode.selectSingleNode("tick-interval").getText())); } 237 238 paint = JFreeChartEngine.getPaint(dialNode.selectSingleNode("value-color")); if (paint != null) { 241 widgetDefinition.setValuePaint(paint); 242 } 243 244 widgetDefinition.setDialShape(DialShape.CHORD); 246 247 Node titleFontNode = dialNode.selectSingleNode("title-font"); if (titleFontNode != null) { 249 String titleFontStr = titleFontNode.getText().trim(); 250 if (!"".equals(titleFontStr)) { widgetDefinition.setTitleFont(new Font (titleFontStr, Font.ITALIC, 24)); 252 } 253 } 254 255 Node valueFontNode = dialNode.selectSingleNode("title-font"); if (valueFontNode != null) { 257 String fontStr = valueFontNode.getText().trim(); 258 if (!"".equals(fontStr)) { widgetDefinition.setValueFont(new Font (fontStr, Font.ITALIC, 24)); 260 } 261 } 262 263 List intervals = dialNode.selectNodes("interval"); Iterator intervalIterator = intervals.iterator(); 266 while (intervalIterator.hasNext()) { 267 Node intervalNode = (Node) intervalIterator.next(); 269 270 String label = intervalNode.selectSingleNode("label").getText(); 273 double minimum = Double.parseDouble(intervalNode.selectSingleNode("minimum").getText()); double maximum = Double.parseDouble(intervalNode.selectSingleNode("maximum").getText()); Range range = new Range(minimum, maximum); 277 278 Paint backgroundPaint = JFreeChartEngine.getPaint(intervalNode.selectSingleNode("color")); if (backgroundPaint == null) { 280 Element backgroundNode = (Element) intervalNode.selectSingleNode("interval-background"); if (backgroundNode != null) { 282 String backgroundType = backgroundNode.attributeValue("type"); if ("texture".equals(backgroundType)) { backgroundPaint = JFreeChartEngine.getTexturePaint(backgroundNode, width, height, session); 285 } else if ("gradient".equals(backgroundType)) { backgroundPaint = JFreeChartEngine.getGradientPaint(backgroundNode, width, height); 287 } 288 } 289 } 290 291 String textColor = intervalNode.selectSingleNode("text-color").getText(); Stroke outlineStroke; 294 if (intervalNode.selectSingleNode("stroke-width") != null) { outlineStroke = new BasicStroke (Float.parseFloat(intervalNode.selectSingleNode("stroke-width").getText())); } else { 297 outlineStroke = new BasicStroke (); 298 } 299 Paint outlinePaint = JFreeChartEngine.getPaint(textColor); 300 301 MeterInterval interval = new MeterInterval(label, range, outlinePaint, outlineStroke, backgroundPaint); 303 304 widgetDefinition.addInterval(interval); 306 } 307 308 } 309 310 public void setUnits( String units ) { 311 this.units = units; 312 } 313 314 public String getUnits( ) { 315 return units; 316 } 317 318 private void setDataByColumn(IPentahoResultSet data) { 319 setDataByRow(PentahoDataTransmuter.pivot(data)); 320 } 321 322 private void setDataByRow(IPentahoResultSet data) { 323 Object [] rowData = data.next(); 324 325 double newValue = ((Number ) rowData[0]).doubleValue(); 326 double newMinimum = ((Number ) rowData[1]).doubleValue(); 327 double newMaximum = ((Number ) rowData[2]).doubleValue(); 328 this.setValue(newValue); 329 this.setMinimum(newMinimum); 330 this.setMaximum(newMaximum); 331 } 332 333 347 public void addInterval(MeterInterval interval) { 348 intervals.add(interval); 349 Range range = interval.getRange(); 350 double min = range.getLowerBound(); 351 double max = range.getUpperBound(); 352 if (rangeLimited && intervals.size() == 1) { 353 setMinimum(min); 354 setMaximum(max); 355 } else { 356 if (min < getMinimum()) { 357 setMinimum(min); 358 } 359 if (max > getMaximum()) { 360 setMaximum(max); 361 } 362 } 363 } 364 365 371 372 public void setValue(double value) { 373 setValue(new Double (value)); 374 if (rangeLimited) { 375 if (value < getMinimum()) { 376 setValue(getMinimum()); 377 } else if (value > getMaximum()) { 378 setValue(getMaximum()); 379 } 380 } else { 381 if (value < getMinimum()) { 382 setMinimum(value); 383 } else if (value > getMaximum()) { 384 setMaximum(value); 385 } 386 } 387 } 388 389 395 public Paint getPlotBackgroundPaint() { 396 return plotBackgroundPaint; 397 } 398 399 406 public void setPlotBackgroundPaint(Paint plotBackgroundPaint) { 407 this.plotBackgroundPaint = plotBackgroundPaint; 408 } 409 410 415 public Paint getNeedlePaint() { 416 return needlePaint; 417 } 418 419 427 public void setNeedlePaint(Paint needlePaint) { 428 this.needlePaint = needlePaint; 429 } 430 431 436 public DialShape getDialShape() { 437 return dialShape; 438 } 439 440 445 public Font getTitleFont() { 446 if (titleFont != null) { 447 return titleFont; 448 } else { 449 return new Font ("sans-serif", Font.PLAIN, 14); } 451 } 452 453 public void setTitleFont(Font tFont) { 454 titleFont = tFont; 455 456 } 457 458 476 public void setDialShape(DialShape dialShape) { 477 this.dialShape = dialShape; 478 } 479 480 486 487 public List getIntervals() { 488 return intervals; 489 } 490 491 public Paint [] getPaintSequence() { 492 return null; 493 } 494 495 public Image getPlotBackgroundImage() { 496 return null; 497 } 498 499 public List getSubtitles() { 500 return subTitles; 501 } 502 503 public Paint getChartBackgroundPaint() { 504 return chartBackgroundPaint; 506 } 507 508 public Image getChartBackgroundImage() { 509 return null; 511 } 512 513 public boolean isBorderVisible() { 514 return false; 516 } 517 518 public Paint getBorderPaint() { 519 return null; 521 } 522 523 public RectangleEdge getTitlePosition() { 524 return titlePosition; 525 } 526 527 531 public void setChartBackgroundPaint(Paint chartBackgroundPaint) { 532 this.chartBackgroundPaint = chartBackgroundPaint; 533 } 534 535 public int getHeight() { 536 return 200; 538 } 539 540 public int getWidth() { 541 return 200; 543 } 544 545 public String getTitle() { 546 return null; 547 } 548 549 public boolean isLegendIncluded() { 550 return false; 552 } 553 554 public boolean isThreeD() { 555 return false; 557 } 558 559 public Paint getValuePaint() { 560 return valuePaint; 561 } 562 563 public Paint getTickPaint() { 564 return tickPaint; 565 } 566 567 public int getTickSize() { 568 return tickSize; 569 } 570 571 public void setValuePaint(Paint valuePaint) { 572 this.valuePaint = valuePaint; 573 } 574 575 public void setTickPaint(Paint tickPaint) { 576 this.tickPaint = tickPaint; 577 } 578 579 public void setTickSize(int tickSize) { 580 this.tickSize = tickSize; 581 } 582 583 public Font getValueFont() { 584 return valueFont; 585 } 586 587 public void setValueFont(Font valueFont) { 588 this.valueFont = valueFont; 589 } 590 591 public boolean isDisplayLabels() { 592 return false; 594 } 595 } 596 | Popular Tags |