KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > jfreechart > PieDatasetChartDefinition


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Feb 15, 2006
14  * @author wseyler
15  */

16
17 package org.pentaho.plugin.jfreechart;
18
19 import java.awt.Color JavaDoc;
20 import java.awt.Font JavaDoc;
21 import java.awt.Image JavaDoc;
22 import java.awt.Paint JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.dom4j.Node;
28 import org.jfree.chart.title.TextTitle;
29 import org.jfree.data.UnknownKeyException;
30 import org.jfree.data.general.DefaultPieDataset;
31 import org.jfree.ui.RectangleEdge;
32 import org.pentaho.core.connection.IPentahoDataTypes;
33 import org.pentaho.core.connection.IPentahoResultSet;
34 import org.pentaho.core.connection.PentahoDataTransmuter;
35 import org.pentaho.core.session.IPentahoSession;
36
37 public class PieDatasetChartDefinition extends DefaultPieDataset implements ChartDefinition {
38     /**
39      *
40      */

41     private static final long serialVersionUID = 1L;
42
43     private static final String JavaDoc INTERIOR_GAP_NODE_NAME = "interior-gap"; //$NON-NLS-1$
44

45     private static final String JavaDoc START_ANGLE_NODE_NAME = "start-angle"; //$NON-NLS-1$
46

47     private static final String JavaDoc LABEL_FONT_NODE_NAME = "label-font"; //$NON-NLS-1$
48

49     private static final String JavaDoc LABEL_PAINT_NODE_NAME = "label-paint"; //$NON-NLS-1$
50

51     private static final String JavaDoc LABEL_BACKGROUND_PAINT_NODE_NAME = "label-background-paint"; //$NON-NLS-1$
52

53     private static final String JavaDoc LABEL_GAP_NODE_NAME = "label-gap"; //$NON-NLS-1$
54

55     // JFreeChart Customizations
56
private String JavaDoc title = ""; //$NON-NLS-1$
57

58     private RectangleEdge titlePosition = RectangleEdge.TOP;
59
60     private Font JavaDoc titleFont = TextTitle.DEFAULT_FONT;
61
62     private List JavaDoc subTitles = new ArrayList JavaDoc();
63
64     private Paint JavaDoc chartBackgroundPaint = Color.WHITE;
65
66     private Image JavaDoc chartBackgroundImage = null;
67
68     private boolean borderVisible = false;
69
70     private Paint JavaDoc borderPaint = Color.BLACK;
71
72     private int width = 200;
73
74     private int height = 200;
75
76     // Plot Customizations
77
private Paint JavaDoc plotBackgroundPaint = Color.WHITE;
78
79     private Image JavaDoc plotBackgroundImage = null;
80
81     private Paint JavaDoc[] paintSequence = null;
82
83     private boolean legendIncluded = true;
84
85     private boolean threeD = false;
86     
87     private boolean displayLabels = true;
88
89     // PiePlot Customizations
90
private double interiorGap = 0.25;
91
92     private double startAngle = 90.0;
93
94     private Font JavaDoc labelFont = new Font JavaDoc("SansSerif", Font.PLAIN, 10); //$NON-NLS-1$
95

96     private Paint JavaDoc labelPaint = Color.BLACK;
97
98     private Paint JavaDoc labelBackgroundPaint = new Color JavaDoc(255, 255, 204);
99
100     private double labelGap = 0.10;
101     
102     // Other stuff
103
private IPentahoSession session;
104
105     /**
106      *
107      */

108     public PieDatasetChartDefinition(IPentahoSession session) {
109         super();
110         this.session = session;
111     }
112
113     /**
114      * @param data
115      */

116     public PieDatasetChartDefinition(IPentahoResultSet data, boolean byRow, IPentahoSession session) {
117         this(session);
118         if (byRow) {
119             setDataByRow(data);
120         } else {
121             setDataByColumn(data);
122         }
123     }
124
125     public PieDatasetChartDefinition(IPentahoResultSet data, boolean byRow, Node chartAttributes, IPentahoSession session) {
126         this(data, byRow, session);
127         setChartAttributes(chartAttributes);
128     }
129
130     private void setChartAttributes(Node chartAttributes) {
131         if (chartAttributes == null) {
132             return;
133         }
134         // set the chart background
135
setChartBackground(chartAttributes.selectSingleNode(CHART_BACKGROUND_NODE_NAME));
136
137         // set the plot background
138
setPlotBackground(chartAttributes.selectSingleNode(PLOT_BACKGROUND_NODE_NAME));
139
140         // do we want a legend
141
setLegendIncluded(chartAttributes.selectSingleNode(INCLUDE_LEGEND_NODE_NAME));
142
143         // get the chart title
144
setTitle(chartAttributes.selectSingleNode(TITLE_NODE_NAME));
145
146         // get the chart subtitles
147
addSubTitles(chartAttributes.selectNodes(SUBTITLE_NODE_NAME));
148
149         // get the paint sequence
150
setPaintSequence(chartAttributes.selectSingleNode(PALETTE_NODE_NAME));
151
152         // get the 3D value
153
setThreeD(chartAttributes.selectSingleNode(THREED_NODE_NAME));
154
155         // set the width
156
setWidth(chartAttributes.selectSingleNode(WIDTH_NODE_NAME));
157
158         // set the height
159
setHeight(chartAttributes.selectSingleNode(HEIGHT_NODE_NAME));
160
161         // set the border on or off
162
setBorderVisible(chartAttributes.selectSingleNode(CHART_BORDER_VISIBLE_NODE_NAME));
163
164         // set the border Paint
165
setBorderPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(CHART_BORDER_PAINT_NODE_NAME)));
166
167         // set the title location
168
setTitlePosition(chartAttributes.selectSingleNode(TITLE_POSITION_NODE_NAME));
169
170         // set the title font
171
setTitleFont(chartAttributes.selectSingleNode(TITLE_FONT_NODE_NAME));
172
173         // set the interior gap
174
setInteriorGap(chartAttributes.selectSingleNode(INTERIOR_GAP_NODE_NAME));
175
176         // set the start angle
177
setStartAngle(chartAttributes.selectSingleNode(START_ANGLE_NODE_NAME));
178
179         // set if we want lables
180
setDisplayLabels(chartAttributes.selectSingleNode(DISPLAY_LABELS_NODE_NAME));
181         
182         // set the label font
183
setLabelFont(chartAttributes.selectSingleNode(LABEL_FONT_NODE_NAME));
184
185         // set the label paint
186
setLabelPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(LABEL_PAINT_NODE_NAME)));
187
188         // set the label background paint
189
setLabelBackgroundPaint(JFreeChartEngine.getPaint(chartAttributes.selectSingleNode(LABEL_BACKGROUND_PAINT_NODE_NAME)));
190
191         // set the label gap
192
setLabelGap(chartAttributes.selectSingleNode(LABEL_GAP_NODE_NAME));
193     }
194
195     private void setDataByColumn(IPentahoResultSet data) {
196         setDataByRow(PentahoDataTransmuter.pivot(data));
197     }
198
199     private void setDataByRow(IPentahoResultSet data) {
200         if (data == null) {
201             return; // No data so we've got nothing to set
202
// TODO come up with some sort of error strategy here.
203
}
204         boolean hasColumnHeaders = data.getMetaData().getColumnHeaders() != null;
205         if (!hasColumnHeaders) {
206             data = PentahoDataTransmuter.transmute(data, false);
207         }
208         String JavaDoc[] columnHeaders = null;
209         try {
210             columnHeaders = PentahoDataTransmuter.getCollapsedHeaders(IPentahoDataTypes.AXIS_COLUMN, data, '|');
211         } catch (Exception JavaDoc e) {
212             // should really NEVER get here
213
e.printStackTrace();
214         }
215         int row = 0;
216         if (!hasColumnHeaders) {
217             data.next();
218             row = 1;
219         }
220         Object JavaDoc[] rowData = data.next();
221         while (rowData != null && row < data.getRowCount()+1) {
222             for (int column = 0; column < rowData.length; column++) {
223                 if (rowData[column] instanceof Number JavaDoc) {
224
225                     Number JavaDoc currentNumber = null;
226                     try { // If value has been set then we get it
227
currentNumber = getValue(columnHeaders[column]);
228                     } catch (UnknownKeyException uke) { // else we just set it
229
// to zero
230
currentNumber = new Double JavaDoc(0.0);
231                     }
232                     if (currentNumber == null) {
233                         currentNumber = new Double JavaDoc(0.0);
234                     }
235                     double currentValue = currentNumber.doubleValue();
236
237                     double newValue = ((Number JavaDoc) rowData[column]).doubleValue();
238                     setValue(columnHeaders[column], new Double JavaDoc(newValue + currentValue));
239                 }
240             }
241             rowData = data.next();
242             row++;
243         }
244     }
245
246     public void setHeight(Node heightNode) {
247         if (heightNode != null) {
248             setHeight(Integer.parseInt(heightNode.getText()));
249         }
250     }
251
252     /**
253      * @param height
254      * The height to set.
255      */

