001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.pm.graphic.chart;
051:
052: /**
053: *
054: */
055:
056: import java.awt.Color;
057: import java.util.HashMap;
058:
059: import org.jfree.chart.JFreeChart;
060: import org.jfree.chart.axis.NumberAxis;
061: import org.jfree.chart.axis.ValueAxis;
062: import org.jfree.chart.labels.StandardXYToolTipGenerator;
063: import org.jfree.chart.plot.DatasetRenderingOrder;
064: import org.jfree.chart.plot.PlotOrientation;
065: import org.jfree.chart.plot.SeriesRenderingOrder;
066: import org.jfree.chart.plot.XYPlot;
067: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
068: import org.jfree.chart.renderer.xy.XYItemRenderer;
069: import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
070: import org.jfree.data.xy.XYDataset;
071: import org.jfree.ui.RectangleInsets;
072:
073: import com.projity.graphic.configuration.shape.Colors;
074: import com.projity.pm.assignment.TimeDistributedConstants;
075: import com.projity.util.Environment;
076:
077: /**
078: * A simple demonstration application showing how to create a vertical bar
079: * chart.
080: *
081: */
082: public class ChartHelper implements TimeDistributedConstants {
083: public static final int BOTTOM_INSET = 7;// replace domain legend
084:
085: public static JFreeChart createChart(final XYDataset dataset,
086: boolean bar, final XYDataset secondDataset) {
087:
088: JFreeChart chart;
089: if (secondDataset != null)
090: chart = createBarLineChart(dataset, secondDataset);
091: else
092: chart = bar ? createBarChart(dataset)
093: : createLineChart(dataset);
094: chart.setAntiAlias(false);// faster
095: chart.setBorderVisible(false);
096: return chart;
097: }
098:
099: /**
100: * Creates a new chart.
101: *
102: * @param dataset
103: * the dataset.
104: *
105: * @return The chart.
106: */
107: public static JFreeChart createBarChart(final XYDataset dataset) {
108: ValueAxis domainAxis = null;
109: NumberAxis axis = new NumberAxis(null);
110: axis.setAutoRangeIncludesZero(false);
111: domainAxis = axis;
112:
113: ValueAxis valueAxis = new NumberAxis(null);
114: XYItemRenderer barRenderer = new XYStepAreaRenderer(
115: XYStepAreaRenderer.AREA,
116: new StandardXYToolTipGenerator(), null);
117:
118: XYPlot plot = new XYPlot(dataset, domainAxis, valueAxis,
119: barRenderer);
120: plot.setOrientation(PlotOrientation.VERTICAL);
121: JFreeChart chart = new JFreeChart(null,
122: JFreeChart.DEFAULT_TITLE_FONT, plot, false);
123: removeAxisAndInsets(chart);
124: return chart;
125: }
126:
127: public static JFreeChart createBarLineChart(
128: final XYDataset barDataset, final XYDataset lineDataset) {
129: JFreeChart chart = createBarChart(barDataset);
130: XYItemRenderer lineRenderer = new StandardXYItemRenderer(
131: StandardXYItemRenderer.LINES);
132: chart.getXYPlot().setDataset(1, lineDataset);
133: chart.getXYPlot().setRenderer(1, lineRenderer);
134: chart.getXYPlot().setSeriesRenderingOrder(
135: SeriesRenderingOrder.FORWARD);
136: chart.getXYPlot().setDatasetRenderingOrder(
137: DatasetRenderingOrder.FORWARD); // draw the line after the bar so it's superimposed
138: return chart;
139: }
140:
141: public static JFreeChart createLineChart(final XYDataset dataset) {
142: NumberAxis xAxis = new NumberAxis(null);
143: xAxis.setAutoRangeIncludesZero(false);
144: NumberAxis yAxis = new NumberAxis(null);
145: XYItemRenderer renderer = new StandardXYItemRenderer(
146: StandardXYItemRenderer.LINES);
147: XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
148: plot.setOrientation(PlotOrientation.VERTICAL);
149: renderer.setToolTipGenerator(new StandardXYToolTipGenerator());
150: JFreeChart chart = new JFreeChart(null,
151: JFreeChart.DEFAULT_TITLE_FONT, plot, false);
152: removeAxisAndInsets(chart);
153: return chart;
154: }
155:
156: public static void removeAxisAndInsets(JFreeChart chart) {
157: XYPlot plot = chart.getXYPlot();
158: removeAxisAndInsets(plot);
159: }
160:
161: public static void removeAxisAndInsets(XYPlot plot) {
162: plot.getRangeAxis().setVisible(false);
163: plot.getDomainAxis().setVisible(false);
164: plot.setDomainGridlinesVisible(false);
165: plot.setInsets(new RectangleInsets(0, 0, BOTTOM_INSET, 0));
166: }
167:
168: private static HashMap map = null;
169:
170: private static HashMap getMap() {
171: if (map == null) {
172: map = new HashMap();
173: map.put(PERCENT_ALLOC, Colors.RED);
174: map.put(OVERALLOCATED, Colors.RED);
175: if (!Environment.getStandAlone())
176: map.put(OTHER_PROJECTS, Colors.GRAY);
177: map.put(AVAILABILITY, Colors.BLACK);
178: map.put(SELECTED, Colors.BLUE);
179: map.put(THIS_PROJECT, Colors.GREEN);
180: map.put(WORK, Colors.RED);
181: map.put(ACTUAL_WORK, Colors.BROWN);
182: map.put(REMAINING_WORK, Colors.PURPLE);
183: map.put(BASELINE_WORK, Colors.DARK_SLATE_GRAY);
184: map.put(COST, Colors.RED);
185: map.put(ACTUAL_COST, Colors.BROWN);
186: map.put(FIXED_COST, Colors.CORAL);
187: map.put(ACTUAL_FIXED_COST, Colors.BURLY_WOOD);
188: map.put(REMAINING_COST, Colors.PURPLE);
189: map.put(BASELINE_COST, Colors.DARK_SLATE_GRAY);
190: map.put(ACWP, Colors.RED);
191: map.put(BCWP, Colors.OLIVE_DRAB);
192: map.put(BCWS, Colors.GOLD);
193: map.put(BASELINE1_WORK, Colors.MAGENTA);
194: map.put(BASELINE2_WORK, Colors.KHAKI);
195: map.put(BASELINE3_WORK, Colors.TAN);
196: map.put(BASELINE4_WORK, Colors.NAVY);
197: map.put(BASELINE5_WORK, Colors.TURQUOISE);
198: map.put(BASELINE6_WORK, Colors.VIOLET);
199: map.put(BASELINE7_WORK, Colors.MAROON);
200: map.put(BASELINE8_WORK, Colors.SALMON);
201: map.put(BASELINE9_WORK, Colors.ORANGE);
202: map.put(BASELINE10_WORK, Colors.CYAN);
203: map.put(BASELINE1_COST, Colors.MAGENTA);
204: map.put(BASELINE2_COST, Colors.KHAKI);
205: map.put(BASELINE3_COST, Colors.TAN);
206: map.put(BASELINE4_COST, Colors.NAVY);
207: map.put(BASELINE5_COST, Colors.TURQUOISE);
208: map.put(BASELINE6_COST, Colors.VIOLET);
209: map.put(BASELINE7_COST, Colors.MAROON);
210: map.put(BASELINE8_COST, Colors.SALMON);
211: map.put(BASELINE9_COST, Colors.ORANGE);
212: map.put(BASELINE10_COST, Colors.CYAN);
213: }
214: return map;
215: }
216:
217: public static Color getColorForField(Object field) {
218: Color result = (Color) getMap().get(field);
219: if (result == null)
220: result = Color.BLACK;
221: return result;
222:
223: }
224:
225: }
|