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: * @(#)BasicSingleValueDatasetBasedCharts.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.ChartConstants;
034: import org.openesb.tools.extchart.property.ChartDefaults;
035: import org.openesb.tools.extchart.property.JFChartConstants;
036: import org.openesb.tools.extchart.property.meter.MeterProperties;
037: import org.openesb.tools.extpropertysheet.IExtPropertyGroup;
038: import org.openesb.tools.extpropertysheet.IExtPropertyGroupsBean;
039: import com.sun.web.ui.component.Property;
040: import java.awt.BasicStroke;
041: import java.awt.Color;
042: import java.awt.Font;
043: import java.awt.Paint;
044: import java.text.DecimalFormat;
045: import java.util.HashMap;
046: import java.util.Map;
047: import java.util.Properties;
048: import java.util.logging.Logger;
049: import org.jfree.chart.JFreeChart;
050: import org.jfree.chart.plot.DialShape;
051: import org.jfree.chart.plot.MeterInterval;
052: import org.jfree.chart.plot.MeterPlot;
053: import org.jfree.chart.plot.ThermometerPlot;
054: import org.jfree.data.Range;
055: import org.jfree.data.general.ValueDataset;
056: import org.jfree.data.xy.XYDataset;
057: import org.jfree.ui.RectangleInsets;
058:
059: /**
060: *
061: * @author rdwivedi
062: */
063: public class BasicSingleValueDatasetBasedCharts extends Chart {
064:
065: private IExtPropertyGroupsBean propGrps = null;
066: private DataAccess mDA = null;
067: private static Logger mLogger = Logger
068: .getLogger(BasicSingleValueDatasetBasedCharts.class
069: .getName());
070: private static String METER_CHART = "meterChart";
071: private static String THERMO_CHART = "thermoChart";
072:
073: public static Map getAllAllowedCharts() {
074: HashMap map = new HashMap();
075: map.put(METER_CHART, "Meter Chart");
076: map.put(THERMO_CHART, "Thermo Chart");
077:
078: return map;
079: }
080:
081: /** Creates a new instance of BasicCategoryDatasetBasedCharts */
082: public BasicSingleValueDatasetBasedCharts(
083: IExtPropertyGroupsBean pg, DataAccess da) {
084: propGrps = pg;
085: mDA = da;
086: }
087:
088: public ChartDefaults getChartDefaults() {
089: IExtPropertyGroup p = propGrps
090: .getGroupByName(JFChartConstants.CHART_COMMON_PROPERTIES);
091: return (ChartDefaults) p;
092: }
093:
094: private IExtPropertyGroupsBean getChartPropertiesGroup() {
095: return propGrps;
096: }
097:
098: private IExtPropertyGroup getSpecificProperties() {
099: return getChartPropertiesGroup().getGroupByName(
100: JFChartConstants.CHART_SPECIFIC_PROPERTIES);
101: }
102:
103: public JFreeChart createChart() throws ChartException {
104: ValueDataset dataset = null;
105: JFreeChart chart = null;
106: dataset = (ValueDataset) mDA.getDataSet();
107: chart = generateChart(dataset);
108:
109: return chart;
110: }
111:
112: private JFreeChart generateChart(ValueDataset ds) {
113: JFreeChart chart = null;
114: ChartDefaults p = getChartDefaults();
115: String type = (String) p.getProperty(
116: JFChartConstants.CHART_TYPE).getValue();
117: if (type.equals(METER_CHART)) {
118: chart = createMeterChart(ds);
119: } else if (type.equals(THERMO_CHART)) {
120: chart = createThermoChart(ds);
121: } else {
122: mLogger.info("Chart type" + type + " not supported.");
123: }
124:
125: chart.setBackgroundPaint(this .getChartDefaults()
126: .getBackgroundColor());
127: super .setTitle(chart);
128: super .setLegend(chart);
129: super .setBorder(chart);
130:
131: return chart;
132: }
133:
134: private JFreeChart createMeterChart(ValueDataset ds) {
135: JFreeChart chart = null;
136:
137: MeterPlot plot = null;
138:
139: MeterProperties meterProp = (MeterProperties) getSpecificProperties();
140: ChartDefaults def = this .getChartDefaults();
141: plot = new MeterPlot(ds);
142: DecimalFormat df = new DecimalFormat(def.getRealNumberFormat());
143: plot.setTickLabelFormat(df);
144: plot.setUnits(meterProp.getUnits());
145: Range range = new Range(meterProp.getMinimumValue(), meterProp
146: .getMaximumValue());
147: plot.setRange(range);
148:
149: Range normalRange = new Range(
150: meterProp.getMinimumNormalValue(), meterProp
151: .getMaximumNormalValue());
152: MeterInterval normal = new MeterInterval("Normal", normalRange,
153: meterProp.getNormalPaint(), new BasicStroke(2.0f), null);
154: plot.addInterval(normal);
155:
156: Range warningRange = new Range(meterProp
157: .getMinimumWarningValue(), meterProp
158: .getMaximumWarningValue());
159: MeterInterval warning = new MeterInterval("Warning",
160: warningRange, meterProp.getWarningPaint(),
161: new BasicStroke(2.0f), null);
162: plot.addInterval(warning);
163:
164: Range criticalRange = new Range(meterProp
165: .getMinimumCriticalValue(), meterProp
166: .getMaximumCriticalValue());
167: MeterInterval critical = new MeterInterval("Critical",
168: criticalRange, meterProp.getCriticalPaint(),
169: new BasicStroke(2.0f), null);
170:
171: plot.addInterval(critical);
172:
173: double maxValue = Math.max(meterProp.getMaximumCriticalValue(),
174: meterProp.getMaximumValue());
175:
176: int dialType = meterProp.getDialType();
177: switch (dialType) {
178: case MeterProperties.DIALTYPE_PIE:
179: plot.setDialShape(DialShape.PIE);
180: break;
181: case MeterProperties.DIALTYPE_CHORD:
182: plot.setDialShape(DialShape.CHORD);
183: break;
184: default:
185: plot.setDialShape(DialShape.CIRCLE);
186: }
187:
188: int tickLabelType = meterProp.getTickLabelType();
189: if (tickLabelType == MeterProperties.TICK_NO_LABELS) {
190: plot.setTickLabelsVisible(false);
191: }
192:
193: Font tickLabelFont = meterProp.getTickLabelFont();
194: if (tickLabelFont != null) {
195: plot.setTickLabelFont(tickLabelFont);
196: }
197:
198: plot.setDrawBorder(meterProp.isDrawBorder());
199: plot.setDialBackgroundPaint(meterProp.getDialBackgroundPaint());
200: plot
201: .setDialOutlinePaint((Paint) meterProp
202: .getDialBorderColor());
203: plot.setNeedlePaint(meterProp.getNeedlePaint());
204: plot.setValuePaint(meterProp.getValuePaint());
205: //plot.setMeterAngle(meterProp.getMeterAngle());
206: Font valueFont = meterProp.getValueFont();
207: if (valueFont != null) {
208: plot.setValueFont(valueFont);
209: }
210:
211: plot.setInsets(new RectangleInsets(5, 5, 5, 5));
212: String title = null;
213: String sNoData = "No Data";
214:
215: chart = new JFreeChart(title, def.getTitleFont(), plot, def
216: .isIncludeLegend());
217: return chart;
218: }
219:
220: private JFreeChart createThermoChart(ValueDataset ds) {
221: MeterProperties meterProp = (MeterProperties) getSpecificProperties();
222: ChartDefaults def = this .getChartDefaults();
223: boolean includeLegend = def.isIncludeLegend();
224: boolean is3D = def.is3D();
225: String title = def.getTitle();
226: boolean bUseTooltips = true;
227: boolean bDrilldownEnabled = false;
228: ThermometerPlot plot = new ThermometerPlot(ds);
229: JFreeChart chart = new JFreeChart("Thermometer Demo 1", // chart title
230: JFreeChart.DEFAULT_TITLE_FONT, plot, // plot
231: false // no legend
232: );
233:
234: // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
235: plot.setInsets(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
236: plot.setThermometerStroke(new BasicStroke(2.0f));
237: plot.setThermometerPaint(Color.lightGray);
238: // OPTIONAL CUSTOMISATION COMPLETED.
239:
240: return chart;
241: }
242:
243: }
|