1 | package org.expeditee.items.widgets.charts;
|
---|
2 |
|
---|
3 | import java.awt.Font;
|
---|
4 |
|
---|
5 | import javax.swing.JPanel;
|
---|
6 |
|
---|
7 | import org.expeditee.gui.AttributeUtils;
|
---|
8 | import org.expeditee.gui.AttributeValuePair;
|
---|
9 | import org.expeditee.gui.Frame;
|
---|
10 | import org.expeditee.gui.FrameIO;
|
---|
11 | import org.expeditee.items.Item;
|
---|
12 | import org.expeditee.items.Text;
|
---|
13 | import org.expeditee.items.widgets.InteractiveWidget;
|
---|
14 | import org.jfree.chart.ChartPanel;
|
---|
15 | import org.jfree.chart.JFreeChart;
|
---|
16 | import org.jfree.chart.labels.PieSectionLabelGenerator;
|
---|
17 | import org.jfree.chart.plot.PiePlot;
|
---|
18 | import org.jfree.chart.title.LegendTitle;
|
---|
19 | import org.jfree.chart.title.TextTitle;
|
---|
20 | import org.jfree.data.general.DefaultPieDataset;
|
---|
21 |
|
---|
22 | public abstract class AbstractPie extends InteractiveWidget {
|
---|
23 |
|
---|
24 | protected static final String DEFAULT_TITLE = "";
|
---|
25 |
|
---|
26 | protected DefaultPieDataset _data = new DefaultPieDataset();
|
---|
27 |
|
---|
28 | private JFreeChart _chart = null;
|
---|
29 |
|
---|
30 | private LegendTitle _legend = null;
|
---|
31 |
|
---|
32 | private PieSectionLabelGenerator _labelGenerator;
|
---|
33 |
|
---|
34 | protected JFreeChart getChart() {
|
---|
35 | return _chart;
|
---|
36 | }
|
---|
37 |
|
---|
38 | protected DefaultPieDataset getData() {
|
---|
39 | return _data;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public AbstractPie(Text source, String[] args) {
|
---|
43 | super(source, new JPanel(), 100, 300, -1, 100, 300, -1);
|
---|
44 |
|
---|
45 | init();
|
---|
46 | refresh();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | protected String[] getArgs() {
|
---|
51 | return null;
|
---|
52 | }
|
---|
53 |
|
---|
54 | protected void init() {
|
---|
55 | // create a chart...
|
---|
56 | _chart = createNewChart();
|
---|
57 | _chart.getPlot().setNoDataMessage("Add link to data frame");
|
---|
58 | _legend = _chart.getLegend();
|
---|
59 | _labelGenerator = ((PiePlot) getChart().getPlot()).getLabelGenerator();
|
---|
60 | ChartPanel cp = new ChartPanel(_chart);
|
---|
61 | cp.setPopupMenu(null);
|
---|
62 | super._swingComponent = cp;
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected abstract JFreeChart createNewChart();
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void refresh() {
|
---|
69 | super.refresh();
|
---|
70 | // Get the data from the linked frame
|
---|
71 | Frame dataFrame = getDataFrame();
|
---|
72 |
|
---|
73 | _data.clear();
|
---|
74 | _chart.clearSubtitles();
|
---|
75 |
|
---|
76 | if (dataFrame != null) {
|
---|
77 | _chart.setTitle(dataFrame.getTitle());
|
---|
78 |
|
---|
79 | for (Text t : dataFrame.getBodyTextItems(false)) {
|
---|
80 | AttributeValuePair avp = AttributeUtils.getPair(t.getText());
|
---|
81 | if (avp != null) {
|
---|
82 | _data.setValue(avp.getAttribute(), avp.getDoubleValue());
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (dataFrame.hasAnnotation("legend")) {
|
---|
87 | _chart.addLegend(_legend);
|
---|
88 | }
|
---|
89 | if (dataFrame.hasAnnotation("subtitle")) {
|
---|
90 | getChart().addSubtitle(
|
---|
91 | new TextTitle(dataFrame.getAnnotationValue("subtitle"),
|
---|
92 | JFreeChart.DEFAULT_TITLE_FONT.deriveFont(
|
---|
93 | Font.ITALIC,
|
---|
94 | JFreeChart.DEFAULT_TITLE_FONT
|
---|
95 | .getSize2D() * .7F)));
|
---|
96 | }
|
---|
97 | if (dataFrame != null) {
|
---|
98 | if (dataFrame.hasAnnotation("labels")) {
|
---|
99 | ((PiePlot) getChart().getPlot())
|
---|
100 | .setLabelGenerator(_labelGenerator);
|
---|
101 | } else {
|
---|
102 | ((PiePlot) getChart().getPlot()).setLabelGenerator(null);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 | _chart.setTitle(this.getClass().getSimpleName() + " Chart");
|
---|
107 | }
|
---|
108 | super._swingComponent.invalidate();
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public float getMinimumBorderThickness() {
|
---|
113 | return 1.0F;
|
---|
114 | }
|
---|
115 | }
|
---|