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: * @(#)Chart.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.extpropertysheet.IExtPropertyGroup;
037: import org.openesb.tools.extpropertysheet.IExtPropertyGroupsBean;
038: import java.awt.Color;
039: import java.util.Map;
040:
041: import java.util.logging.Logger;
042: import org.jfree.chart.JFreeChart;
043: import org.jfree.chart.title.LegendTitle;
044: import org.jfree.chart.title.TextTitle;
045: import org.jfree.ui.HorizontalAlignment;
046: import org.jfree.ui.RectangleEdge;
047:
048: /**
049: *
050: * @author rdwivedi
051: */
052: public abstract class Chart {
053:
054: private static Logger mLogger = Logger.getLogger(Chart.class
055: .getName());
056:
057: abstract public JFreeChart createChart() throws ChartException;
058:
059: abstract public ChartDefaults getChartDefaults();
060:
061: // Chart specific methods common to all
062:
063: protected void setTitle(JFreeChart chart) {
064:
065: ChartDefaults chartProp = getChartDefaults();
066: String title = chartProp.getTitle();
067: if (!chartProp.isDisplayTitle()) {
068: chart.setTitle((String) null);
069: } else {
070: int nHorizonalAlignment = chartProp.getTitleAlignment();
071: HorizontalAlignment horizontalAlignment = null;
072: switch (nHorizonalAlignment) {
073: case ChartDefaults.LEFT:
074: horizontalAlignment = HorizontalAlignment.LEFT; //TextTitle.LEFT;
075: break;
076: case ChartDefaults.RIGHT:
077: horizontalAlignment = HorizontalAlignment.RIGHT; //TextTitle.RIGHT;
078: break;
079: default:
080: horizontalAlignment = HorizontalAlignment.CENTER; //TextTitle.CENTER;
081: }
082: TextTitle textTitle = new TextTitle(title, chartProp
083: .getTitleFont());
084: Color titleColor = chartProp.getTitleColor();
085: if (titleColor != null) {
086: textTitle.setPaint(titleColor);
087: }
088: Color titleBackground = chartProp.getTitleBackground();
089: if (titleBackground != null) {
090: textTitle.setBackgroundPaint(titleBackground);
091: }
092: textTitle.setHorizontalAlignment(horizontalAlignment);
093: chart.setTitle(textTitle);
094: }
095: }
096:
097: /**
098: * Sets legend of the chart basing on chart properties
099: * @param chart - chart
100: */
101: protected void setLegend(JFreeChart chart) {
102: ChartDefaults chartProp = getChartDefaults();
103: if (!chartProp.isIncludeLegend()) {
104: return;
105: }
106: LegendTitle legend = chart.getLegend();
107: if (legend == null) {
108: return;
109: }
110: int anchor = chartProp.getLegendAnchor();
111: switch (anchor) {
112: case ChartDefaults.ANCHOR_NORTH:
113: legend.setPosition(RectangleEdge.TOP);
114: break;
115: case ChartDefaults.ANCHOR_EAST:
116: legend.setPosition(RectangleEdge.RIGHT);
117: break;
118: case ChartDefaults.ANCHOR_WEST:
119: legend.setPosition(RectangleEdge.LEFT);
120: break;
121: default:
122: legend.setPosition(RectangleEdge.BOTTOM);
123: }
124: }
125:
126: /**
127: * Sets border for the chart
128: * @param chart - chart
129: */
130: protected void setBorder(JFreeChart chart) {
131: ChartDefaults chartProp = getChartDefaults();
132: boolean borderVisible = chartProp.isBorderVisible();
133: if (borderVisible) {
134: chart.setBorderVisible(true);
135: Color borderPaint = chartProp.getBorderPaint();
136: if (borderPaint != null) {
137: chart.setBorderPaint(borderPaint);
138: }
139: }
140: }
141:
142: public static Map getAllowedChartsForDatasetType(String dsType) {
143: Map map = null;
144: if (dsType.equals(JFChartConstants.CATEGORY_DATASET)) {
145: map = BasicCategoryDatasetBasedCharts.getAllAllowedCharts();
146:
147: } else if (dsType.equals(JFChartConstants.XY_DATASET)) {
148: map = BasicXYDatasetBasedCharts.getAllAllowedCharts();
149:
150: } else if (dsType.equals(JFChartConstants.VALUE_DATASET)) {
151: map = BasicSingleValueDatasetBasedCharts
152: .getAllAllowedCharts();
153:
154: } else if (dsType.equals(JFChartConstants.PIE_DATASET)) {
155: map = BasicPieDatasetBasedCharts.getAllAllowedCharts();
156:
157: }
158: return map;
159: }
160:
161: public static Chart createChartObject(IExtPropertyGroupsBean pg,
162: DataAccess da) throws ChartException {
163: Chart chart = null;
164: String datasetType = da.getDatasetType();
165: if (datasetType.equals(JFChartConstants.CATEGORY_DATASET)) {
166: chart = new BasicCategoryDatasetBasedCharts(pg, da);
167: } else if (datasetType.equals(JFChartConstants.XY_DATASET)) {
168: chart = new BasicXYDatasetBasedCharts(pg, da);
169: } else if (datasetType
170: .equalsIgnoreCase(JFChartConstants.VALUE_DATASET)) {
171: chart = new BasicSingleValueDatasetBasedCharts(pg, da);
172: } else if (datasetType
173: .equalsIgnoreCase(JFChartConstants.PIE_DATASET)) {
174: chart = new BasicPieDatasetBasedCharts(pg, da);
175: } else {
176: throw new ChartException("Chart Properties are not set.");
177: }
178:
179: return chart;
180: }
181:
182: }
|