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.items.Text;
|
---|
11 | import org.expeditee.items.widgets.InteractiveWidget;
|
---|
12 | import org.jfree.chart.ChartPanel;
|
---|
13 | import org.jfree.chart.JFreeChart;
|
---|
14 | import org.jfree.chart.labels.PieSectionLabelGenerator;
|
---|
15 | import org.jfree.chart.plot.PiePlot;
|
---|
16 | import org.jfree.chart.plot.Plot;
|
---|
17 | import org.jfree.chart.title.LegendTitle;
|
---|
18 | import org.jfree.chart.title.TextTitle;
|
---|
19 | import org.jfree.data.general.AbstractDataset;
|
---|
20 | import org.jfree.data.general.DefaultPieDataset;
|
---|
21 |
|
---|
22 | public abstract class AbstractChart extends InteractiveWidget {
|
---|
23 |
|
---|
24 | protected static final String DEFAULT_TITLE = "";
|
---|
25 |
|
---|
26 | private JFreeChart _chart = null;
|
---|
27 |
|
---|
28 | private LegendTitle _legend = null;
|
---|
29 |
|
---|
30 | protected JFreeChart getChart() {
|
---|
31 | return _chart;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public AbstractChart(Text source, String[] args) {
|
---|
35 | super(source, new JPanel(), 100, 300, -1, 100, 300, -1);
|
---|
36 |
|
---|
37 | init();
|
---|
38 | refresh();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | protected String[] getArgs() {
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected void init() {
|
---|
47 | // create a chart...
|
---|
48 | _chart = createNewChart();
|
---|
49 | _chart.getPlot().setNoDataMessage("Add link to data frame");
|
---|
50 | _legend = _chart.getLegend();
|
---|
51 | ChartPanel cp = new ChartPanel(_chart);
|
---|
52 | cp.setPopupMenu(null);
|
---|
53 | super._swingComponent = cp;
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected abstract JFreeChart createNewChart();
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public void refresh() {
|
---|
60 | super.refresh();
|
---|
61 | // Get the data from the linked frame
|
---|
62 | Frame dataFrame = getDataFrame();
|
---|
63 |
|
---|
64 | _chart.clearSubtitles();
|
---|
65 |
|
---|
66 | if (dataFrame != null) {
|
---|
67 | _chart.setTitle(dataFrame.getTitle());
|
---|
68 |
|
---|
69 | refreshData(dataFrame);
|
---|
70 |
|
---|
71 | if (dataFrame.hasAnnotation("legend")) {
|
---|
72 | _chart.addLegend(_legend);
|
---|
73 | }
|
---|
74 | if (dataFrame.hasAnnotation("subtitle")) {
|
---|
75 | getChart().addSubtitle(
|
---|
76 | new TextTitle(dataFrame.getAnnotationValue("subtitle"),
|
---|
77 | JFreeChart.DEFAULT_TITLE_FONT.deriveFont(
|
---|
78 | Font.ITALIC,
|
---|
79 | JFreeChart.DEFAULT_TITLE_FONT
|
---|
80 | .getSize2D() * .7F)));
|
---|
81 | }
|
---|
82 |
|
---|
83 | refreshPlot(dataFrame, _chart.getPlot());
|
---|
84 |
|
---|
85 | } else {
|
---|
86 | _chart.setTitle(this.getClass().getSimpleName() + " Chart");
|
---|
87 | }
|
---|
88 | super._swingComponent.invalidate();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * @param dataFrame
|
---|
93 | */
|
---|
94 | protected abstract void refreshPlot(Frame dataFrame, Plot plot);
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * @param dataFrame
|
---|
98 | */
|
---|
99 | protected abstract void refreshData(Frame dataFrame);
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public float getMinimumBorderThickness() {
|
---|
103 | return 1.0F;
|
---|
104 | }
|
---|
105 | }
|
---|