Debian Codegraph @

src/main/java/org/jfree/svg/demo/SVGPieChartDemo1.java

raw
1 /* =====================
2 * SVGPieChartDemo1.java
3 * =====================
4 *
5 * Copyright (c) 2013-2021, Object Refinery Limited.
6 * All rights reserved.
7 *
8 * http://www.jfree.org/jfreesvg/index.html
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * - Neither the name of the Object Refinery Limited nor the
18 * names of its contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 package org.jfree.svg.demo;
35
36 import java.awt.BasicStroke;
37 import java.awt.Color;
38 import java.awt.Font;
39 import java.awt.RadialGradientPaint;
40 import java.awt.Rectangle;
41 import java.awt.geom.Point2D;
42 import java.io.File;
43 import java.io.IOException;
44 import org.jfree.chart.ChartFactory;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.StandardChartTheme;
47 //import org.jfree.chart.drawable.GradientPainter;
48 import org.jfree.chart.plot.PiePlot;
49 import org.jfree.chart.title.TextTitle;
50 import org.jfree.chart.ui.HorizontalAlignment;
51 import org.jfree.chart.ui.RectangleEdge;
52 import org.jfree.data.general.DefaultPieDataset;
53 import org.jfree.data.general.PieDataset;
54 import org.jfree.svg.SVGGraphics2D;
55 import org.jfree.svg.SVGUtils;
56
57 /**
58 * A demo/test for a pie chart.
59 */
60 public class SVGPieChartDemo1 {
61
62 static {
63 // set a theme using the new shadow generator feature available in
64 // 1.0.14 - for backwards compatibility it is not enabled by default
65 ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow",
66 true));
67 }
68
69 /**
70 * Creates a sample dataset.
71 *
72 * Source: http://www.bbc.co.uk/news/business-15489523
73 *
74 * @return A sample dataset.
75 */
76 private static PieDataset createDataset() {
77 DefaultPieDataset dataset = new DefaultPieDataset();
78 dataset.setValue("Samsung", new Double(27.8));
79 dataset.setValue("Others", new Double(55.3));
80 dataset.setValue("Nokia", new Double(16.8));
81 dataset.setValue("Apple", new Double(17.1));
82 return dataset;
83 }
84
85 /**
86 * Creates a chart.
87 *
88 * @param dataset the dataset.
89 *
90 * @return A chart.
91 */
92 private static JFreeChart createChart(PieDataset dataset) {
93
94 JFreeChart chart = ChartFactory.createPieChart(
95 "Smart Phones Manufactured / Q3 2011", // chart title
96 dataset);
97 chart.removeLegend();
98
99 // set a custom background for the chart
100 // chart.setBackgroundPainter(new GradientPainter(new Color(20, 20, 20),
101 // RectangleAnchor.TOP_LEFT, Color.DARK_GRAY,
102 // RectangleAnchor.BOTTOM_RIGHT));
103
104 // customise the title position and font
105 TextTitle t = chart.getTitle();
106 t.setHorizontalAlignment(HorizontalAlignment.LEFT);
107 t.setPaint(new Color(240, 240, 240));
108 t.setFont(new Font("Arial", Font.BOLD, 26));
109
110 PiePlot plot = (PiePlot) chart.getPlot();
111 // plot.setBackgroundPainter(null);
112 plot.setInteriorGap(0.04);
113 // plot.setBorderPainter(null);
114
115 // use gradients and white borders for the section colours
116 plot.setSectionPaint("Others", createGradientPaint(new Color(200, 200, 255), Color.BLUE));
117 plot.setSectionPaint("Samsung", createGradientPaint(new Color(255, 200, 200), Color.RED));
118 plot.setSectionPaint("Apple", createGradientPaint(new Color(200, 255, 200), Color.GREEN));
119 plot.setSectionPaint("Nokia", createGradientPaint(new Color(200, 255, 200), Color.YELLOW));
120 plot.setDefaultSectionOutlinePaint(Color.WHITE);
121 plot.setSectionOutlinesVisible(true);
122 plot.setDefaultSectionOutlineStroke(new BasicStroke(2.0f));
123
124 // customise the section label appearance
125 plot.setLabelFont(new Font("Courier New", Font.BOLD, 20));
126 plot.setLabelLinkPaint(Color.WHITE);
127 plot.setLabelLinkStroke(new BasicStroke(2.0f));
128 plot.setLabelOutlineStroke(null);
129 plot.setLabelPaint(Color.WHITE);
130 plot.setLabelBackgroundPaint(null);
131 // add a subtitle giving the data source
132 TextTitle source = new TextTitle("Source: http://www.bbc.co.uk/news/business-15489523",
133 new Font("Courier New", Font.PLAIN, 12));
134 source.setPaint(Color.WHITE);
135 source.setPosition(RectangleEdge.BOTTOM);
136 source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
137 chart.addSubtitle(source);
138 return chart;
139
140 }
141
142 /**
143 * A utility method for creating gradient paints.
144 *
145 * @param c1 color 1.
146 * @param c2 color 2.
147 *
148 * @return A radial gradient paint.
149 */
150 private static RadialGradientPaint createGradientPaint(Color c1, Color c2) {
151 Point2D center = new Point2D.Float(0, 0);
152 float radius = 200;
153 float[] dist = {0.0f, 1.0f};
154 return new RadialGradientPaint(center, radius, dist,
155 new Color[] {c1, c2});
156 }
157
158 /**
159 * Starting point for the demo.
160 *
161 * @param args ignored.
162 *
163 * @throws IOException
164 */
165 public static void main(String[] args) throws IOException {
166 JFreeChart chart = createChart(createDataset());
167 SVGGraphics2D g2 = new SVGGraphics2D(600, 400);
168 g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true);
169 Rectangle r = new Rectangle(0, 0, 600, 400);
170 chart.draw(g2, r);
171 File f = new File("SVGPieChartDemo1.svg");
172 SVGUtils.writeToSVG(f, g2.getSVGElement());
173 }
174 }