01: package com.technoetic.xplanner.charts;
02:
03: import org.jfree.chart.ChartColor;
04: import org.jfree.chart.JFreeChart;
05: import org.jfree.chart.plot.Pie3DPlot;
06: import org.jfree.chart.plot.PiePlot;
07: import de.laures.cewolf.ChartPostProcessor;
08: import de.laures.cewolf.taglib.util.ColorHelper;
09:
10: import java.awt.*;
11: import java.util.Iterator;
12: import java.util.Map;
13:
14: public class PieChartPostProcessor implements ChartPostProcessor {
15: private static Paint[] DEFAULT_PAINTLIST = {
16: ChartColor.VERY_LIGHT_RED, ChartColor.VERY_LIGHT_BLUE,
17: ChartColor.VERY_LIGHT_GREEN, ChartColor.VERY_LIGHT_YELLOW,
18: ChartColor.VERY_LIGHT_MAGENTA, ChartColor.VERY_LIGHT_CYAN,
19: ChartColor.LIGHT_RED, ChartColor.LIGHT_BLUE,
20: ChartColor.LIGHT_GREEN, ChartColor.LIGHT_YELLOW,
21: ChartColor.LIGHT_MAGENTA, ChartColor.LIGHT_CYAN,
22: Color.lightGray, Color.red, Color.blue, Color.green,
23: Color.yellow, Color.orange, Color.magenta, Color.cyan,
24: Color.pink, Color.gray, ChartColor.DARK_RED,
25: ChartColor.DARK_BLUE, ChartColor.DARK_GREEN,
26: ChartColor.DARK_YELLOW, ChartColor.DARK_MAGENTA,
27: ChartColor.DARK_CYAN, Color.darkGray,
28: ChartColor.VERY_DARK_RED, ChartColor.VERY_DARK_BLUE,
29: ChartColor.VERY_DARK_GREEN, ChartColor.VERY_DARK_YELLOW,
30: ChartColor.VERY_DARK_MAGENTA, ChartColor.VERY_DARK_CYAN, };
31:
32: public void processChart(Object chart, Map parameters) {
33: PiePlot plot = (PiePlot) ((JFreeChart) chart).getPlot();
34: setPlotDefaults(plot);
35: for (Iterator iterator = parameters.keySet().iterator(); iterator
36: .hasNext();) {
37: String key = (String) iterator.next();
38: if (key.equals("border.color")) {
39: plot.setOutlinePaint(parseColor(parameters.get(key)));
40: } else if (key.startsWith("section.exploded")
41: && isTrue(parameters.get(key))) {
42: plot.setRadius(0.90);
43: plot.setExplodePercent(getIndex(key), 1.0);
44: } else if (key.startsWith("depth")
45: && isTrue(parameters.get(key))) {
46: if (plot instanceof Pie3DPlot) {
47: ((Pie3DPlot) plot)
48: .setDepthFactor(Double
49: .parseDouble(parameters.get(key)
50: .toString()));
51: }
52: } else if (key.startsWith("section.color")) {
53: plot.setPaint(getIndex(key), parseColor(parameters
54: .get(key)));
55: }
56: }
57: }
58:
59: private void setPlotDefaults(PiePlot plot) {
60: for (int i = 0; i < DEFAULT_PAINTLIST.length; i++) {
61: plot.setPaint(i, DEFAULT_PAINTLIST[i]);
62: }
63: plot.setOutlinePaint(Color.WHITE);
64: if (plot instanceof Pie3DPlot) {
65: ((Pie3DPlot) plot).setDepthFactor(0.2);
66: }
67: }
68:
69: private boolean isTrue(Object o) {
70: return Boolean.valueOf(o.toString()).booleanValue();
71: }
72:
73: private int getIndex(String key) {
74: try {
75: return Integer.parseInt(key
76: .substring(key.lastIndexOf(".") + 1));
77: } catch (NumberFormatException e) {
78: return 0;
79: }
80: }
81:
82: private Paint parseColor(Object value) {
83: return ColorHelper.getColor(value.toString());
84: }
85: }
|