1 | /**
|
---|
2 | * AbstractPie.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items.widgets.charts;
|
---|
20 |
|
---|
21 | import java.awt.BasicStroke;
|
---|
22 |
|
---|
23 | import org.expeditee.core.Colour;
|
---|
24 | import org.expeditee.gio.swing.SwingConversions;
|
---|
25 | import org.expeditee.gui.AttributeValuePair;
|
---|
26 | import org.expeditee.gui.Frame;
|
---|
27 | import org.expeditee.items.Text;
|
---|
28 | import org.jfree.chart.labels.PieSectionLabelGenerator;
|
---|
29 | import org.jfree.chart.plot.PiePlot;
|
---|
30 | import org.jfree.chart.plot.Plot;
|
---|
31 | import org.jfree.data.general.DefaultPieDataset;
|
---|
32 | import org.jfree.ui.RectangleInsets;
|
---|
33 |
|
---|
34 | public abstract class AbstractPie extends AbstractChart {
|
---|
35 |
|
---|
36 | protected DefaultPieDataset _data;
|
---|
37 |
|
---|
38 | private PieSectionLabelGenerator _labelGenerator;
|
---|
39 |
|
---|
40 | protected DefaultPieDataset getChartData() {
|
---|
41 | if (_data == null)
|
---|
42 | _data = new DefaultPieDataset();
|
---|
43 | return _data;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public AbstractPie(Text source, String[] args) {
|
---|
47 | super(source, args);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void init() {
|
---|
52 | super.init();
|
---|
53 | _labelGenerator = ((PiePlot) getChart().getPlot()).getLabelGenerator();
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * @param dataFrame
|
---|
58 | */
|
---|
59 | @Override
|
---|
60 | protected void refreshPlot(Frame dataFrame, Plot plot) {
|
---|
61 | if (dataFrame.hasAnnotation("labels")) {
|
---|
62 | ((PiePlot) plot).setLabelGenerator(_labelGenerator);
|
---|
63 | } else {
|
---|
64 | ((PiePlot) plot).setLabelGenerator(null);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | protected void clearData() {
|
---|
70 | _data.clear();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * @param dataFrame
|
---|
75 | */
|
---|
76 | @Override
|
---|
77 | protected void refreshData(Frame dataFrame) {
|
---|
78 | for (Text t : dataFrame.getBodyTextItems(false)) {
|
---|
79 | // Ignore line ends for pie charts
|
---|
80 | //TODO find out HOW text became null when i was doing a graph!!
|
---|
81 | String text = t.getText();
|
---|
82 | if (t.isLineEnd() || text == null)
|
---|
83 | continue;
|
---|
84 | AttributeValuePair avp = new AttributeValuePair(text);
|
---|
85 | if (avp != null && avp.hasPair()) {
|
---|
86 | try {
|
---|
87 | _data.setValue(avp.getAttribute(), avp.getDoubleValue());
|
---|
88 | _paints.put(avp.getAttribute(), SwingConversions.toSwingColor(t.getBackgroundColor()));
|
---|
89 | } catch (NumberFormatException nfe) {
|
---|
90 |
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void setSourceBorderColor(Colour c) {
|
---|
98 | super.setSourceBorderColor(c);
|
---|
99 | if (getChart() == null)
|
---|
100 | return;
|
---|
101 | PiePlot plot = ((PiePlot) getChart().getPlot());
|
---|
102 | if (c == null) {
|
---|
103 | plot.setBaseSectionOutlinePaint(Plot.DEFAULT_OUTLINE_PAINT);
|
---|
104 | plot.setLabelLinkPaint(PiePlot.DEFAULT_LABEL_OUTLINE_PAINT);
|
---|
105 | plot.setLabelOutlinePaint(PiePlot.DEFAULT_LABEL_OUTLINE_PAINT);
|
---|
106 | plot.setLabelShadowPaint(PiePlot.DEFAULT_LABEL_SHADOW_PAINT);
|
---|
107 | plot.setShadowPaint(PiePlot.DEFAULT_SHADOW_PAINT);
|
---|
108 | } else {
|
---|
109 | plot.setBaseSectionOutlinePaint(SwingConversions.toSwingColor(c));
|
---|
110 | plot.setLabelLinkPaint(SwingConversions.toSwingColor(c));
|
---|
111 | plot.setLabelOutlinePaint(SwingConversions.toSwingColor(c));
|
---|
112 | Colour newC = c.darker();
|
---|
113 | Colour faded = new Colour(newC.getRed(), newC.getGreen(), newC
|
---|
114 | .getBlue(), Colour.FromComponent255(128));
|
---|
115 |
|
---|
116 | plot.setLabelShadowPaint(SwingConversions.toSwingColor(faded));
|
---|
117 | plot.setShadowPaint(SwingConversions.toSwingColor(c));
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public void setSourceColor(Colour c) {
|
---|
123 | super.setSourceColor(c);
|
---|
124 | if (getChart() == null)
|
---|
125 | return;
|
---|
126 | PiePlot plot = ((PiePlot) getChart().getPlot());
|
---|
127 | if (c == null) {
|
---|
128 | plot.setLabelPaint(PiePlot.DEFAULT_LABEL_PAINT);
|
---|
129 | } else {
|
---|
130 | plot.setLabelPaint(SwingConversions.toSwingColor(c));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void setSourceThickness(float newThickness, boolean setConnected) {
|
---|
136 | super.setSourceThickness(newThickness, setConnected);
|
---|
137 | if (getChart() == null)
|
---|
138 | return;
|
---|
139 | PiePlot plot = ((PiePlot) getChart().getPlot());
|
---|
140 | plot.setBaseSectionOutlineStroke(new BasicStroke(newThickness));
|
---|
141 |
|
---|
142 | plot.setLabelLinkStroke(new BasicStroke(newThickness));
|
---|
143 | plot.setLabelOutlineStroke(new BasicStroke(newThickness));
|
---|
144 |
|
---|
145 | plot.setLabelFont(plot.getLabelFont().deriveFont(
|
---|
146 | getFontSize(newThickness, PiePlot.DEFAULT_LABEL_FONT
|
---|
147 | .getSize2D())));
|
---|
148 | plot.setLabelPadding(new RectangleInsets(newThickness + 1,
|
---|
149 | newThickness + 1, newThickness + 1, newThickness + 1));
|
---|
150 | plot.setLegendItemShape(Plot.getCircle((newThickness + 1) * 2));
|
---|
151 | }
|
---|
152 | }
|
---|