-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlankPlotChart.java
More file actions
231 lines (202 loc) · 7.95 KB
/
Copy pathBlankPlotChart.java
File metadata and controls
231 lines (202 loc) · 7.95 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package Charts;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.text.DecimalFormat;
import javax.swing.JComponent;
import javax.swing.border.EmptyBorder;
public class BlankPlotChart extends JComponent {
public BlankPlotChatRender getBlankPlotChatRender() {
return blankPlotChatRender;
}
public void setBlankPlotChatRender(BlankPlotChatRender blankPlotChatRender) {
this.blankPlotChatRender = blankPlotChatRender;
}
public double getMaxValues() {
return maxValues;
}
public void setMaxValues(double maxValues) {
this.maxValues = maxValues;
niceScale.setMax(maxValues);
repaint();
}
public double getMinValues() {
return minValues;
}
public int getLabelCount() {
return labelCount;
}
public void setLabelCount(int labelCount) {
this.labelCount = labelCount;
}
public String getValuesFormat() {
return valuesFormat;
}
public void setValuesFormat(String valuesFormat) {
this.valuesFormat = valuesFormat;
format.applyPattern(valuesFormat);
}
private final DecimalFormat format = new DecimalFormat("#,##0.##");
private NiceScale niceScale;
private double maxValues;
private double minValues;
private int labelCount;
private String valuesFormat = "#,##0.##";
private BlankPlotChatRender blankPlotChatRender;
public BlankPlotChart() {
setBackground(Color.WHITE);
setOpaque(false);
setForeground(new Color(180, 180, 180));
setBorder(new EmptyBorder(35, 10, 10, 10));
init();
}
private void init() {
initValues(0, 10);
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent me) {
mouseMove((Graphics2D) getGraphics(), me);
}
});
}
public void initValues(double minValues, double maxValues) {
this.minValues = minValues;
this.maxValues = maxValues;
niceScale = new NiceScale(minValues, maxValues);
repaint();
}
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
if (niceScale != null) {
Graphics2D g2 = (Graphics2D) grphcs;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
createLine(g2);
createValues(g2);
createLabelText(g2);
renderSeries(g2);
}
}
private void createLine(Graphics2D g2) {
g2.setColor(new Color(150, 150, 150, 20));
Insets insets = getInsets();
double textHeight = getLabelTextHeight(g2);
double height = getHeight() - (insets.top + insets.bottom) - textHeight;
double space = height / niceScale.getMaxTicks();
double locationY = insets.bottom + textHeight;
double textWidth = getMaxValuesTextWidth(g2);
double spaceText = 5;
for (int i = 0; i <= niceScale.getMaxTicks(); i++) {
int y = (int) (getHeight() - locationY);
g2.drawLine((int) (insets.left + textWidth + spaceText), y, (int) getWidth() - insets.right, y);
locationY += space;
}
}
private void createValues(Graphics2D g2) {
g2.setColor(getForeground());
Insets insets = getInsets();
double textHeight = getLabelTextHeight(g2);
double height = getHeight() - (insets.top + insets.bottom) - textHeight;
double space = height / niceScale.getMaxTicks();
double valuesCount = niceScale.getNiceMin();
double locationY = insets.bottom + textHeight;
FontMetrics ft = g2.getFontMetrics();
for (int i = 0; i <= niceScale.getMaxTicks(); i++) {
String text = format.format(valuesCount);
Rectangle2D r2 = ft.getStringBounds(text, g2);
double stringY = r2.getCenterY() * -1;
double y = getHeight() - locationY + stringY;
g2.drawString(text, insets.left, (int) y);
locationY += space;
valuesCount += niceScale.getTickSpacing();
}
}
private void createLabelText(Graphics2D g2) {
if (labelCount > 0) {
Insets insets = getInsets();
double textWidth = getMaxValuesTextWidth(g2);
double spaceText = 5;
double width = getWidth() - insets.left - insets.right - textWidth - spaceText;
double space = width / labelCount;
double locationX = insets.left + textWidth + spaceText;
double locationText = getHeight() - insets.bottom + 5;
FontMetrics ft = g2.getFontMetrics();
for (int i = 0; i < labelCount; i++) {
double centerX = ((locationX + space / 2));
g2.setColor(getForeground());
String text = getChartText(i);
Rectangle2D r2 = ft.getStringBounds(text, g2);
double textX = centerX - r2.getWidth() / 2;
g2.drawString(text, (int) textX, (int) locationText);
locationX += space;
}
}
}
private void renderSeries(Graphics2D g2) {
if (blankPlotChatRender != null) {
Insets inset = getInsets();
double textWidth = getMaxValuesTextWidth(g2);
double textHeight = getLabelTextHeight(g2);
Rectangle2D.Double rectangle = new Rectangle.Double(inset.left + textWidth, inset.top, getWidth() - (inset.left + inset.right + textWidth), getHeight() - (inset.top + inset.bottom + textHeight));
blankPlotChatRender.renderGraphics(this, g2, rectangle);
}
}
private void mouseMove(Graphics2D g2, MouseEvent evt) {
if (blankPlotChatRender != null) {
Insets inset = getInsets();
double textWidth = getMaxValuesTextWidth(g2);
double textHeight = getLabelTextHeight(g2);
Rectangle2D.Double rectangle = new Rectangle.Double(inset.left + textWidth, inset.top, getWidth() - (inset.left + inset.right + textWidth), getHeight() - (inset.top + inset.bottom + textHeight));
blankPlotChatRender.mouseMove(rectangle, evt);
}
}
private double getMaxValuesTextWidth(Graphics2D g2) {
double width = 0;
FontMetrics ft = g2.getFontMetrics();
double valuesCount = niceScale.getNiceMin();
for (int i = 0; i <= niceScale.getMaxTicks(); i++) {
String text = format.format(valuesCount);
Rectangle2D r2 = ft.getStringBounds(text, g2);
double w = r2.getWidth();
if (w > width) {
width = w;
}
valuesCount += niceScale.getTickSpacing();
}
return width;
}
private int getLabelTextHeight(Graphics2D g2) {
FontMetrics ft = g2.getFontMetrics();
return ft.getHeight();
}
private String getChartText(int index) {
if (blankPlotChatRender != null) {
return blankPlotChatRender.getLabelText(index);
} else {
return "Label";
}
}
public SeriesSize getRectangle(int index, double height, double space, double startX, double startY) {
double x = startX + space * index;
SeriesSize size = new SeriesSize(x, startY + 1, space, height);
return size;
}
public NiceScale getNiceScale() {
return niceScale;
}
public void setNiceScale(NiceScale niceScale) {
this.niceScale = niceScale;
}
public DecimalFormat getFormat() {
return format;
}
}