source: trunk/src/org/expeditee/items/widgets/charts/AbstractCategory.java@ 204

Last change on this file since 204 was 204, checked in by ra33, 16 years ago

Added bar chart for single categories

File size: 3.0 KB
Line 
1package org.expeditee.items.widgets.charts;
2
3import java.awt.Font;
4
5import javax.swing.JPanel;
6
7import org.expeditee.gui.AttributeUtils;
8import org.expeditee.gui.AttributeValuePair;
9import org.expeditee.gui.Frame;
10import org.expeditee.items.Text;
11import org.expeditee.items.widgets.InteractiveWidget;
12import org.jfree.chart.ChartPanel;
13import org.jfree.chart.JFreeChart;
14import org.jfree.chart.plot.CategoryPlot;
15import org.jfree.chart.plot.PlotOrientation;
16import org.jfree.chart.title.LegendTitle;
17import org.jfree.chart.title.TextTitle;
18import org.jfree.data.category.DefaultCategoryDataset;
19
20public 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}
Note: See TracBrowser for help on using the repository browser.