1
1
package org .knowm .xchart .demo .charts .pie ;
2
2
3
3
import java .awt .Color ;
4
- import java .util .Collection ;
5
-
6
4
import org .knowm .xchart .PieChart ;
7
5
import org .knowm .xchart .PieChartBuilder ;
8
- import org .knowm .xchart .PieSeries ;
9
6
import org .knowm .xchart .SwingWrapper ;
10
7
import org .knowm .xchart .demo .charts .ExampleChart ;
11
- import org .knowm .xchart .style .LabelGenerator ;
12
8
import org .knowm .xchart .style .PieStyler .LabelType ;
13
9
14
10
/**
20
16
* <li>Pie Chart
21
17
* <li>PieChartBuilder
22
18
* <li>Custom series palette
23
- * <li>Custom label type
24
19
* <li>Value Annotations
25
20
* <li>Tooltips
26
21
*/
@@ -40,13 +35,6 @@ public PieChart getChart() {
40
35
PieChart chart =
41
36
new PieChartBuilder ().width (800 ).height (600 ).title (getClass ().getSimpleName ()).build ();
42
37
43
- // Series
44
- chart .addSeries ("Gold" , 24 );
45
- chart .addSeries ("Silver" , 21 );
46
- chart .addSeries ("Platinum" , 39 );
47
- chart .addSeries ("Copper" , 17 );
48
- chart .addSeries ("Zinc" , 40 );
49
-
50
38
// Customize Chart
51
39
Color [] sliceColors =
52
40
new Color [] {
@@ -57,12 +45,18 @@ public PieChart getChart() {
57
45
new Color (246 , 199 , 182 )
58
46
};
59
47
chart .getStyler ().setSeriesColors (sliceColors );
60
- chart .getStyler ().setLabelType (LabelType .Custom );
61
- chart .getStyler ().setLabelGenerator (new CustomLabelGenerator (chart .getSeriesMap ().values ()));
48
+ chart .getStyler ().setLabelType (LabelType .Value );
62
49
// chart.getStyler().setDecimalPattern("#0.000");
63
50
chart .getStyler ().setToolTipsEnabled (true );
64
51
// chart.getStyler().setToolTipsAlwaysVisible(true);
65
52
53
+ // Series
54
+ chart .addSeries ("Gold" , 24 );
55
+ chart .addSeries ("Silver" , 21 );
56
+ chart .addSeries ("Platinum" , 39 );
57
+ chart .addSeries ("Copper" , 17 );
58
+ chart .addSeries ("Zinc" , 40 );
59
+
66
60
return chart ;
67
61
}
68
62
@@ -71,21 +65,4 @@ public String getExampleChartName() {
71
65
72
66
return getClass ().getSimpleName () + " - Pie Chart Custom Color Palette" ;
73
67
}
74
-
75
- private static class CustomLabelGenerator implements LabelGenerator {
76
-
77
- private final double total ;
78
-
79
- public CustomLabelGenerator (Collection <PieSeries > values ) {
80
-
81
- this .total = values .stream ().map (PieSeries ::getValue ).mapToDouble (Number ::doubleValue ).sum ();
82
- }
83
-
84
- @ Override
85
- public String generateSeriesLabel (PieSeries series ) {
86
-
87
- double percent = (series .getValue ().doubleValue () / total ) * 100 ;
88
- return String .format ("%s (%.2f%%)" , series .getValue (), percent );
89
- }
90
- }
91
68
}
0 commit comments