/
RingPlot.java
732 lines (677 loc) · 24.3 KB
/
RingPlot.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2017, 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.]
*
* -------------
* RingPlot.java
* -------------
* (C) Copyright 2004-2017, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limtied);
* Contributor(s): Christoph Beck (bug 2121818);
*
* Changes
* -------
* 08-Nov-2004 : Version 1 (DG);
* 22-Feb-2005 : Renamed DonutPlot --> RingPlot (DG);
* 06-Jun-2005 : Added default constructor and fixed equals() method to handle
* GradientPaint (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 20-Dec-2005 : Fixed problem with entity shape (bug 1386328) (DG);
* 27-Sep-2006 : Updated drawItem() method for new lookup methods (DG);
* 12-Oct-2006 : Added configurable section depth (DG);
* 14-Feb-2007 : Added notification in setSectionDepth() method (DG);
* 23-Sep-2008 : Fix for bug 2121818 by Christoph Beck (DG);
* 13-Jul-2009 : Added support for shadow generator (DG);
* 11-Oct-2011 : Check sectionOutlineVisible - bug 3237879 (DG);
* 02-Jul-2013 : Use ParamChecks (DG);
* 28-Feb-2014 : Add center text feature (DG);
*
*/
package org.jfree.chart.plot;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.Format;
import org.jfree.chart.entity.EntityCollection;
import org.jfree.chart.entity.PieSectionEntity;
import org.jfree.chart.labels.PieToolTipGenerator;
import org.jfree.chart.text.TextUtils;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.urls.PieURLGenerator;
import org.jfree.chart.util.LineUtils;
import org.jfree.chart.util.ObjectUtils;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.ParamChecks;
import org.jfree.chart.util.Rotation;
import org.jfree.chart.util.SerialUtils;
import org.jfree.chart.util.ShapeUtils;
import org.jfree.chart.util.UnitType;
import org.jfree.data.general.PieDataset;
/**
* A customised pie plot that leaves a hole in the middle.
*/
public class RingPlot extends PiePlot implements Cloneable, Serializable {
/** For serialization. */
private static final long serialVersionUID = 1556064784129676620L;
/** The center text mode. */
private CenterTextMode centerTextMode = CenterTextMode.NONE;
/**
* Text to display in the middle of the chart (used for
* CenterTextMode.FIXED).
*/
private String centerText;
/**
* The formatter used when displaying the first data value from the
* dataset (CenterTextMode.VALUE).
*/
private Format centerTextFormatter = new DecimalFormat("0.00");
/** The font used to display the center text. */
private Font centerTextFont;
/** The color used to display the center text. */
private Color centerTextColor;
/**
* A flag that controls whether or not separators are drawn between the
* sections of the chart.
*/
private boolean separatorsVisible;
/** The stroke used to draw separators. */
private transient Stroke separatorStroke;
/** The paint used to draw separators. */
private transient Paint separatorPaint;
/**
* The length of the inner separator extension (as a percentage of the
* depth of the sections).
*/
private double innerSeparatorExtension;
/**
* The length of the outer separator extension (as a percentage of the
* depth of the sections).
*/
private double outerSeparatorExtension;
/**
* The depth of the section as a percentage of the diameter.
*/
private double sectionDepth;
/**
* Creates a new plot with a {@code null} dataset.
*/
public RingPlot() {
this(null);
}
/**
* Creates a new plot for the specified dataset.
*
* @param dataset the dataset ({@code null} permitted).
*/
public RingPlot(PieDataset dataset) {
super(dataset);
this.centerTextMode = CenterTextMode.NONE;
this.centerText = null;
this.centerTextFormatter = new DecimalFormat("0.00");
this.centerTextFont = DEFAULT_LABEL_FONT;
this.centerTextColor = Color.BLACK;
this.separatorsVisible = true;
this.separatorStroke = new BasicStroke(0.5f);
this.separatorPaint = Color.gray;
this.innerSeparatorExtension = 0.20; // 20%
this.outerSeparatorExtension = 0.20; // 20%
this.sectionDepth = 0.20; // 20%
}
/**
* Returns the mode for displaying text in the center of the plot. The
* default value is {@link CenterTextMode#NONE} therefore no text
* will be displayed by default.
*
* @return The mode (never {@code null}).
*
* @since 1.0.18
*/
public CenterTextMode getCenterTextMode() {
return this.centerTextMode;
}
/**
* Sets the mode for displaying text in the center of the plot and sends
* a change event to all registered listeners. For
* {@link CenterTextMode#FIXED}, the display text will come from the
* {@code centerText} attribute (see {@link #getCenterText()}).
* For {@link CenterTextMode#VALUE}, the center text will be the value from
* the first section in the dataset.
*
* @param mode the mode ({@code null} not permitted).
*
* @since 1.0.18
*/
public void setCenterTextMode(CenterTextMode mode) {
ParamChecks.nullNotPermitted(mode, "mode");
this.centerTextMode = mode;
fireChangeEvent();
}
/**
* Returns the text to display in the center of the plot when the mode
* is {@link CenterTextMode#FIXED}.
*
* @return The text (possibly {@code null}).
*
* @since 1.0.18.
*/
public String getCenterText() {
return this.centerText;
}
/**
* Sets the text to display in the center of the plot and sends a
* change event to all registered listeners. If the text is set to
* {@code null}, no text will be displayed.
*
* @param text the text ({@code null} permitted).
*
* @since 1.0.18
*/
public void setCenterText(String text) {
this.centerText = text;
fireChangeEvent();
}
/**
* Returns the formatter used to format the center text value for the mode
* {@link CenterTextMode#VALUE}. The default value is
* {@code DecimalFormat("0.00")}.
*
* @return The formatter (never {@code null}).
*
* @since 1.0.18
*/
public Format getCenterTextFormatter() {
return this.centerTextFormatter;
}
/**
* Sets the formatter used to format the center text value and sends a
* change event to all registered listeners.
*
* @param formatter the formatter ({@code null} not permitted).
*
* @since 1.0.18
*/
public void setCenterTextFormatter(Format formatter) {
ParamChecks.nullNotPermitted(formatter, "formatter");
this.centerTextFormatter = formatter;
}
/**
* Returns the font used to display the center text. The default value
* is {@link PiePlot#DEFAULT_LABEL_FONT}.
*
* @return The font (never {@code null}).
*
* @since 1.0.18
*/
public Font getCenterTextFont() {
return this.centerTextFont;
}
/**
* Sets the font used to display the center text and sends a change event
* to all registered listeners.
*
* @param font the font ({@code null} not permitted).
*
* @since 1.0.18
*/
public void setCenterTextFont(Font font) {
ParamChecks.nullNotPermitted(font, "font");
this.centerTextFont = font;
fireChangeEvent();
}
/**
* Returns the color for the center text. The default value is
* {@code Color.BLACK}.
*
* @return The color (never {@code null}).
*
* @since 1.0.18
*/
public Color getCenterTextColor() {
return this.centerTextColor;
}
/**
* Sets the color for the center text and sends a change event to all
* registered listeners.
*
* @param color the color ({@code null} not permitted).
*
* @since 1.0.18
*/
public void setCenterTextColor(Color color) {
ParamChecks.nullNotPermitted(color, "color");
this.centerTextColor = color;
fireChangeEvent();
}
/**
* Returns a flag that indicates whether or not separators are drawn between
* the sections in the chart.
*
* @return A boolean.
*
* @see #setSeparatorsVisible(boolean)
*/
public boolean getSeparatorsVisible() {
return this.separatorsVisible;
}
/**
* Sets the flag that controls whether or not separators are drawn between
* the sections in the chart, and sends a change event to all registered
* listeners.
*
* @param visible the flag.
*
* @see #getSeparatorsVisible()
*/
public void setSeparatorsVisible(boolean visible) {
this.separatorsVisible = visible;
fireChangeEvent();
}
/**
* Returns the separator stroke.
*
* @return The stroke (never {@code null}).
*
* @see #setSeparatorStroke(Stroke)
*/
public Stroke getSeparatorStroke() {
return this.separatorStroke;
}
/**
* Sets the stroke used to draw the separator between sections and sends
* a change event to all registered listeners.
*
* @param stroke the stroke ({@code null} not permitted).
*
* @see #getSeparatorStroke()
*/
public void setSeparatorStroke(Stroke stroke) {
ParamChecks.nullNotPermitted(stroke, "stroke");
this.separatorStroke = stroke;
fireChangeEvent();
}
/**
* Returns the separator paint.
*
* @return The paint (never {@code null}).
*
* @see #setSeparatorPaint(Paint)
*/
public Paint getSeparatorPaint() {
return this.separatorPaint;
}
/**
* Sets the paint used to draw the separator between sections and sends a
* change event to all registered listeners.
*
* @param paint the paint ({@code null} not permitted).
*
* @see #getSeparatorPaint()
*/
public void setSeparatorPaint(Paint paint) {
ParamChecks.nullNotPermitted(paint, "paint");
this.separatorPaint = paint;
fireChangeEvent();
}
/**
* Returns the length of the inner extension of the separator line that
* is drawn between sections, expressed as a percentage of the depth of
* the section.
*
* @return The inner separator extension (as a percentage).
*
* @see #setInnerSeparatorExtension(double)
*/
public double getInnerSeparatorExtension() {
return this.innerSeparatorExtension;
}
/**
* Sets the length of the inner extension of the separator line that is
* drawn between sections, as a percentage of the depth of the
* sections, and sends a change event to all registered listeners.
*
* @param percent the percentage.
*
* @see #getInnerSeparatorExtension()
* @see #setOuterSeparatorExtension(double)
*/
public void setInnerSeparatorExtension(double percent) {
this.innerSeparatorExtension = percent;
fireChangeEvent();
}
/**
* Returns the length of the outer extension of the separator line that
* is drawn between sections, expressed as a percentage of the depth of
* the section.
*
* @return The outer separator extension (as a percentage).
*
* @see #setOuterSeparatorExtension(double)
*/
public double getOuterSeparatorExtension() {
return this.outerSeparatorExtension;
}
/**
* Sets the length of the outer extension of the separator line that is
* drawn between sections, as a percentage of the depth of the
* sections, and sends a change event to all registered listeners.
*
* @param percent the percentage.
*
* @see #getOuterSeparatorExtension()
*/
public void setOuterSeparatorExtension(double percent) {
this.outerSeparatorExtension = percent;
fireChangeEvent();
}
/**
* Returns the depth of each section, expressed as a percentage of the
* plot radius.
*
* @return The depth of each section.
*
* @see #setSectionDepth(double)
* @since 1.0.3
*/
public double getSectionDepth() {
return this.sectionDepth;
}
/**
* The section depth is given as percentage of the plot radius.
* Specifying 1.0 results in a straightforward pie chart.
*
* @param sectionDepth the section depth.
*
* @see #getSectionDepth()
* @since 1.0.3
*/
public void setSectionDepth(double sectionDepth) {
this.sectionDepth = sectionDepth;
fireChangeEvent();
}
/**
* Initialises the plot state (which will store the total of all dataset
* values, among other things). This method is called once at the
* beginning of each drawing.
*
* @param g2 the graphics device.
* @param plotArea the plot area ({@code null} not permitted).
* @param plot the plot.
* @param index the secondary index ({@code null} for primary
* renderer).
* @param info collects chart rendering information for return to caller.
*
* @return A state object (maintains state information relevant to one
* chart drawing).
*/
@Override
public PiePlotState initialise(Graphics2D g2, Rectangle2D plotArea,
PiePlot plot, Integer index, PlotRenderingInfo info) {
PiePlotState state = super.initialise(g2, plotArea, plot, index, info);
state.setPassesRequired(3);
return state;
}
/**
* Draws a single data item.
*
* @param g2 the graphics device ({@code null} not permitted).
* @param section the section index.
* @param dataArea the data plot area.
* @param state state information for one chart.
* @param currentPass the current pass index.
*/
@Override
protected void drawItem(Graphics2D g2, int section, Rectangle2D dataArea,
PiePlotState state, int currentPass) {
PieDataset dataset = getDataset();
Number n = dataset.getValue(section);
if (n == null) {
return;
}
double value = n.doubleValue();
double angle1 = 0.0;
double angle2 = 0.0;
Rotation direction = getDirection();
if (direction == Rotation.CLOCKWISE) {
angle1 = state.getLatestAngle();
angle2 = angle1 - value / state.getTotal() * 360.0;
}
else if (direction == Rotation.ANTICLOCKWISE) {
angle1 = state.getLatestAngle();
angle2 = angle1 + value / state.getTotal() * 360.0;
}
else {
throw new IllegalStateException("Rotation type not recognised.");
}
double angle = (angle2 - angle1);
if (Math.abs(angle) > getMinimumArcAngleToDraw()) {
Comparable key = getSectionKey(section);
double ep = 0.0;
double mep = getMaximumExplodePercent();
if (mep > 0.0) {
ep = getExplodePercent(key) / mep;
}
Rectangle2D arcBounds = getArcBounds(state.getPieArea(),
state.getExplodedPieArea(), angle1, angle, ep);
Arc2D.Double arc = new Arc2D.Double(arcBounds, angle1, angle,
Arc2D.OPEN);
// create the bounds for the inner arc
double depth = this.sectionDepth / 2.0;
RectangleInsets s = new RectangleInsets(UnitType.RELATIVE,
depth, depth, depth, depth);
Rectangle2D innerArcBounds = new Rectangle2D.Double();
innerArcBounds.setRect(arcBounds);
s.trim(innerArcBounds);
// calculate inner arc in reverse direction, for later
// GeneralPath construction
Arc2D.Double arc2 = new Arc2D.Double(innerArcBounds, angle1
+ angle, -angle, Arc2D.OPEN);
GeneralPath path = new GeneralPath();
path.moveTo((float) arc.getStartPoint().getX(),
(float) arc.getStartPoint().getY());
path.append(arc.getPathIterator(null), false);
path.append(arc2.getPathIterator(null), true);
path.closePath();
Line2D separator = new Line2D.Double(arc2.getEndPoint(),
arc.getStartPoint());
if (currentPass == 0) {
Paint shadowPaint = getShadowPaint();
double shadowXOffset = getShadowXOffset();
double shadowYOffset = getShadowYOffset();
if (shadowPaint != null && getShadowGenerator() == null) {
Shape shadowArc = ShapeUtils.createTranslatedShape(
path, (float) shadowXOffset, (float) shadowYOffset);
g2.setPaint(shadowPaint);
g2.fill(shadowArc);
}
}
else if (currentPass == 1) {
Paint paint = lookupSectionPaint(key);
g2.setPaint(paint);
g2.fill(path);
Paint outlinePaint = lookupSectionOutlinePaint(key);
Stroke outlineStroke = lookupSectionOutlineStroke(key);
if (getSectionOutlinesVisible() && outlinePaint != null
&& outlineStroke != null) {
g2.setPaint(outlinePaint);
g2.setStroke(outlineStroke);
g2.draw(path);
}
if (section == 0) {
String nstr = null;
if (this.centerTextMode.equals(CenterTextMode.VALUE)) {
nstr = this.centerTextFormatter.format(n);
} else if (this.centerTextMode.equals(CenterTextMode.FIXED)) {
nstr = this.centerText;
}
if (nstr != null) {
g2.setFont(this.centerTextFont);
g2.setPaint(this.centerTextColor);
TextUtils.drawAlignedString(nstr, g2,
(float) dataArea.getCenterX(),
(float) dataArea.getCenterY(),
TextAnchor.CENTER);
}
}
// add an entity for the pie section
if (state.getInfo() != null) {
EntityCollection entities = state.getEntityCollection();
if (entities != null) {
String tip = null;
PieToolTipGenerator toolTipGenerator
= getToolTipGenerator();
if (toolTipGenerator != null) {
tip = toolTipGenerator.generateToolTip(dataset,
key);
}
String url = null;
PieURLGenerator urlGenerator = getURLGenerator();
if (urlGenerator != null) {
url = urlGenerator.generateURL(dataset, key,
getPieIndex());
}
PieSectionEntity entity = new PieSectionEntity(path,
dataset, getPieIndex(), section, key, tip,
url);
entities.add(entity);
}
}
}
else if (currentPass == 2) {
if (this.separatorsVisible) {
Line2D extendedSeparator = LineUtils.extendLine(
separator, this.innerSeparatorExtension,
this.outerSeparatorExtension);
g2.setStroke(this.separatorStroke);
g2.setPaint(this.separatorPaint);
g2.draw(extendedSeparator);
}
}
}
state.setLatestAngle(angle2);
}
/**
* This method overrides the default value for cases where the ring plot
* is very thin. This fixes bug 2121818.
*
* @return The label link depth, as a percentage of the plot's radius.
*/
@Override
protected double getLabelLinkDepth() {
return Math.min(super.getLabelLinkDepth(), getSectionDepth() / 2);
}
/**
* Tests this plot for equality with an arbitrary object.
*
* @param obj the object to test against ({@code null} permitted).
*
* @return A boolean.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof RingPlot)) {
return false;
}
RingPlot that = (RingPlot) obj;
if (!this.centerTextMode.equals(that.centerTextMode)) {
return false;
}
if (!ObjectUtils.equal(this.centerText, that.centerText)) {
return false;
}
if (!this.centerTextFormatter.equals(that.centerTextFormatter)) {
return false;
}
if (!this.centerTextFont.equals(that.centerTextFont)) {
return false;
}
if (!this.centerTextColor.equals(that.centerTextColor)) {
return false;
}
if (this.separatorsVisible != that.separatorsVisible) {
return false;
}
if (!ObjectUtils.equal(this.separatorStroke,
that.separatorStroke)) {
return false;
}
if (!PaintUtils.equal(this.separatorPaint, that.separatorPaint)) {
return false;
}
if (this.innerSeparatorExtension != that.innerSeparatorExtension) {
return false;
}
if (this.outerSeparatorExtension != that.outerSeparatorExtension) {
return false;
}
if (this.sectionDepth != that.sectionDepth) {
return false;
}
return super.equals(obj);
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtils.writeStroke(this.separatorStroke, stream);
SerialUtils.writePaint(this.separatorPaint, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.separatorStroke = SerialUtils.readStroke(stream);
this.separatorPaint = SerialUtils.readPaint(stream);
}
}