256     public void setHeight(int height) {
257         this.height = height;
258     }
259
260     public int getHeight() {
261         return height;
262     }
263
264     public void setWidth(Node widthNode) {
265         if (widthNode != null) {
266             setWidth(Integer.parseInt(widthNode.getText()));
267         }
268     }
269
270     /**
271      * @param width
272      * The width to set.
273      */

274     public void setWidth(int width) {
275         this.width = width;
276     }
277
278     public int getWidth() {
279         return width;
280     }
281
282     public void setTitle(Node chartTitleNode) {
283         if (chartTitleNode != null) {
284             setTitle(chartTitleNode.getText());
285         }
286     }
287
288     /**
289      * @param title
290      * The title to set.
291      */

292     public void setTitle(String JavaDoc title) {
293         this.title = title;
294     }
295
296     public String JavaDoc getTitle() {
297         return title;
298     }
299
300     public void setTitleFont(Font JavaDoc titleFont) {
301         this.titleFont = titleFont;
302     }
303
304     public void setTitleFont(Node titleFontNode) {
305         Font JavaDoc font = JFreeChartEngine.getFont(titleFontNode);
306         if (font != null) {
307             setTitleFont(font);
308         }
309     }
310
311     public Font JavaDoc getTitleFont() {
312         return titleFont;
313     }
314
315     public void addSubTitles(List JavaDoc subTitleNodes) {
316         if (subTitleNodes != null) {
317             Iterator JavaDoc iter = subTitleNodes.iterator();
318             while (iter.hasNext()) {
319                 addSubTitle(((Node) iter.next()).getText());
320             }
321         }
322     }
323
324     public void addSubTitle(String JavaDoc subTitle) {
325         subTitles.add(subTitle);
326     }
327
328     public List JavaDoc getSubtitles() {
329         return subTitles;
330     }
331
332     public void setChartBackground(Node chartBackgroundNode) {
333         if (chartBackgroundNode != null) {
334             Node backgroundTypeNode = chartBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME);
335             if (backgroundTypeNode != null) {
336                 String JavaDoc backgroundTypeStr = backgroundTypeNode.getText();
337                 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
338                     setChartBackgroundPaint(JFreeChartEngine.getPaint(chartBackgroundNode));
339                     setChartBackgroundImage((Image JavaDoc) null);
340                 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
341                     setChartBackgroundImage(chartBackgroundNode);
342                     setChartBackgroundPaint(null);
343                 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
344                     setChartBackgroundPaint(JFreeChartEngine.getTexturePaint(chartBackgroundNode, getWidth(), getHeight(), getSession()));
345                     setChartBackgroundImage((Image JavaDoc) null);
346                 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
347                     setChartBackgroundPaint(JFreeChartEngine.getGradientPaint(chartBackgroundNode, getWidth(), getHeight()));
348                     setChartBackgroundImage((Image JavaDoc) null);
349                 }
350             }
351         }
352     }
353
354     /**
355      * @param backgroundPaint
356      * The backgroundPaint to set.
357      */

