-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
CategoryLabelPositions.java
408 lines (373 loc) · 15.5 KB
/
CategoryLabelPositions.java
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.]
*
* ---------------------------
* CategoryLabelPositions.java
* ---------------------------
* (C) Copyright 2004-2016, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 06-Jan-2004 : Version 1 (DG);
* 17-Feb-2004 : Added equals() method (DG);
* 05-Nov-2004 : Adjusted settings for UP_90 and DOWN_90 (DG);
* 02-Jul-2013 : Use ParamChecks (DG);
*
*/
package org.jfree.chart.axis;
import java.io.Serializable;
import org.jfree.chart.text.TextBlockAnchor;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.util.Args;
/**
* Records the label positions for a category axis. Instances of this class
* are immutable.
*/
public class CategoryLabelPositions implements Serializable {
/** For serialization. */
private static final long serialVersionUID = -8999557901920364580L;
/** STANDARD category label positions. */
public static final CategoryLabelPositions
STANDARD = new CategoryLabelPositions(
new CategoryLabelPosition(
RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_CENTER), // TOP
new CategoryLabelPosition(
RectangleAnchor.TOP, TextBlockAnchor.TOP_CENTER), // BOTTOM
new CategoryLabelPosition(
RectangleAnchor.RIGHT, TextBlockAnchor.CENTER_RIGHT,
CategoryLabelWidthType.RANGE, 0.30f), // LEFT
new CategoryLabelPosition(
RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT,
CategoryLabelWidthType.RANGE, 0.30f) // RIGHT
);
/** UP_90 category label positions. */
public static final CategoryLabelPositions
UP_90 = new CategoryLabelPositions(
new CategoryLabelPosition(
RectangleAnchor.BOTTOM, TextBlockAnchor.CENTER_LEFT,
TextAnchor.CENTER_LEFT, -Math.PI / 2.0,
CategoryLabelWidthType.RANGE, 0.30f), // TOP
new CategoryLabelPosition(
RectangleAnchor.TOP, TextBlockAnchor.CENTER_RIGHT,
TextAnchor.CENTER_RIGHT, -Math.PI / 2.0,
CategoryLabelWidthType.RANGE, 0.30f), // BOTTOM
new CategoryLabelPosition(
RectangleAnchor.RIGHT, TextBlockAnchor.BOTTOM_CENTER,
TextAnchor.BOTTOM_CENTER, -Math.PI / 2.0,
CategoryLabelWidthType.CATEGORY, 0.9f), // LEFT
new CategoryLabelPosition(
RectangleAnchor.LEFT, TextBlockAnchor.TOP_CENTER,
TextAnchor.TOP_CENTER, -Math.PI / 2.0,
CategoryLabelWidthType.CATEGORY, 0.90f) // RIGHT
);
/** DOWN_90 category label positions. */
public static final CategoryLabelPositions
DOWN_90 = new CategoryLabelPositions(
new CategoryLabelPosition(
RectangleAnchor.BOTTOM, TextBlockAnchor.CENTER_RIGHT,
TextAnchor.CENTER_RIGHT, Math.PI / 2.0,
CategoryLabelWidthType.RANGE, 0.30f), // TOP
new CategoryLabelPosition(
RectangleAnchor.TOP, TextBlockAnchor.CENTER_LEFT,
TextAnchor.CENTER_LEFT, Math.PI / 2.0,
CategoryLabelWidthType.RANGE, 0.30f), // BOTTOM
new CategoryLabelPosition(
RectangleAnchor.RIGHT, TextBlockAnchor.TOP_CENTER,
TextAnchor.TOP_CENTER, Math.PI / 2.0,
CategoryLabelWidthType.CATEGORY, 0.90f), // LEFT
new CategoryLabelPosition(
RectangleAnchor.LEFT, TextBlockAnchor.BOTTOM_CENTER,
TextAnchor.BOTTOM_CENTER, Math.PI / 2.0,
CategoryLabelWidthType.CATEGORY, 0.90f) // RIGHT
);
/** UP_45 category label positions. */
public static final CategoryLabelPositions UP_45
= createUpRotationLabelPositions(Math.PI / 4.0);
/** DOWN_45 category label positions. */
public static final CategoryLabelPositions DOWN_45
= createDownRotationLabelPositions(Math.PI / 4.0);
/**
* Creates a new instance where the category labels angled upwards by the
* specified amount.
*
* @param angle the rotation angle (should be < Math.PI / 2.0).
*
* @return A category label position specification.
*/
public static CategoryLabelPositions createUpRotationLabelPositions(
double angle) {
return new CategoryLabelPositions(
new CategoryLabelPosition(
RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_LEFT,
TextAnchor.BOTTOM_LEFT, -angle,
CategoryLabelWidthType.RANGE, 0.50f), // TOP
new CategoryLabelPosition(
RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT,
TextAnchor.TOP_RIGHT, -angle,
CategoryLabelWidthType.RANGE, 0.50f), // BOTTOM
new CategoryLabelPosition(
RectangleAnchor.RIGHT, TextBlockAnchor.BOTTOM_RIGHT,
TextAnchor.BOTTOM_RIGHT, -angle,
CategoryLabelWidthType.RANGE, 0.50f), // LEFT
new CategoryLabelPosition(
RectangleAnchor.LEFT, TextBlockAnchor.TOP_LEFT,
TextAnchor.TOP_LEFT, -angle,
CategoryLabelWidthType.RANGE, 0.50f) // RIGHT
);
}
/**
* Creates a new instance where the category labels angled downwards by the
* specified amount.
*
* @param angle the rotation angle (should be < Math.PI / 2.0).
*
* @return A category label position specification.
*/
public static CategoryLabelPositions createDownRotationLabelPositions(
double angle) {
return new CategoryLabelPositions(
new CategoryLabelPosition(
RectangleAnchor.BOTTOM, TextBlockAnchor.BOTTOM_RIGHT,
TextAnchor.BOTTOM_RIGHT, angle,
CategoryLabelWidthType.RANGE, 0.50f), // TOP
new CategoryLabelPosition(
RectangleAnchor.TOP, TextBlockAnchor.TOP_LEFT,
TextAnchor.TOP_LEFT, angle,
CategoryLabelWidthType.RANGE, 0.50f), // BOTTOM
new CategoryLabelPosition(
RectangleAnchor.RIGHT, TextBlockAnchor.TOP_RIGHT,
TextAnchor.TOP_RIGHT, angle,
CategoryLabelWidthType.RANGE, 0.50f), // LEFT
new CategoryLabelPosition(
RectangleAnchor.LEFT, TextBlockAnchor.BOTTOM_LEFT,
TextAnchor.BOTTOM_LEFT, angle,
CategoryLabelWidthType.RANGE, 0.50f) // RIGHT
);
}
/**
* The label positioning details used when an axis is at the top of a
* chart.
*/
private CategoryLabelPosition positionForAxisAtTop;
/**
* The label positioning details used when an axis is at the bottom of a
* chart.
*/
private CategoryLabelPosition positionForAxisAtBottom;
/**
* The label positioning details used when an axis is at the left of a
* chart.
*/
private CategoryLabelPosition positionForAxisAtLeft;
/**
* The label positioning details used when an axis is at the right of a
* chart.
*/
private CategoryLabelPosition positionForAxisAtRight;
/**
* Default constructor.
*/
public CategoryLabelPositions() {
this.positionForAxisAtTop = new CategoryLabelPosition();
this.positionForAxisAtBottom = new CategoryLabelPosition();
this.positionForAxisAtLeft = new CategoryLabelPosition();
this.positionForAxisAtRight = new CategoryLabelPosition();
}
/**
* Creates a new position specification.
*
* @param top the label position info used when an axis is at the top
* ({@code null} not permitted).
* @param bottom the label position info used when an axis is at the
* bottom ({@code null} not permitted).
* @param left the label position info used when an axis is at the left
* ({@code null} not permitted).
* @param right the label position info used when an axis is at the right
* ({@code null} not permitted).
*/
public CategoryLabelPositions(CategoryLabelPosition top,
CategoryLabelPosition bottom, CategoryLabelPosition left,
CategoryLabelPosition right) {
Args.nullNotPermitted(top, "top");
Args.nullNotPermitted(bottom, "bottom");
Args.nullNotPermitted(left, "left");
Args.nullNotPermitted(right, "right");
this.positionForAxisAtTop = top;
this.positionForAxisAtBottom = bottom;
this.positionForAxisAtLeft = left;
this.positionForAxisAtRight = right;
}
/**
* Returns the category label position specification for an axis at the
* given location.
*
* @param edge the axis location.
*
* @return The category label position specification.
*/
public CategoryLabelPosition getLabelPosition(RectangleEdge edge) {
CategoryLabelPosition result = null;
if (edge == RectangleEdge.TOP) {
result = this.positionForAxisAtTop;
}
else if (edge == RectangleEdge.BOTTOM) {
result = this.positionForAxisAtBottom;
}
else if (edge == RectangleEdge.LEFT) {
result = this.positionForAxisAtLeft;
}
else if (edge == RectangleEdge.RIGHT) {
result = this.positionForAxisAtRight;
}
return result;
}
/**
* Returns a new instance based on an existing instance but with the top
* position changed.
*
* @param base the base ({@code null} not permitted).
* @param top the top position ({@code null} not permitted).
*
* @return A new instance (never {@code null}).
*/
public static CategoryLabelPositions replaceTopPosition(
CategoryLabelPositions base, CategoryLabelPosition top) {
Args.nullNotPermitted(base, "base");
Args.nullNotPermitted(top, "top");
return new CategoryLabelPositions(top,
base.getLabelPosition(RectangleEdge.BOTTOM),
base.getLabelPosition(RectangleEdge.LEFT),
base.getLabelPosition(RectangleEdge.RIGHT));
}
/**
* Returns a new instance based on an existing instance but with the bottom
* position changed.
*
* @param base the base ({@code null} not permitted).
* @param bottom the bottom position ({@code null} not permitted).
*
* @return A new instance (never {@code null}).
*/
public static CategoryLabelPositions replaceBottomPosition(
CategoryLabelPositions base, CategoryLabelPosition bottom) {
Args.nullNotPermitted(base, "base");
Args.nullNotPermitted(bottom, "bottom");
return new CategoryLabelPositions(
base.getLabelPosition(RectangleEdge.TOP),
bottom,
base.getLabelPosition(RectangleEdge.LEFT),
base.getLabelPosition(RectangleEdge.RIGHT));
}
/**
* Returns a new instance based on an existing instance but with the left
* position changed.
*
* @param base the base ({@code null} not permitted).
* @param left the left position ({@code null} not permitted).
*
* @return A new instance (never {@code null}).
*/
public static CategoryLabelPositions replaceLeftPosition(
CategoryLabelPositions base, CategoryLabelPosition left) {
Args.nullNotPermitted(base, "base");
Args.nullNotPermitted(left, "left");
return new CategoryLabelPositions(
base.getLabelPosition(RectangleEdge.TOP),
base.getLabelPosition(RectangleEdge.BOTTOM),
left,
base.getLabelPosition(RectangleEdge.RIGHT));
}
/**
* Returns a new instance based on an existing instance but with the right
* position changed.
*
* @param base the base ({@code null} not permitted).
* @param right the right position ({@code null} not permitted).
*
* @return A new instance (never {@code null}).
*/
public static CategoryLabelPositions replaceRightPosition(
CategoryLabelPositions base, CategoryLabelPosition right) {
Args.nullNotPermitted(base, "base");
Args.nullNotPermitted(right, "right");
return new CategoryLabelPositions(
base.getLabelPosition(RectangleEdge.TOP),
base.getLabelPosition(RectangleEdge.BOTTOM),
base.getLabelPosition(RectangleEdge.LEFT),
right);
}
/**
* Returns {@code true} if this object is equal to the specified
* object, and {@code false} otherwise.
*
* @param obj the other object.
*
* @return A boolean.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof CategoryLabelPositions)) {
return false;
}
CategoryLabelPositions that = (CategoryLabelPositions) obj;
if (!this.positionForAxisAtTop.equals(that.positionForAxisAtTop)) {
return false;
}
if (!this.positionForAxisAtBottom.equals(
that.positionForAxisAtBottom)) {
return false;
}
if (!this.positionForAxisAtLeft.equals(that.positionForAxisAtLeft)) {
return false;
}
if (!this.positionForAxisAtRight.equals(that.positionForAxisAtRight)) {
return false;
}
return true;
}
/**
* Returns a hash code for this object.
*
* @return A hash code.
*/
@Override
public int hashCode() {
int result = 19;
result = 37 * result + this.positionForAxisAtTop.hashCode();
result = 37 * result + this.positionForAxisAtBottom.hashCode();
result = 37 * result + this.positionForAxisAtLeft.hashCode();
result = 37 * result + this.positionForAxisAtRight.hashCode();
return result;
}
}