1 | package org.expeditee.items.widgets.charts;
|
---|
2 |
|
---|
3 | import java.awt.BasicStroke;
|
---|
4 | import java.awt.Color;
|
---|
5 |
|
---|
6 | import org.expeditee.gui.AttributeUtils;
|
---|
7 | import org.expeditee.gui.AttributeValuePair;
|
---|
8 | import org.expeditee.gui.Frame;
|
---|
9 | import org.expeditee.items.Text;
|
---|
10 | import org.jfree.chart.labels.PieSectionLabelGenerator;
|
---|
11 | import org.jfree.chart.plot.PiePlot;
|
---|
12 | import org.jfree.chart.plot.Plot;
|
---|
13 | import org.jfree.data.general.DefaultPieDataset;
|
---|
14 | import org.jfree.ui.RectangleInsets;
|
---|
15 |
|
---|
16 | public abstract class AbstractPie extends AbstractChart {
|
---|
17 |
|
---|
18 | protected DefaultPieDataset _data;
|
---|
19 |
|
---|
20 | private PieSectionLabelGenerator _labelGenerator;
|
---|
21 |
|
---|
22 | protected DefaultPieDataset getChartData() {
|
---|
23 | if (_data == null)
|
---|
24 | _data = new DefaultPieDataset();
|
---|
25 | return _data;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public AbstractPie(Text source, String[] args) {
|
---|
29 | super(source, args);
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | protected void init() {
|
---|
34 | super.init();
|
---|
35 | _labelGenerator = ((PiePlot) getChart().getPlot()).getLabelGenerator();
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * @param dataFrame
|
---|
40 | */
|
---|
41 | @Override
|
---|
42 | protected void refreshPlot(Frame dataFrame, Plot plot) {
|
---|
43 | if (dataFrame.hasAnnotation("labels")) {
|
---|
44 | ((PiePlot) plot).setLabelGenerator(_labelGenerator);
|
---|
45 | } else {
|
---|
46 | ((PiePlot) plot).setLabelGenerator(null);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void clearData() {
|
---|
52 | _data.clear();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * @param dataFrame
|
---|
57 | */
|
---|
58 | @Override
|
---|
59 | protected void refreshData(Frame dataFrame) {
|
---|
60 | for (Text t : dataFrame.getBodyTextItems(false)) {
|
---|
61 | // Ignore line ends for pie charts
|
---|
62 | if (t.isLineEnd())
|
---|
63 | continue;
|
---|
64 | AttributeValuePair avp = new AttributeValuePair(t.getText());
|
---|
65 | if (avp != null) {
|
---|
66 | try {
|
---|
67 | _data.setValue(avp.getAttribute(), avp.getDoubleValue());
|
---|
68 | _paints.put(avp.getAttribute(), t.getBackgroundColor());
|
---|
69 | } catch (NumberFormatException nfe) {
|
---|
70 |
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public void setSourceBorderColor(Color c) {
|
---|
78 | super.setSourceBorderColor(c);
|
---|
79 | if (getChart() == null)
|
---|
80 | return;
|
---|
81 | PiePlot plot = ((PiePlot) getChart().getPlot());
|
---|
82 | if (c == null) {
|
---|
83 | plot.setBaseSectionOutlinePaint(Plot.DEFAULT_OUTLINE_PAINT);
|
---|
84 | plot.setLabelLinkPaint(PiePlot.DEFAULT_LABEL_OUTLINE_PAINT);
|
---|
85 | plot.setLabelOutlinePaint(PiePlot.DEFAULT_LABEL_OUTLINE_PAINT);
|
---|
86 | plot.setLabelShadowPaint(PiePlot.DEFAULT_LABEL_SHADOW_PAINT);
|
---|
87 | plot.setShadowPaint(PiePlot.DEFAULT_SHADOW_PAINT);
|
---|
88 | } else {
|
---|
89 | plot.setBaseSectionOutlinePaint(c);
|
---|
90 | plot.setLabelLinkPaint(c);
|
---|
91 | plot.setLabelOutlinePaint(c);
|
---|
92 | Color newC = c.darker();
|
---|
93 | Color faded = new Color(newC.getRed(), newC.getGreen(), newC
|
---|
94 | .getBlue(), 128);
|
---|
95 |
|
---|
96 | plot.setLabelShadowPaint(faded);
|
---|
97 | plot.setShadowPaint(c);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public void setSourceColor(Color c) {
|
---|
103 | super.setSourceColor(c);
|
---|
104 | if (getChart() == null)
|
---|
105 | return;
|
---|
106 | PiePlot plot = ((PiePlot) getChart().getPlot());
|
---|
107 | if (c == null) {
|
---|
108 | plot.setLabelPaint(PiePlot.DEFAULT_LABEL_PAINT);
|
---|
109 | } else {
|
---|
110 | plot.setLabelPaint(c);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public void setSourceThickness(float newThickness, boolean setConnected) {
|
---|
116 | super.setSourceThickness(newThickness, setConnected);
|
---|
117 | if (getChart() == null)
|
---|
118 | return;
|
---|
119 | PiePlot plot = ((PiePlot) getChart().getPlot());
|
---|
120 | plot.setBaseSectionOutlineStroke(new BasicStroke(newThickness));
|
---|
121 |
|
---|
122 | plot.setLabelLinkStroke(new BasicStroke(newThickness));
|
---|
123 | plot.setLabelOutlineStroke(new BasicStroke(newThickness));
|
---|
124 |
|
---|
125 | plot.setLabelFont(plot.getLabelFont().deriveFont(
|
---|
126 | getFontSize(newThickness, PiePlot.DEFAULT_LABEL_FONT
|
---|
127 | .getSize2D())));
|
---|
128 | plot.setLabelPadding(new RectangleInsets(newThickness + 1,
|
---|
129 | newThickness + 1, newThickness + 1, newThickness + 1));
|
---|
130 | plot.setLegendItemShape(Plot.getCircle((newThickness + 1) * 2));
|
---|
131 | }
|
---|
132 | }
|
---|