358     public void setChartBackgroundPaint(Paint JavaDoc chartBackgroundPaint) {
359         if (chartBackgroundPaint != null) {
360             this.chartBackgroundPaint = chartBackgroundPaint;
361         }
362     }
363
364     public Paint JavaDoc getChartBackgroundPaint() {
365         return chartBackgroundPaint;
366     }
367
368     public void setChartBackgroundImage(Node chartBackgroundImageNode) {
369         setChartBackgroundImage(JFreeChartEngine.getImage(chartBackgroundImageNode, getSession()));
370     }
371
372     /**
373      * @param chartBackgroundImage
374      * The chartBackgroundImage to set.
375      */

376     public void setChartBackgroundImage(Image JavaDoc chartBackgroundImage) {
377         this.chartBackgroundImage = chartBackgroundImage;
378     }
379
380     public Image JavaDoc getChartBackgroundImage() {
381         return chartBackgroundImage;
382     }
383
384     public void setBorderVisible(Node borderVisibleNode) {
385         if (borderVisibleNode != null) {
386             String JavaDoc boolStr = borderVisibleNode.getText();
387             Boolean JavaDoc booleanValue = new Boolean JavaDoc(boolStr);
388             setBorderVisible(booleanValue.booleanValue());
389         }
390     }
391
392     /**
393      * @param borderVisible
394      * The borderVisible to set.
395      */

