-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathChartBean.java
More file actions
215 lines (188 loc) · 5.34 KB
/
Copy pathChartBean.java
File metadata and controls
215 lines (188 loc) · 5.34 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package chart;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.beans.*;
import javax.swing.*;
/**
* A bean to draw a bar chart.
* @version 1.31 2007-10-03
* @author Cay Horstmann
*/
public class ChartBean extends JComponent
{
public enum Position { LEFT, CENTER, RIGHT };
private static final int XPREFSIZE = 300;
private static final int YPREFSIZE = 300;
private static final int INSETS = 10;
private double[] values = { 1, 2, 3 };
private String title = "Title";
private Position titlePosition = Position.CENTER;
private boolean inverse;
private Color color = Color.red;
public ChartBean()
{
setPreferredSize(new Dimension(XPREFSIZE, YPREFSIZE));
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
if (values == null || values.length == 0) return;
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++)
{
if (minValue > getValues(i)) minValue = getValues(i);
if (maxValue < getValues(i)) maxValue = getValues(i);
}
if (maxValue == minValue) return;
Rectangle2D bounds = getBounds();
double clientWidth = bounds.getWidth();
double clientHeight = bounds.getHeight();
double barWidth = (clientWidth - 2 * INSETS) / values.length;
g2.setPaint(inverse ? color : Color.white);
g2.fill(new Rectangle2D.Double(0, 0, clientWidth, clientHeight));
g2.setPaint(Color.black);
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D titleBounds = titleFont.getStringBounds(title, context);
double titleWidth = titleBounds.getWidth();
double y = -titleBounds.getY();
double x = 0;
if (titlePosition == Position.CENTER) x += (clientWidth - titleWidth) / 2;
else if (titlePosition == Position.RIGHT) x += clientWidth - titleWidth;
g2.setFont(titleFont);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.drawString(title, (float) x, (float) y);
double top = titleBounds.getHeight();
double scale = (clientHeight - top - 2 * INSETS) / (maxValue - minValue);
y = clientHeight;
for (int i = 0; i < values.length; i++)
{
double x1 = INSETS + i * barWidth + 1;
double y1 = INSETS + top;
double value = getValues(i);
double height = value * scale;
if (value >= 0) y1 += (maxValue - value) * scale;
else
{
y1 += (int) (maxValue * scale);
height = -height;
}
g2.setPaint(inverse ? Color.white : color);
Rectangle2D bar = new Rectangle2D.Double(x1, y1, barWidth - 2, height);
g2.fill(bar);
g2.setPaint(Color.black);
g2.draw(bar);
}
}
/**
* Sets the title property.
* @param t the new chart title.
*/
public void setTitle(String t)
{
title = t;
}
/**
* Gets the title property.
* @return the chart title.
*/
public String getTitle()
{
return title;
}
/**
* Sets the indexed values property.
* @param newValue the values to display in the chart.
*/
public void setValues(double[] newValue)
{
double[] oldValue = values;
firePropertyChange("values", oldValue, newValue);
values = newValue;
}
/**
* Gets the indexed values property.
* @return the values to display in the chart.
*/
public double[] getValues()
{
return values;
}
/**
* Sets the indexed values property.
* @param i the index of the value to set
* @param value the new value for that index
*/
public void setValues(int i, double value)
{
if (0 <= i && i < values.length)
{
double oldValue = values[i];
values[i] = value;
for (PropertyChangeListener listener : getPropertyChangeListeners())
listener.propertyChange(new IndexedPropertyChangeEvent(this, "values", oldValue,
value, i));
}
}
/**
* Gets the indexed values property.
* @param i the index of the value to get
* @return the value for that index
*/
public double getValues(int i)
{
if (0 <= i && i < values.length) return values[i];
return 0;
}
/**
* Sets the inverse property.
* @param b true if the display is inverted (white bars on colored background)
*/
public void setInverse(boolean b)
{
inverse = b;
}
/**
* Gets the inverse property.
* @return true if the display is inverted
*/
public boolean isInverse()
{
return inverse;
}
/**
* Sets the titlePosition property.
* @param p LEFT, CENTER, or RIGHT
*/
public void setTitlePosition(Position p)
{
titlePosition = p;
}
/**
* Gets the titlePosition property.
* @return LEFT, CENTER, or RIGHT
*/
public Position getTitlePosition()
{
return titlePosition;
}
/**
* Sets the graphColor property.
* @param c the color to use for the graph
*/
public void setGraphColor(Color c)
{
color = c;
}
/**
* Gets the graphColor property.
* @param c the color to use for the graph
*/
public Color getGraphColor()
{
return color;
}
}