-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
ChartFactory.java
2302 lines (2110 loc) · 98.6 KB
/
ChartFactory.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ===========================================================
* 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.]
*
* -----------------
* ChartFactory.java
* -----------------
* (C) Copyright 2001-2017, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): Serge V. Grachov;
* Joao Guilherme Del Valle;
* Bill Kelemen;
* Jon Iles;
* Jelai Wang;
* Richard Atkinson;
* David Browning (for Australian Institute of Marine
* Science);
* Benoit Xhenseval;
*
* Changes
* -------
* 19-Oct-2001 : Version 1, most methods transferred from JFreeChart.java (DG);
* 22-Oct-2001 : Added methods to create stacked bar charts (DG);
* Renamed DataSource.java --> Dataset.java etc. (DG);
* 31-Oct-2001 : Added 3D-effect vertical bar and stacked-bar charts,
* contributed by Serge V. Grachov (DG);
* 07-Nov-2001 : Added a flag to control whether or not a legend is added to
* the chart (DG);
* 17-Nov-2001 : For pie chart, changed dataset from CategoryDataset to
* PieDataset (DG);
* 30-Nov-2001 : Removed try/catch handlers from chart creation, as the
* exception are now RuntimeExceptions, as suggested by Joao
* Guilherme Del Valle (DG);
* 06-Dec-2001 : Added createCombinableXXXXXCharts methods (BK);
* 12-Dec-2001 : Added createCandlestickChart() method (DG);
* 13-Dec-2001 : Updated methods for charts with new renderers (DG);
* 08-Jan-2002 : Added import for
* com.jrefinery.chart.combination.CombinedChart (DG);
* 31-Jan-2002 : Changed the createCombinableVerticalXYBarChart() method to use
* renderer (DG);
* 06-Feb-2002 : Added new method createWindPlot() (DG);
* 23-Apr-2002 : Updates to the chart and plot constructor API (DG);
* 21-May-2002 : Added new method createAreaChart() (JI);
* 06-Jun-2002 : Added new method createGanttChart() (DG);
* 11-Jun-2002 : Renamed createHorizontalStackedBarChart()
* --> createStackedHorizontalBarChart() for consistency (DG);
* 06-Aug-2002 : Updated Javadoc comments (DG);
* 21-Aug-2002 : Added createPieChart(CategoryDataset) method (DG);
* 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
* 09-Oct-2002 : Added methods including tooltips and URL flags (DG);
* 06-Nov-2002 : Moved renderers into a separate package (DG);
* 18-Nov-2002 : Changed CategoryDataset to TableDataset (DG);
* 21-Mar-2003 : Incorporated HorizontalCategoryAxis3D, see bug id 685501 (DG);
* 13-May-2003 : Merged some horizontal and vertical methods (DG);
* 24-May-2003 : Added support for timeline in createHighLowChart (BK);
* 07-Jul-2003 : Added createHistogram() method contributed by Jelai Wang (DG);
* 27-Jul-2003 : Added createStackedAreaXYChart() method (RA);
* 05-Aug-2003 : added new method createBoxAndWhiskerChart (DB);
* 08-Sep-2003 : Changed ValueAxis API (DG);
* 07-Oct-2003 : Added stepped area XY chart contributed by Matthias Rose (DG);
* 06-Nov-2003 : Added createWaterfallChart() method (DG);
* 20-Nov-2003 : Set rendering order for 3D bar charts to fix overlapping
* problems (DG);
* 25-Nov-2003 : Added createWaferMapChart() method (DG);
* 23-Dec-2003 : Renamed createPie3DChart() --> createPieChart3D for
* consistency (DG);
* 20-Jan-2004 : Added createPolarChart() method (DG);
* 28-Jan-2004 : Fixed bug (882890) with axis range in
* createStackedXYAreaChart() method (DG);
* 25-Feb-2004 : Renamed XYToolTipGenerator --> XYItemLabelGenerator (DG);
* 11-Mar-2004 : Updated for pie chart changes (DG);
* 27-Apr-2004 : Added new createPieChart() method contributed by Benoit
* Xhenseval (see RFE 942195) (DG);
* 11-May-2004 : Split StandardCategoryItemLabelGenerator
* --> StandardCategoryToolTipGenerator and
* StandardCategoryLabelGenerator (DG);
* 06-Jan-2005 : Removed deprecated methods (DG);
* 27-Jan-2005 : Added new constructor to LineAndShapeRenderer (DG);
* 28-Feb-2005 : Added docs to createBubbleChart() method (DG);
* 17-Mar-2005 : Added createRingPlot() method (DG);
* 21-Apr-2005 : Replaced Insets with RectangleInsets (DG);
* 29-Nov-2005 : Removed signal chart (DG);
* ------------- JFREECHART 1.0.x ---------------------------------------------
* 26-Jan-2006 : Corrected API docs for createScatterPlot() (DG);
* 23-Aug-2006 : Modified createStackedXYAreaChart() to use
* StackedXYAreaRenderer2, because StackedXYAreaRenderer doesn't
* handle negative values (DG);
* 27-Sep-2006 : Update createPieChart() method for deprecated code (DG);
* 29-Nov-2006 : Update createXYBarChart() to use a time based tool tip
* generator is a DateAxis is requested (DG);
* 17-Jan-2007 : Added createBoxAndWhiskerChart() method from patch 1603937
* submitted by Darren Jung (DG);
* 10-Jul-2007 : Added new methods to create pie charts with locale for
* section label and tool tip formatting (DG);
* 14-Aug-2008 : Added ChartTheme facility (DG);
* 23-Oct-2008 : Check for legacy theme in setChartTheme() and reset default
* bar painters (DG);
* 20-Dec-2008 : In createStackedAreaChart(), set category margin to 0.0 (DG);
* 02-Jul-2013 : Use ParamChecks class (DG);
*
*/
package org.jfree.chart;
import java.awt.Color;
import java.awt.Font;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.Timeline;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator;
import org.jfree.chart.labels.HighLowItemLabelGenerator;
import org.jfree.chart.labels.IntervalCategoryToolTipGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.PieToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.StandardXYZToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PolarPlot;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.WaferMapPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.DefaultPolarItemRenderer;
import org.jfree.chart.renderer.WaferMapRenderer;
import org.jfree.chart.renderer.category.AreaRenderer;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.GanttRenderer;
import org.jfree.chart.renderer.category.GradientBarPainter;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedAreaRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.category.WaterfallBarRenderer;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.renderer.xy.GradientXYBarPainter;
import org.jfree.chart.renderer.xy.HighLowRenderer;
import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.WindItemRenderer;
import org.jfree.chart.renderer.xy.XYAreaRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYBoxAndWhiskerRenderer;
import org.jfree.chart.renderer.xy.XYBubbleRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.Layer;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.urls.PieURLGenerator;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.chart.urls.StandardXYURLGenerator;
import org.jfree.chart.urls.StandardXYZURLGenerator;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.SortOrder;
import org.jfree.chart.util.TableOrder;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.general.WaferMapDataset;
import org.jfree.data.statistics.BoxAndWhiskerCategoryDataset;
import org.jfree.data.statistics.BoxAndWhiskerXYDataset;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.TableXYDataset;
import org.jfree.data.xy.WindDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYZDataset;
/**
* A collection of utility methods for creating some standard charts with
* JFreeChart.
*/
public abstract class ChartFactory {
/** The chart theme. */
private static ChartTheme currentTheme = new StandardChartTheme("JFree");
/**
* Returns the current chart theme used by the factory.
*
* @return The chart theme.
*
* @see #setChartTheme(ChartTheme)
* @see ChartUtils#applyCurrentTheme(JFreeChart)
*
* @since 1.0.11
*/
public static ChartTheme getChartTheme() {
return currentTheme;
}
/**
* Sets the current chart theme. This will be applied to all new charts
* created via methods in this class.
*
* @param theme the theme ({@code null} not permitted).
*
* @see #getChartTheme()
* @see ChartUtils#applyCurrentTheme(JFreeChart)
*
* @since 1.0.11
*/
public static void setChartTheme(ChartTheme theme) {
Args.nullNotPermitted(theme, "theme");
currentTheme = theme;
// here we do a check to see if the user is installing the "Legacy"
// theme, and reset the bar painters in that case...
if (theme instanceof StandardChartTheme) {
StandardChartTheme sct = (StandardChartTheme) theme;
if (sct.getName().equals("Legacy")) {
BarRenderer.setDefaultBarPainter(new StandardBarPainter());
XYBarRenderer.setDefaultBarPainter(new StandardXYBarPainter());
}
else {
BarRenderer.setDefaultBarPainter(new GradientBarPainter());
XYBarRenderer.setDefaultBarPainter(new GradientXYBarPainter());
}
}
}
/**
* Creates a pie chart with default settings.
* <P>
* The chart object returned by this method uses a {@link PiePlot} instance
* as the plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param locale the locale ({@code null} not permitted).
*
* @return A pie chart.
*
* @since 1.0.7
*/
public static JFreeChart createPieChart(String title, PieDataset dataset,
boolean legend, boolean tooltips, Locale locale) {
PiePlot plot = new PiePlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a pie chart with default settings.
* <P>
* The chart object returned by this method uses a {@link PiePlot} instance
* as the plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
*
* @return A pie chart.
*
* @since 1.0.16
*/
public static JFreeChart createPieChart(String title, PieDataset dataset) {
return createPieChart(title, dataset, true, true, false);
}
/**
* Creates a pie chart with default settings.
* <P>
* The chart object returned by this method uses a {@link PiePlot} instance
* as the plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return A pie chart.
*/
public static JFreeChart createPieChart(String title, PieDataset dataset,
boolean legend, boolean tooltips, boolean urls) {
PiePlot plot = new PiePlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
}
if (urls) {
plot.setURLGenerator(new StandardPieURLGenerator());
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a pie chart with default settings that compares 2 datasets.
* The colour of each section will be determined by the move from the value
* for the same key in {@code previousDataset}. ie if value1 >
* value2 then the section will be in green (unless
* {@code greenForIncrease} is {@code false}, in which case it
* would be {@code red}). Each section can have a shade of red or
* green as the difference can be tailored between 0% (black) and
* percentDiffForMaxScale% (bright red/green).
* <p>
* For instance if {@code percentDiffForMaxScale} is 10 (10%), a
* difference of 5% will have a half shade of red/green, a difference of
* 10% or more will have a maximum shade/brightness of red/green.
* <P>
* The chart object returned by this method uses a {@link PiePlot} instance
* as the plot.
* <p>
* Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
* Xhenseval</a>.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param previousDataset the dataset for the last run, this will be used
* to compare each key in the dataset
* @param percentDiffForMaxScale scale goes from bright red/green to black,
* percentDiffForMaxScale indicate the change
* required to reach top scale.
* @param greenForIncrease an increase since previousDataset will be
* displayed in green (decrease red) if true.
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param locale the locale ({@code null} not permitted).
* @param subTitle displays a subtitle with colour scheme if true
* @param showDifference create a new dataset that will show the %
* difference between the two datasets.
*
* @return A pie chart.
*
* @since 1.0.7
*/
public static JFreeChart createPieChart(String title, PieDataset dataset,
PieDataset previousDataset, int percentDiffForMaxScale,
boolean greenForIncrease, boolean legend, boolean tooltips,
Locale locale, boolean subTitle, boolean showDifference) {
PiePlot plot = new PiePlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
}
List keys = dataset.getKeys();
DefaultPieDataset series = null;
if (showDifference) {
series = new DefaultPieDataset();
}
double colorPerPercent = 255.0 / percentDiffForMaxScale;
for (Iterator it = keys.iterator(); it.hasNext();) {
Comparable key = (Comparable) it.next();
Number newValue = dataset.getValue(key);
Number oldValue = previousDataset.getValue(key);
if (oldValue == null) {
if (greenForIncrease) {
plot.setSectionPaint(key, Color.GREEN);
}
else {
plot.setSectionPaint(key, Color.RED);
}
if (showDifference) {
assert series != null; // suppresses compiler warning
series.setValue(key + " (+100%)", newValue);
}
}
else {
double percentChange = (newValue.doubleValue()
/ oldValue.doubleValue() - 1.0) * 100.0;
double shade
= (Math.abs(percentChange) >= percentDiffForMaxScale ? 255
: Math.abs(percentChange) * colorPerPercent);
if (greenForIncrease
&& newValue.doubleValue() > oldValue.doubleValue()
|| !greenForIncrease && newValue.doubleValue()
< oldValue.doubleValue()) {
plot.setSectionPaint(key, new Color(0, (int) shade, 0));
}
else {
plot.setSectionPaint(key, new Color((int) shade, 0, 0));
}
if (showDifference) {
assert series != null; // suppresses compiler warning
series.setValue(key + " (" + (percentChange >= 0 ? "+" : "")
+ NumberFormat.getPercentInstance().format(
percentChange / 100.0) + ")", newValue);
}
}
}
if (showDifference) {
plot.setDataset(series);
}
JFreeChart chart = new JFreeChart(title,
JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
if (subTitle) {
TextTitle subtitle = new TextTitle("Bright " + (greenForIncrease
? "red" : "green") + "=change >=-" + percentDiffForMaxScale
+ "%, Bright " + (!greenForIncrease ? "red" : "green")
+ "=change >=+" + percentDiffForMaxScale + "%",
new Font("SansSerif", Font.PLAIN, 10));
chart.addSubtitle(subtitle);
}
currentTheme.apply(chart);
return chart;
}
/**
* Creates a pie chart with default settings that compares 2 datasets.
* The colour of each section will be determined by the move from the value
* for the same key in {@code previousDataset}. ie if value1 >
* value2 then the section will be in green (unless
* {@code greenForIncrease} is {@code false}, in which case it
* would be {@code red}). Each section can have a shade of red or
* green as the difference can be tailored between 0% (black) and
* percentDiffForMaxScale% (bright red/green).
* <p>
* For instance if {@code percentDiffForMaxScale} is 10 (10%), a
* difference of 5% will have a half shade of red/green, a difference of
* 10% or more will have a maximum shade/brightness of red/green.
* <P>
* The chart object returned by this method uses a {@link PiePlot} instance
* as the plot.
* <p>
* Written by <a href="mailto:opensource@objectlab.co.uk">Benoit
* Xhenseval</a>.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param previousDataset the dataset for the last run, this will be used
* to compare each key in the dataset
* @param percentDiffForMaxScale scale goes from bright red/green to black,
* percentDiffForMaxScale indicate the change
* required to reach top scale.
* @param greenForIncrease an increase since previousDataset will be
* displayed in green (decrease red) if true.
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
* @param subTitle displays a subtitle with colour scheme if true
* @param showDifference create a new dataset that will show the %
* difference between the two datasets.
*
* @return A pie chart.
*/
public static JFreeChart createPieChart(String title, PieDataset dataset,
PieDataset previousDataset, int percentDiffForMaxScale,
boolean greenForIncrease, boolean legend, boolean tooltips,
boolean urls, boolean subTitle, boolean showDifference) {
PiePlot plot = new PiePlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
}
if (urls) {
plot.setURLGenerator(new StandardPieURLGenerator());
}
List keys = dataset.getKeys();
DefaultPieDataset series = null;
if (showDifference) {
series = new DefaultPieDataset();
}
double colorPerPercent = 255.0 / percentDiffForMaxScale;
for (Iterator it = keys.iterator(); it.hasNext();) {
Comparable key = (Comparable) it.next();
Number newValue = dataset.getValue(key);
Number oldValue = previousDataset.getValue(key);
if (oldValue == null) {
if (greenForIncrease) {
plot.setSectionPaint(key, Color.GREEN);
}
else {
plot.setSectionPaint(key, Color.RED);
}
if (showDifference) {
assert series != null; // suppresses compiler warning
series.setValue(key + " (+100%)", newValue);
}
}
else {
double percentChange = (newValue.doubleValue()
/ oldValue.doubleValue() - 1.0) * 100.0;
double shade
= (Math.abs(percentChange) >= percentDiffForMaxScale ? 255
: Math.abs(percentChange) * colorPerPercent);
if (greenForIncrease
&& newValue.doubleValue() > oldValue.doubleValue()
|| !greenForIncrease && newValue.doubleValue()
< oldValue.doubleValue()) {
plot.setSectionPaint(key, new Color(0, (int) shade, 0));
}
else {
plot.setSectionPaint(key, new Color((int) shade, 0, 0));
}
if (showDifference) {
assert series != null; // suppresses compiler warning
series.setValue(key + " (" + (percentChange >= 0 ? "+" : "")
+ NumberFormat.getPercentInstance().format(
percentChange / 100.0) + ")", newValue);
}
}
}
if (showDifference) {
plot.setDataset(series);
}
JFreeChart chart = new JFreeChart(title,
JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
if (subTitle) {
TextTitle subtitle = new TextTitle("Bright " + (greenForIncrease
? "red" : "green") + "=change >=-" + percentDiffForMaxScale
+ "%, Bright " + (!greenForIncrease ? "red" : "green")
+ "=change >=+" + percentDiffForMaxScale + "%",
new Font("SansSerif", Font.PLAIN, 10));
chart.addSubtitle(subtitle);
}
currentTheme.apply(chart);
return chart;
}
/**
* Creates a ring chart with default settings.
* <P>
* The chart object returned by this method uses a {@link RingPlot}
* instance as the plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param locale the locale ({@code null} not permitted).
*
* @return A ring chart.
*
* @since 1.0.7
*/
public static JFreeChart createRingChart(String title, PieDataset dataset,
boolean legend, boolean tooltips, Locale locale) {
RingPlot plot = new RingPlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(locale));
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a ring chart with default settings.
* <P>
* The chart object returned by this method uses a {@link RingPlot}
* instance as the plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return A ring chart.
*/
public static JFreeChart createRingChart(String title, PieDataset dataset,
boolean legend, boolean tooltips, boolean urls) {
RingPlot plot = new RingPlot(dataset);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator());
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
}
if (urls) {
plot.setURLGenerator(new StandardPieURLGenerator());
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a chart that displays multiple pie plots. The chart object
* returned by this method uses a {@link MultiplePiePlot} instance as the
* plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset ({@code null} permitted).
* @param order the order that the data is extracted (by row or by column)
* ({@code null} not permitted).
* @param legend include a legend?
* @param tooltips generate tooltips?
* @param urls generate URLs?
*
* @return A chart.
*/
public static JFreeChart createMultiplePieChart(String title,
CategoryDataset dataset, TableOrder order, boolean legend,
boolean tooltips, boolean urls) {
Args.nullNotPermitted(order, "order");
MultiplePiePlot plot = new MultiplePiePlot(dataset);
plot.setDataExtractOrder(order);
plot.setBackgroundPaint(null);
plot.setOutlineStroke(null);
if (tooltips) {
PieToolTipGenerator tooltipGenerator
= new StandardPieToolTipGenerator();
PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
pp.setToolTipGenerator(tooltipGenerator);
}
if (urls) {
PieURLGenerator urlGenerator = new StandardPieURLGenerator();
PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
pp.setURLGenerator(urlGenerator);
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a 3D pie chart using the specified dataset. The chart object
* returned by this method uses a {@link PiePlot3D} instance as the
* plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param locale the locale ({@code null} not permitted).
*
* @return A pie chart.
*
* @since 1.0.7
*/
public static JFreeChart createPieChart3D(String title, PieDataset dataset,
boolean legend, boolean tooltips, Locale locale) {
Args.nullNotPermitted(locale, "locale");
PiePlot3D plot = new PiePlot3D(dataset);
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator(locale));
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a 3D pie chart using the specified dataset. The chart object
* returned by this method uses a {@link PiePlot3D} instance as the
* plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
*
* @return A pie chart.
*
* @since 1.0.16
*/
public static JFreeChart createPieChart3D(String title,
PieDataset dataset) {
return createPieChart3D(title, dataset, true, true, false);
}
/**
* Creates a 3D pie chart using the specified dataset. The chart object
* returned by this method uses a {@link PiePlot3D} instance as the
* plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return A pie chart.
*/
public static JFreeChart createPieChart3D(String title, PieDataset dataset,
boolean legend, boolean tooltips, boolean urls) {
PiePlot3D plot = new PiePlot3D(dataset);
plot.setInsets(new RectangleInsets(0.0, 5.0, 5.0, 5.0));
if (tooltips) {
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
}
if (urls) {
plot.setURLGenerator(new StandardPieURLGenerator());
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a chart that displays multiple pie plots. The chart object
* returned by this method uses a {@link MultiplePiePlot} instance as the
* plot.
*
* @param title the chart title ({@code null} permitted).
* @param dataset the dataset ({@code null} permitted).
* @param order the order that the data is extracted (by row or by column)
* ({@code null} not permitted).
* @param legend include a legend?
* @param tooltips generate tooltips?
* @param urls generate URLs?
*
* @return A chart.
*/
public static JFreeChart createMultiplePieChart3D(String title,
CategoryDataset dataset, TableOrder order, boolean legend,
boolean tooltips, boolean urls) {
Args.nullNotPermitted(order, "order");
MultiplePiePlot plot = new MultiplePiePlot(dataset);
plot.setDataExtractOrder(order);
plot.setBackgroundPaint(null);
plot.setOutlineStroke(null);
JFreeChart pieChart = new JFreeChart(new PiePlot3D(null));
TextTitle seriesTitle = new TextTitle("Series Title",
new Font("SansSerif", Font.BOLD, 12));
seriesTitle.setPosition(RectangleEdge.BOTTOM);
pieChart.setTitle(seriesTitle);
pieChart.removeLegend();
pieChart.setBackgroundPaint(null);
plot.setPieChart(pieChart);
if (tooltips) {
PieToolTipGenerator tooltipGenerator
= new StandardPieToolTipGenerator();
PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
pp.setToolTipGenerator(tooltipGenerator);
}
if (urls) {
PieURLGenerator urlGenerator = new StandardPieURLGenerator();
PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
pp.setURLGenerator(urlGenerator);
}
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a bar chart with a vertical orientation. The chart object
* returned by this method uses a {@link CategoryPlot} instance as the
* plot, with a {@link CategoryAxis} for the domain axis, a
* {@link NumberAxis} as the range axis, and a {@link BarRenderer} as the
* renderer.
*
* @param title the chart title ({@code null} permitted).
* @param categoryAxisLabel the label for the category axis
* ({@code null} permitted).
* @param valueAxisLabel the label for the value axis
* ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
*
* @return A bar chart.
*
* @since 1.0.16
*/
public static JFreeChart createBarChart(String title,
String categoryAxisLabel, String valueAxisLabel,
CategoryDataset dataset) {
return createBarChart(title, categoryAxisLabel, valueAxisLabel, dataset,
PlotOrientation.VERTICAL, true, true, false);
}
/**
* Creates a bar chart. The chart object returned by this method uses a
* {@link CategoryPlot} instance as the plot, with a {@link CategoryAxis}
* for the domain axis, a {@link NumberAxis} as the range axis, and a
* {@link BarRenderer} as the renderer.
*
* @param title the chart title ({@code null} permitted).
* @param categoryAxisLabel the label for the category axis
* ({@code null} permitted).
* @param valueAxisLabel the label for the value axis
* ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param orientation the plot orientation (horizontal or vertical)
* ({@code null} not permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return A bar chart.
*/
public static JFreeChart createBarChart(String title,
String categoryAxisLabel, String valueAxisLabel,
CategoryDataset dataset, PlotOrientation orientation,
boolean legend, boolean tooltips, boolean urls) {
Args.nullNotPermitted(orientation, "orientation");
CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
BarRenderer renderer = new BarRenderer();
if (orientation == PlotOrientation.HORIZONTAL) {
ItemLabelPosition position1 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT);
renderer.setDefaultPositiveItemLabelPosition(position1);
ItemLabelPosition position2 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT);
renderer.setDefaultNegativeItemLabelPosition(position2);
} else if (orientation == PlotOrientation.VERTICAL) {
ItemLabelPosition position1 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
renderer.setDefaultPositiveItemLabelPosition(position1);
ItemLabelPosition position2 = new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER);
renderer.setDefaultNegativeItemLabelPosition(position2);
}
if (tooltips) {
renderer.setDefaultToolTipGenerator(
new StandardCategoryToolTipGenerator());
}
if (urls) {
renderer.setDefaultItemURLGenerator(
new StandardCategoryURLGenerator());
}
CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis,
renderer);
plot.setOrientation(orientation);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}
/**
* Creates a stacked bar chart with default settings. The chart object
* returned by this method uses a {@link CategoryPlot} instance as the
* plot, with a {@link CategoryAxis} for the domain axis, a
* {@link NumberAxis} as the range axis, and a {@link StackedBarRenderer}
* as the renderer.
*
* @param title the chart title ({@code null} permitted).
* @param domainAxisLabel the label for the category axis
* ({@code null} permitted).
* @param rangeAxisLabel the label for the value axis
* ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
*
* @return A stacked bar chart.
*
* @since 1.0.16
*/
public static JFreeChart createStackedBarChart(String title,
String domainAxisLabel, String rangeAxisLabel,
CategoryDataset dataset) {
return createStackedBarChart(title, domainAxisLabel, rangeAxisLabel,
dataset, PlotOrientation.VERTICAL, true, true, false);
}
/**
* Creates a stacked bar chart with default settings. The chart object
* returned by this method uses a {@link CategoryPlot} instance as the
* plot, with a {@link CategoryAxis} for the domain axis, a
* {@link NumberAxis} as the range axis, and a {@link StackedBarRenderer}
* as the renderer.
*
* @param title the chart title ({@code null} permitted).
* @param domainAxisLabel the label for the category axis
* ({@code null} permitted).
* @param rangeAxisLabel the label for the value axis
* ({@code null} permitted).
* @param dataset the dataset for the chart ({@code null} permitted).
* @param orientation the orientation of the chart (horizontal or
* vertical) ({@code null} not permitted).
* @param legend a flag specifying whether or not a legend is required.
* @param tooltips configure chart to generate tool tips?
* @param urls configure chart to generate URLs?
*
* @return A stacked bar chart.
*/
public static JFreeChart createStackedBarChart(String title,
String domainAxisLabel, String rangeAxisLabel,
CategoryDataset dataset, PlotOrientation orientation,
boolean legend, boolean tooltips, boolean urls) {
Args.nullNotPermitted(orientation, "orientation");
CategoryAxis categoryAxis = new CategoryAxis(domainAxisLabel);
ValueAxis valueAxis = new NumberAxis(rangeAxisLabel);
StackedBarRenderer renderer = new StackedBarRenderer();
if (tooltips) {
renderer.setDefaultToolTipGenerator(
new StandardCategoryToolTipGenerator());
}
if (urls) {
renderer.setDefaultItemURLGenerator(
new StandardCategoryURLGenerator());
}
CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis,
renderer);
plot.setOrientation(orientation);
JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
plot, legend);
currentTheme.apply(chart);
return chart;
}