396     public void setBorderVisible(boolean borderVisible) {
397         this.borderVisible = borderVisible;
398     }
399
400     public boolean isBorderVisible() {
401         return borderVisible;
402     }
403
404     public Paint JavaDoc getBorderPaint() {
405         return borderPaint;
406     }
407
408     /**
409      * @param borderPaint
410      * The borderPaint to set.
411      */

412     public void setBorderPaint(Paint JavaDoc borderPaint) {
413         if (borderPaint != null) {
414             this.borderPaint = borderPaint;
415         }
416     }
417
418     public void setTitlePosition(Node titlePositionNode) {
419         if (titlePositionNode != null) {
420             String JavaDoc titlePositionStr = titlePositionNode.getText();
421             if ("top".equalsIgnoreCase(titlePositionStr)) { //$NON-NLS-1$
422
setTitlePosition(RectangleEdge.TOP);
423             } else if ("left".equalsIgnoreCase(titlePositionStr)) { //$NON-NLS-1$
424
setTitlePosition(RectangleEdge.LEFT);
425             } else if ("bottom".equalsIgnoreCase(titlePositionStr)) { //$NON-NLS-1$
426
setTitlePosition(RectangleEdge.BOTTOM);
427             } else if ("right".equalsIgnoreCase(titlePositionStr)) { //$NON-NLS-1$
428
setTitlePosition(RectangleEdge.RIGHT);
429             }
430         }
431     }
432
433     /**
434      * @param titlePosition
435      * The titlePosition to set.
436      */

437     public void setTitlePosition(RectangleEdge titlePosition) {
438         this.titlePosition = titlePosition;
439     }
440
441     public RectangleEdge getTitlePosition() {
442         return titlePosition;
443     }
444
445     public void setPaintSequence(Node paletteNode) {
446         if (paletteNode != null) {
447             List JavaDoc colorNodes = paletteNode.selectNodes(COLOR_NODE_NAME);
448             Paint JavaDoc[] paints = new Paint JavaDoc[colorNodes.size()];
449             for (int i = 0; i < colorNodes.size(); i++) {
450                 paints[i] = JFreeChartEngine.getPaint((Node) colorNodes.get(i));
451             }
452             setPaintSequence(paints);
453         }
454     }
455
456     /**
457      * @param paintSequence
458      * The paintSequence to set.
459      */

