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.plot.CategoryPlot;
|
---|
15 | import org.jfree.chart.plot.PlotOrientation;
|
---|
16 | import org.jfree.chart.title.LegendTitle;
|
---|
17 | import org.jfree.chart.title.TextTitle;
|
---|
18 | import org.jfree.data.category.DefaultCategoryDataset;
|
---|
19 |
|
---|
20 | public abstract class AbstractCategory extends InteractiveWidget {
|
---|
21 |
|
---|
22 | protected static final String DEFAULT_TITLE = "";
|
---|
23 |
|
---|
24 | protected DefaultCategoryDataset _data = new DefaultCategoryDataset();
|
---|
25 |
|
---|
26 | private JFreeChart _chart = null;
|
---|
27 |
|
---|
28 | private LegendTitle _legend = null;
|
---|
29 |
|
---|
30 | protected JFreeChart getChart() {
|
---|
31 | return _chart;
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected DefaultCategoryDataset getData() {
|
---|
35 | return _data;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public AbstractCategory(Text source, String[] args) {
|
---|
39 | super(source, new JPanel(), 100, 300, -1, 100, 300, -1);
|
---|
40 |
|
---|
41 | init();
|
---|
42 | refresh();
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | protected String[] getArgs() {
|
---|
47 | return null;
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected void init() {
|
---|
51 | // create a chart...
|
---|
52 | _chart = createNewChart();
|
---|
53 | _chart.getPlot().setNoDataMessage("Add link to data frame");
|
---|
54 | _legend = _chart.getLegend();
|
---|
55 | ChartPanel cp = new ChartPanel(_chart);
|
---|
56 | cp.setPopupMenu(null);
|
---|
57 | super._swingComponent = cp;
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected abstract JFreeChart createNewChart();
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void refresh() {
|
---|
64 | super.refresh();
|
---|
65 | // Get the data from the linked frame
|
---|
66 | Frame dataFrame = getDataFrame();
|
---|
67 |
|
---|
68 | _data.clear();
|
---|
69 | _chart.clearSubtitles();
|
---|
70 |
|
---|
71 | if (dataFrame != null) {
|
---|
72 | _chart.setTitle(dataFrame.getTitle());
|
---|
73 |
|
---|
74 | for (Text t : dataFrame.getBodyTextItems(false)) {
|
---|
75 | AttributeValuePair avp = AttributeUtils.getPair(t.getText());
|
---|
76 | if (avp != null) {
|
---|
77 | _data
|
---|
78 | .setValue(avp.getDoubleValue(), "", avp
|
---|
79 | .getAttribute());
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (dataFrame.hasAnnotation("legend")) {
|
---|
84 | _chart.addLegend(_legend);
|
---|
85 | }
|
---|
86 | if (dataFrame.hasAnnotation("subtitle")) {
|
---|
87 | getChart().addSubtitle(
|
---|
88 | new TextTitle(dataFrame.getAnnotationValue("subtitle"),
|
---|
89 | JFreeChart.DEFAULT_TITLE_FONT.deriveFont(
|
---|
90 | Font.ITALIC,
|
---|
91 | JFreeChart.DEFAULT_TITLE_FONT
|
---|
92 | .getSize2D() * .7F)));
|
---|
93 | }
|
---|
94 | CategoryPlot plot = (CategoryPlot) getChart().getPlot();
|
---|
95 | if (dataFrame.hasAnnotation("xaxis")) {
|
---|
96 | plot.getRangeAxis().setLabel(
|
---|
97 | dataFrame.getAnnotationValue("xaxis"));
|
---|
98 | }
|
---|
99 | if (dataFrame.hasAnnotation("yaxis")) {
|
---|
100 | plot.getDomainAxis().setLabel(
|
---|
101 | dataFrame.getAnnotationValue("yaxis"));
|
---|
102 | }
|
---|
103 | if (dataFrame.hasAnnotation("horizontal")) {
|
---|
104 | plot.setOrientation(PlotOrientation.HORIZONTAL);
|
---|
105 | } else {
|
---|
106 | plot.setOrientation(PlotOrientation.VERTICAL);
|
---|
107 | }
|
---|
108 |
|
---|
109 | } else {
|
---|
110 | _chart.setTitle(this.getClass().getSimpleName() + " Chart");
|
---|
111 | }
|
---|
112 | super._swingComponent.invalidate();
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public float getMinimumBorderThickness() {
|
---|
117 | return 1.0F;
|
---|
118 | }
|
---|
119 | }
|
---|