-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCBarGraph.java.notused
More file actions
164 lines (143 loc) · 5.18 KB
/
Copy pathCBarGraph.java.notused
File metadata and controls
164 lines (143 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package codeblockutil;
import java.awt.Color;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
public class CBarGraph extends JLabel{
private static final long serialVersionUID = 328149080230L;
private static final Color DEFAULT_BACKGROUND = CGraphite.blue;
private JFreeChart chart;
private BarData chartData;
private Color background;
private boolean lock = false;
private ChartPanel output = null;
private double upperBound = -1e9;
private double lowerBound = 0;
public CBarGraph(String title, int seriesNum, Color background){
super("", SwingConstants.CENTER);
this.chartData = new BarData(title);
this.chart = chartData.makeChart();
this.background = background == null ? DEFAULT_BACKGROUND : background;
this.chart.setBackgroundPaint(this.background);
this.chart.setBorderPaint(null);
ImageIcon icon = new ImageIcon(chart.createBufferedImage(150, 100));
this.setLayout(null);
this.setIcon(icon);
this.setBounds(0,0,170,130);
}
public Insets getInsets(){
return new Insets(15,10,15,10);
}
public void clearChart(){
if(!lock){
this.chart = chartData.makeChart();
this.chart.setBackgroundPaint(this.background);
this.chart.setBorderPaint(null);
if(output != null){
JFreeChart newChart = new JFreeChart(chart.getPlot());
newChart.getLegend().setPosition(RectangleEdge.TOP);
newChart.getLegend().setPadding(5, 5, 5, 5);
newChart.setBackgroundPaint(background);
output.setChart(newChart);
output.invalidate();
output.repaint();
}
}
}
public void updateDomain(String title, int seriesNum, Color background){
if(!lock){
this.chart = chartData.makeChart();
this.background = background == null ? DEFAULT_BACKGROUND : background;
this.chart.setBackgroundPaint(this.background);
this.chart.setBorderPaint(null);
if(output != null){
JFreeChart newChart = new JFreeChart(chart.getPlot());
newChart.getLegend().setPosition(RectangleEdge.TOP);
newChart.getLegend().setPadding(5, 5, 5, 5);
newChart.setBackgroundPaint(background);
output.setChart(newChart);
output.invalidate();
output.repaint();
}
}
}
public void updateValues(String seriesName, double value){
if(!lock){
DefaultCategoryDataset dataset = (DefaultCategoryDataset) chart.getCategoryPlot().getDataset();
dataset.addValue(value, seriesName, Integer.valueOf(0));
if (value < lowerBound) lowerBound = value;
if (value > upperBound) upperBound = value;
CategoryPlot plot = chart.getCategoryPlot();
plot.getRangeAxis().setLowerBound(lowerBound);
plot.getRangeAxis().setUpperBound(upperBound);
}
}
/**
* Updates the series name at the specified index
* @param seriesName the new seriesName to set at the specified index
* @param index the desired index to set the new seriesName to
*/
public void updateSeriesNamesAt(String seriesName, int index) {
//does nothing
//note from ria: could not find a way to update the series name of a bar graph
//in jfreechart
}
/**
* Updates the graph image displayed of this. Image includes graph data, axis, and legend.
*/
public void updateImage(){
if(!lock){
ImageIcon icon = new ImageIcon(chart.createBufferedImage(150, 100, 150.0, 100.0, null));
this.setIcon(icon);
}
}
public ChartPanel getOutputPanel(){
//we return a copy of the chart because we only want to show the
//legend in the larger view of the graph
//not the small runtime graph block view
JFreeChart newChart = new JFreeChart(chart.getPlot());
newChart.getLegend().setPosition(RectangleEdge.TOP);
newChart.getLegend().setPadding(5, 5, 5, 5);
newChart.setBackgroundPaint(background);
output = new ChartPanel(newChart);
return output;
}
public BufferedImage getBufferedImage(int width, int height){
return chart.createBufferedImage(width, height);
}
public String getCSV(){
StringBuilder output = new StringBuilder();
DefaultCategoryDataset d = (DefaultCategoryDataset) chart.getCategoryPlot().getDataset();
for (int i = 0 ; i<d.getRowCount(); i++){
output.append(d.getRowKey(i)+","+d.getValue(d.getRowKey(i),Integer.valueOf(0))+"\n");
}
return output.toString();
}
private class BarData extends ChartData {
public BarData(String title) {
super(title);
}
public JFreeChart makeChart() {
JFreeChart chart;
chart = ChartFactory.createBarChart3D("", "", "",
new DefaultCategoryDataset(), PlotOrientation.VERTICAL, false, false, false);
ValueAxis rangeAxis = chart.getCategoryPlot().getRangeAxis();
if(rangeAxis instanceof NumberAxis){
((NumberAxis)rangeAxis).setNumberFormatOverride(new DecimalFormat("######.###"));
}
return chart;
}
}
}