460     public void setPaintSequence(Paint JavaDoc[] paintSequence) {
461         this.paintSequence = paintSequence;
462     }
463
464     public Paint JavaDoc[] getPaintSequence() {
465         return paintSequence;
466     }
467
468     public void setPlotBackground(Node plotBackgroundNode) {
469         if (plotBackgroundNode != null) {
470             Node backgroundTypeNode = plotBackgroundNode.selectSingleNode(BACKGROUND_TYPE_ATTRIBUTE_NAME);
471             if (backgroundTypeNode != null) {
472                 String JavaDoc backgroundTypeStr = backgroundTypeNode.getText();
473                 if (COLOR_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
474                     setPlotBackgroundPaint(JFreeChartEngine.getPaint(plotBackgroundNode));
475                     setPlotBackgroundImage((Image JavaDoc) null);
476                 } else if (IMAGE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
477                     setPlotBackgroundImage(plotBackgroundNode);
478                     setPlotBackgroundPaint(null);
479                 } else if (TEXTURE_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
480                     setPlotBackgroundPaint(JFreeChartEngine.getTexturePaint(plotBackgroundNode, getWidth(), getHeight(), getSession()));
481                     setPlotBackgroundImage((Image JavaDoc) null);
482                 } else if (GRADIENT_TYPE_NAME.equalsIgnoreCase(backgroundTypeStr)) {
483                     setPlotBackgroundPaint(JFreeChartEngine.getGradientPaint(plotBackgroundNode, getWidth(), getHeight()));
484                     setPlotBackgroundImage((Image JavaDoc) null);
485                 }
486             }
487         }
488     }
489
490     public void setPlotBackgroundPaint(Paint JavaDoc plotBackgroundPaint) {
491         if (plotBackgroundPaint != null) {
492             this.plotBackgroundPaint = plotBackgroundPaint;
493         }
494     }
495
496     public Paint JavaDoc getPlotBackgroundPaint() {
497         return plotBackgroundPaint;
498     }
499
500     /**
501      * @param plotBackgroundImage
502      * The plotBackgroundImage to set.
503      */

504     public void setPlotBackgroundImage(Image JavaDoc plotBackgroundImage) {
505         this.plotBackgroundImage = plotBackgroundImage;
506     }
507
508     public void setPlotBackgroundImage(Node plotBackgroundImageNode) {
509         setPlotBackgroundImage(JFreeChartEngine.getImage(plotBackgroundImageNode, getSession()));
510     }
511
512     public Image JavaDoc getPlotBackgroundImage() {
513         return plotBackgroundImage;
514     }
515
516     public void setLegendIncluded(Node legendNode) {
517         if (legendNode != null) {
518             String JavaDoc boolStr = legendNode.getText();
519             Boolean JavaDoc booleanValue = new Boolean JavaDoc(boolStr);
520             setLegendIncluded(booleanValue.booleanValue());
521         }
522     }
523
524     /**
525      * @param legendIncluded
526      * The legendIncluded to set.
527      */

528     public void setLegendIncluded(boolean legendIncluded) {
529         this.legendIncluded = legendIncluded;
530     }
531
532     public boolean isLegendIncluded() {
533         return legendIncluded;
534     }
535
536     public void setThreeD(Node threeDNode) {
537         if (threeDNode != null) {
538             String JavaDoc boolStr = threeDNode.getText();
539             Boolean JavaDoc booleanValue = new Boolean JavaDoc(boolStr);
540             setThreeD(booleanValue.booleanValue());
541         }
542     }
543
544     /**
545      * @param threeD
546      * The threeD to set.
547      */

