001: package org.pentaho.plugin.jfreereport.reportcharts;
002:
003: import org.jfree.chart.ChartFactory;
004: import org.jfree.chart.JFreeChart;
005: import org.jfree.chart.plot.PlotOrientation;
006: import org.jfree.chart.plot.XYPlot;
007: import org.jfree.data.xy.XYDataset;
008: import org.jfree.data.xy.XYZDataset;
009: import org.jfree.util.Log;
010: import org.pentaho.messages.Messages;
011: import org.pentaho.plugin.jfreechart.BubbleRenderer;
012:
013: public class BubbleChartExpression extends XYChartExpression {
014: private static final long serialVersionUID = 7934486966108227105L;
015:
016: private double maxBubbleSize = 0;
017:
018: private double maxZValue = 1;
019:
020: private String bubbleLabelContent = "{0}: ({1}, {2}, {3})"; //$NON-NLS-1$
021:
022: private String xFormat = ""; //$NON-NLS-1$
023:
024: private String yFormat = ""; //$NON-NLS-1$
025:
026: private String zFormat = ""; //$NON-NLS-1$
027:
028: public BubbleChartExpression() {
029: }
030:
031: public JFreeChart getChart(XYDataset xyDataset) {
032: JFreeChart rtn = null;
033:
034: // Make sure we have a proper dataset
035: ExtendedXYZDataset dataset;
036: if (xyDataset instanceof XYZDataset) {
037: dataset = (ExtendedXYZDataset) xyDataset;
038: maxZValue = dataset.getMaxZValue();
039: } else {
040: return rtn;
041: }
042:
043: rtn = ChartFactory.createBubbleChart(getTitle(),
044: getDomainTitle(), getRangeTitle(), dataset,
045: isHorizontal() ? PlotOrientation.HORIZONTAL
046: : PlotOrientation.VERTICAL, isShowLegend(),
047: false, false);
048:
049: BubbleRenderer renderer = new BubbleRenderer();
050: renderer.setMaxSize(getMaxBubbleSize());
051: renderer.setMaxZ(getMaxZValue());
052: rtn.getXYPlot().setRenderer(renderer);
053:
054: return rtn;
055: }
056:
057: protected void setChartProperties(JFreeChart chart) {
058: super .setChartProperties(chart);
059:
060: XYPlot xypl = chart.getXYPlot();
061: BubbleRenderer renderer = (BubbleRenderer) xypl.getRenderer();
062:
063: /*
064: * We don't handle these in reports yet ...
065: renderer.setToolTipGenerator(new StandardXYZToolTipGenerator(getBubbleLabelContent(),
066: new DecimalFormat(getXFormat()),
067: new DecimalFormat(getYFormat()),
068: new DecimalFormat(getZFormat())));
069:
070: renderer.setURLGenerator(new StandardBubbleURLGenerator());
071: */
072: renderer.setMaxSize(getMaxBubbleSize());
073: renderer.setMaxZ(getMaxZValue());
074: }
075:
076: public Object getValue() {
077:
078: // Because everytime a new data row is processed we end up here, we need to
079: // update the maxZValue, or the value only gets processed the first time through
080: // due to chart caching... Definitely haven't calculated the max value at that point.
081:
082: final Object maybeXYZDatasetCollector = getDataRow().get(
083: getDataSource());
084: if (!(maybeXYZDatasetCollector instanceof XYZSeriesCollectorFunction)) {
085: Log.debug(Messages
086: .getString("XYCHARTEXPRESSION.USER_NOT_A_DATASET")); //$NON-NLS-1$
087: return null;
088: }
089:
090: final XYZSeriesCollectorFunction xyzscf = (XYZSeriesCollectorFunction) maybeXYZDatasetCollector;
091: ExtendedXYZDataset dataset = (ExtendedXYZDataset) xyzscf
092: .getDatasourceValue();
093:
094: if (dataset != null) {
095: maxZValue = dataset.getMaxZValue();
096: }
097:
098: return super .getValue();
099: }
100:
101: public double getMaxZValue() {
102: return maxZValue;
103: }
104:
105: public double getMaxBubbleSize() {
106: return maxBubbleSize;
107: }
108:
109: public void setMaxBubbleSize(double maxBubbleSize) {
110: this .maxBubbleSize = maxBubbleSize;
111: }
112:
113: public String getBubbleLabelContent() {
114: return bubbleLabelContent;
115: }
116:
117: /**
118: * @param bubbleLabelContent
119: * Definition of the bubble labels' content. e.g. "{0}: ({1}, {2}, {3})"
120: * {0} = series name
121: * {1} = x value
122: * {2} = y value
123: * {3} = z value
124: */
125: public void setBubbleLableContent(String bubbleLabelContent) {
126: this .bubbleLabelContent = bubbleLabelContent;
127: }
128:
129: public String getXFormat() {
130: return xFormat;
131: }
132:
133: /**
134: * @param format
135: * Definition of the bubble labels' x value number format.
136: * e.g. "#,##0.0#" or "#,##0.00 EUR" or "##0.00 %"
137: */
138: public void setXFormat(String format) {
139: xFormat = format;
140: }
141:
142: public String getYFormat() {
143: return yFormat;
144: }
145:
146: /**
147: * @param format
148: * Definition of the bubble labels' y value number format.
149: * e.g. "#,##0.0#" or "#,##0.00 EUR" or "##0.00 %"
150: */
151: public void setYFormat(String format) {
152: yFormat = format;
153: }
154:
155: public String getZFormat() {
156: return zFormat;
157: }
158:
159: /**
160: * @param format
161: * Definition of the bubble labels' z value number format.
162: * e.g. "#,##0.0#" or "#,##0.00 EUR" or "##0.00 %"
163: */
164: public void setZFormat(String format) {
165: zFormat = format;
166: }
167:
168: }
|