001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)BasicPieDatasetBasedCharts.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package org.openesb.tools.extchart.jfchart;
030:
031: import org.openesb.tools.extchart.exception.ChartException;
032: import org.openesb.tools.extchart.jfchart.data.DataAccess;
033: import org.openesb.tools.extchart.property.ChartDefaults;
034: import org.openesb.tools.extchart.property.JFChartConstants;
035: import org.openesb.tools.extchart.property.pie.PieConstants;
036: import org.openesb.tools.extchart.property.pie.PieProperties;
037: import org.openesb.tools.extpropertysheet.IExtPropertyGroup;
038: import org.openesb.tools.extpropertysheet.IExtPropertyGroupsBean;
039: import java.awt.Color;
040: import java.awt.Font;
041: import java.text.DecimalFormat;
042: import java.text.NumberFormat;
043: import java.util.HashMap;
044: import java.util.Map;
045: import java.util.Properties;
046: import java.util.logging.Logger;
047: import org.jfree.chart.ChartFactory;
048: import org.jfree.chart.JFreeChart;
049: import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
050: import org.jfree.chart.plot.PiePlot;
051: import org.jfree.chart.plot.PiePlot3D;
052: import org.jfree.data.general.DefaultPieDataset;
053: import org.jfree.data.general.PieDataset;
054: import org.jfree.data.general.ValueDataset;
055: import org.jfree.util.Rotation;
056:
057: /**
058: *
059: * @author rdwivedi
060: */
061: public class BasicPieDatasetBasedCharts extends Chart {
062: private IExtPropertyGroupsBean propGrps = null;
063: private DataAccess mDA = null;
064: private static Logger mLogger = Logger
065: .getLogger(BasicPieDatasetBasedCharts.class.getName());
066:
067: private static String PIE_CHART = "pieChart";
068: private static String RING_CHART = "ringChart";
069:
070: public static Map getAllAllowedCharts() {
071: HashMap map = new HashMap();
072: map.put(PIE_CHART, "Pie Chart");
073: map.put(RING_CHART, "Ring Chart");
074:
075: return map;
076: }
077:
078: /** Creates a new instance of BasicCategoryDatasetBasedCharts */
079: public BasicPieDatasetBasedCharts(IExtPropertyGroupsBean pg,
080: DataAccess da) {
081: propGrps = pg;
082: mDA = da;
083: }
084:
085: public ChartDefaults getChartDefaults() {
086: IExtPropertyGroup p = propGrps
087: .getGroupByName(JFChartConstants.CHART_COMMON_PROPERTIES);
088: return (ChartDefaults) p;
089: }
090:
091: private IExtPropertyGroupsBean getChartPropertiesGroup() {
092: return propGrps;
093: }
094:
095: private IExtPropertyGroup getSpecificProperties() {
096: return getChartPropertiesGroup().getGroupByName(
097: JFChartConstants.CHART_SPECIFIC_PROPERTIES);
098: }
099:
100: public JFreeChart createChart() throws ChartException {
101: PieDataset dataset = null;
102: JFreeChart chart = null;
103: dataset = (PieDataset) mDA.getDataSet();
104: chart = generateChart(dataset);
105:
106: return chart;
107: }
108:
109: private JFreeChart generateChart(PieDataset ds) {
110: JFreeChart chart = null;
111: ChartDefaults p = getChartDefaults();
112: String type = (String) p.getProperty(
113: JFChartConstants.CHART_TYPE).getValue();
114: if (type.equals(PIE_CHART)) {
115: chart = createPieChart(ds);
116: } else if (type.equals(RING_CHART)) {
117: chart = createRingChart(ds);
118: } else {
119: mLogger.info("Chart type" + type + " not supported.");
120: }
121:
122: chart.setBackgroundPaint(this .getChartDefaults()
123: .getBackgroundColor());
124: super .setTitle(chart);
125: super .setLegend(chart);
126: super .setBorder(chart);
127:
128: return chart;
129: }
130:
131: private JFreeChart createRingChart(PieDataset dataset) {
132:
133: PieProperties pieProp = (PieProperties) this
134: .getSpecificProperties();
135: ChartDefaults def = this .getChartDefaults();
136: JFreeChart chart = null;
137: String title = def.getTitle();
138: boolean includeLegend = def.isIncludeLegend();
139: boolean bUseTooltips = true;
140: boolean bDrilldownEnabled = false;
141:
142: chart = ChartFactory.createRingChart(title, dataset,
143: includeLegend, bUseTooltips, bDrilldownEnabled);
144:
145: return chart;
146: }
147:
148: private JFreeChart createPieChart(PieDataset dataset) {
149:
150: PieProperties pieProp = (PieProperties) this
151: .getSpecificProperties();
152: ChartDefaults def = this .getChartDefaults();
153:
154: JFreeChart chart = null;
155: String sNoData = "No Data";
156: Object value = null;
157: boolean bHasData = dataset != null;
158:
159: boolean includeLegend = def.isIncludeLegend() && bHasData;
160: boolean is3D = def.is3D();
161: String title = def.getTitle();
162: PiePlot p = null;
163: DefaultPieDataset singlePieDataset = null;
164: boolean bUseTooltips = true;
165: boolean bDrilldownEnabled = false;
166:
167: String realNoFormat = def.getRealNumberFormat();
168: DecimalFormat formatter = new DecimalFormat(realNoFormat);
169:
170: if (is3D) {
171: chart = ChartFactory.createPieChart3D(title, dataset,
172: includeLegend, bUseTooltips, bDrilldownEnabled);
173: p = (PiePlot) chart.getPlot();
174: if (p instanceof PiePlot3D) {
175: PiePlot3D PiePlot3D = (PiePlot3D) p;
176: PiePlot3D.setDepthFactor(def.getDepthFactor());
177: }
178: //setURLGenerator(p, singlePieDataset);
179: } else {
180: chart = ChartFactory.createPieChart(title, dataset,
181: includeLegend, bUseTooltips, bDrilldownEnabled);
182: p = (PiePlot) chart.getPlot();
183: //setURLGenerator(p, singlePieDataset);
184: }
185:
186: p.setCircular(def.isCircular());
187:
188: StandardPieSectionLabelGenerator standardPieItemLabelGenerator = null;
189: if (bHasData && pieProp.isShowSectionLabels()) {
190: String sLabelFormat = getLabelFormat(pieProp
191: .getSectionLabel());
192: standardPieItemLabelGenerator = new StandardPieSectionLabelGenerator(
193: sLabelFormat, formatter, NumberFormat
194: .getPercentInstance());
195: Font sectionLabelFont = pieProp.getSectionLabelFont();
196: if (sectionLabelFont != null) {
197: p.setLabelFont(sectionLabelFont);
198: }
199: Color sectionLabelPaint = pieProp.getSectionLabelPaint();
200: if (sectionLabelPaint != null) {
201: p.setLabelPaint(sectionLabelPaint);
202: }
203: double sectionLabelGap = pieProp.getSectionLabelGap();
204: sectionLabelGap = Math.min(sectionLabelGap, 1.0d);
205: p.setLabelGap(sectionLabelGap);
206: }
207: p.setLabelGenerator(standardPieItemLabelGenerator);
208:
209: String sTooltipLabelFormat = getLabelFormat(pieProp
210: .getSectionLabel());
211: StandardPieSectionLabelGenerator tooltipGenerator = null;
212: if (bHasData && sTooltipLabelFormat != null) {//tooltips) {
213: tooltipGenerator = new StandardPieSectionLabelGenerator(
214: sTooltipLabelFormat, formatter, NumberFormat
215: .getPercentInstance());
216: }
217:
218: p.setLabelGenerator(tooltipGenerator);
219:
220: double interiorGap = def.getInteriorGap();
221: interiorGap = Math.min(interiorGap, PiePlot.MAX_INTERIOR_GAP);
222: p.setInteriorGap(interiorGap);
223:
224: switch (def.getDirection()) {
225: case ChartDefaults.CLOCKWISE:
226: p.setDirection(Rotation.CLOCKWISE);
227: break;
228: default:
229: p.setDirection(Rotation.ANTICLOCKWISE);
230: }
231:
232: return chart;
233:
234: }
235:
236: String getLabelFormat(int sectionLabel) {
237: String sLabelFormat = null;
238: switch (sectionLabel) {
239: case PieConstants.SECTION_NAME_LABELS:
240: sLabelFormat = "{0}";
241: break;
242: case PieConstants.SECTION_VALUE_LABELS:
243: sLabelFormat = "{1}";
244: break;
245: case PieConstants.SECTION_PERCENT_LABELS:
246: sLabelFormat = "{2}";
247: break;
248: case PieConstants.SECTION_NAME_AND_VALUE_LABELS:
249: sLabelFormat = "{0} = {1}";
250: break;
251: case PieConstants.SECTION_NAME_AND_PERCENT_LABELS:
252: sLabelFormat = "{0} = {2}";
253: break;
254: case PieConstants.SECTION_VALUE_AND_PERCENT_LABELS:
255: sLabelFormat = "{1},{2}";
256: break;
257: case PieConstants.SECTION_NAME_VALUE_AND_PERCENT_LABELS:
258: sLabelFormat = "{0} = {1},{2}";
259: break;
260: case PieConstants.SECTION_NO_LABELS:
261: default:
262: break;
263: }
264: return sLabelFormat;
265: }
266:
267: }
|