548     public void setThreeD(boolean threeD) {
549         this.threeD = threeD;
550     }
551
552     public boolean isThreeD() {
553         return threeD;
554     }
555
556     private void setInteriorGap(Node interiorGapNode) {
557         if (interiorGapNode != null) {
558             String JavaDoc gapNodeStr = interiorGapNode.getText();
559             Double JavaDoc doubleValue = new Double JavaDoc(gapNodeStr);
560             setInteriorGap(doubleValue.doubleValue());
561         }
562     }
563
564     /**
565      * @param interiorGap
566      * The interiorGap to set.
567      */

568     public void setInteriorGap(double interiorGap) {
569         this.interiorGap = interiorGap;
570     }
571
572     public double getInteriorGap() {
573         return interiorGap;
574     }
575
576     private void setStartAngle(Node startAngleNode) {
577         if (startAngleNode != null) {
578             String JavaDoc gapNodeStr = startAngleNode.getText();
579             Double JavaDoc doubleValue = new Double JavaDoc(gapNodeStr);
580             setStartAngle(doubleValue.doubleValue());
581         }
582     }
583
584     /**
585      * @param startAngle
586      * The startAngle to set.
587      */

588     public void setStartAngle(double startAngle) {
589         this.startAngle = startAngle;
590     }
591
592     public double getStartAngle() {
593         return startAngle;
594     }
595
596     private void setLabelFont(Node labelFontNode) {
597         Font JavaDoc font = JFreeChartEngine.getFont(labelFontNode);
598         if (font != null) {
599             setLabelFont(font);
600         }
601     }
602
603     public void setLabelFont(Font JavaDoc font) {
604         labelFont = font;
605     }
606
607     public Font JavaDoc getLabelFont() {
608         // TODO Auto-generated method stub
609
return labelFont;
610     }
611
612     /**
613      * @param labelPaint
614      * The labelPaint to set.
615      */

616     public void setLabelPaint(Paint JavaDoc labelPaint) {
617         if (labelPaint != null) {
618             this.labelPaint = labelPaint;
619         }
620     }
621
622     /**
623      * @return Returns the labelPaint.
624      */

625     public Paint JavaDoc getLabelPaint() {
626         return labelPaint;
627     }
628
629     public Paint JavaDoc getLabelBackgroundPaint() {
630         // TODO Auto-generated method stub
631
return labelBackgroundPaint;
632     }
633
634     /**
635      * @param labelBackgroundPaint
636      * The labelBackgroundPaint to set.
637      */

638     public void setLabelBackgroundPaint(Paint JavaDoc labelBackgroundPaint) {
639         if (labelBackgroundPaint != null) {
640             this.labelBackgroundPaint = labelBackgroundPaint;
641         }
642     }
643
644     public double getLabelGap() {
645         return labelGap;
646     }
647
648     /**
649      * @param node
650      * The labelGap to set.
651      */

652     public void setLabelGap(Node labelGapNode) {
653         if (labelGapNode != null) {
654             String JavaDoc gapNodeStr = labelGapNode.getText();
655             Double JavaDoc doubleValue = new Double JavaDoc(gapNodeStr);
656             setLabelGap(doubleValue.doubleValue());
657         }
658     }
659
660     public void setLabelGap(double labelGap) {
661         this.labelGap = labelGap;
662     }
663
664     public boolean isDisplayLabels() {
665         return displayLabels;
666     }
667
668     public void setDisplayLabels(Node threeDNode) {
669         if (threeDNode != null) {
670             String JavaDoc boolStr = threeDNode.getText();
671             Boolean JavaDoc booleanValue = new Boolean JavaDoc(boolStr);
672             setDisplayLabels(booleanValue.booleanValue());
673         }
674     }
675
676     public void setDisplayLabels(boolean displayLabels) {
677         this.displayLabels = displayLabels;
678     }
679
680     public IPentahoSession getSession() {
681         return session;
682     }
683
684     public void setSession(IPentahoSession session) {
685         this.session = session;
686     }
687 }
688
Popular Tags