Debian Codegraph @

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

raw
1 /* =====================
2 * SVGBarChartDemo1.java
3 * =====================
4 *
5 * Copyright (c) 2013-2021, Object Refinery Limited.
6 * All rights reserved.
7 *
8 * http://www.jfree.org/jfreechart/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.Color;
37 import java.awt.GradientPaint;
38 import java.awt.Rectangle;
39 import java.io.File;
40 import java.io.IOException;
41 import org.jfree.chart.ChartFactory;
42 import org.jfree.chart.ChartUtils;
43 import org.jfree.chart.JFreeChart;
44 import org.jfree.chart.axis.NumberAxis;
45 import org.jfree.chart.labels.ItemLabelAnchor;
46 import org.jfree.chart.labels.ItemLabelPosition;
47 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
48 import org.jfree.chart.plot.CategoryPlot;
49 import org.jfree.chart.renderer.category.StatisticalBarRenderer;
50 import org.jfree.chart.ui.TextAnchor;
51 import org.jfree.data.category.CategoryDataset;
52 import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
53 import org.jfree.svg.SVGGraphics2D;
54 import org.jfree.svg.SVGUtils;
55
56 /**
57 * A demo/test for a bar chart.
58 */
59 public class SVGBarChartDemo1 {
60
61 /**
62 * Creates a sample dataset.
63 *
64 * @return The dataset.
65 */
66 private static CategoryDataset createDataset() {
67 DefaultStatisticalCategoryDataset dataset
68 = new DefaultStatisticalCategoryDataset();
69 dataset.add(10.0, 2.4, "Row 1", "Column 1");
70 dataset.add(15.0, 4.4, "Row 1", "Column 2");
71 dataset.add(13.0, 2.1, "Row 1", "Column 3");
72 dataset.add(7.0, 1.3, "Row 1", "Column 4");
73 dataset.add(22.0, 2.4, "Row 2", "Column 1");
74 dataset.add(18.0, 4.4, "Row 2", "Column 2");
75 dataset.add(28.0, 2.1, "Row 2", "Column 3");
76 dataset.add(17.0, 1.3, "Row 2", "Column 4");
77 return dataset;
78 }
79
80 /**
81 * Creates a sample chart.
82 *
83 * @param dataset a dataset.
84 *
85 * @return The chart.
86 */
87 private static JFreeChart createChart(CategoryDataset dataset) {
88
89 JFreeChart chart = ChartFactory.createLineChart(
90 "Statistical Bar Chart Demo 1", "Type", "Value", dataset);
91
92 CategoryPlot plot = (CategoryPlot) chart.getPlot();
93
94 // customise the range axis...
95 NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
96 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
97 rangeAxis.setAutoRangeIncludesZero(false);
98
99 // customise the renderer...
100 StatisticalBarRenderer renderer = new StatisticalBarRenderer();
101 renderer.setDrawBarOutline(false);
102 renderer.setErrorIndicatorPaint(Color.black);
103 renderer.setIncludeBaseInRange(false);
104 plot.setRenderer(renderer);
105
106 // ensure the current theme is applied to the renderer just added
107 ChartUtils.applyCurrentTheme(chart);
108
109 renderer.setDefaultItemLabelGenerator(
110 new StandardCategoryItemLabelGenerator());
111 renderer.setDefaultItemLabelsVisible(true);
112 renderer.setDefaultItemLabelPaint(Color.yellow);
113 renderer.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(
114 ItemLabelAnchor.INSIDE6, TextAnchor.BOTTOM_CENTER));
115
116 // set up gradient paints for series...
117 GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
118 0.0f, 0.0f, new Color(0, 0, 64));
119 GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green,
120 0.0f, 0.0f, new Color(0, 64, 0));
121 renderer.setSeriesPaint(0, gp0);
122 renderer.setSeriesPaint(1, gp1);
123 return chart;
124 }
125
126 /**
127 * Starting point for the demo.
128 *
129 * @param args ignored.
130 *
131 * @throws IOException
132 */
133 public static void main(String[] args) throws IOException {
134 JFreeChart chart = createChart(createDataset());
135 SVGGraphics2D g2 = new SVGGraphics2D(600, 400);
136 Rectangle r = new Rectangle(0, 0, 600, 400);
137 chart.draw(g2, r);
138 File f = new File("SVGBarChartDemo1.svg");
139 SVGUtils.writeToSVG(f, g2.getSVGElement());
140 }
141 }