/* ===================== * SVGBarChartDemo1.java * ===================== * * Copyright (c) 2013-2021, Object Refinery Limited. * All rights reserved. * * http://www.jfree.org/jfreechart/index.html * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - Neither the name of the Object Refinery Limited nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ package org.jfree.svg.demo; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Rectangle; import java.io.File; import java.io.IOException; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtils; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.StatisticalBarRenderer; import org.jfree.chart.ui.TextAnchor; import org.jfree.data.category.CategoryDataset; import org.jfree.data.statistics.DefaultStatisticalCategoryDataset; import org.jfree.svg.SVGGraphics2D; import org.jfree.svg.SVGUtils; /** * A demo/test for a bar chart. */ public class SVGBarChartDemo1 { /** * Creates a sample dataset. * * @return The dataset. */ private static CategoryDataset createDataset() { DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset(); dataset.add(10.0, 2.4, "Row 1", "Column 1"); dataset.add(15.0, 4.4, "Row 1", "Column 2"); dataset.add(13.0, 2.1, "Row 1", "Column 3"); dataset.add(7.0, 1.3, "Row 1", "Column 4"); dataset.add(22.0, 2.4, "Row 2", "Column 1"); dataset.add(18.0, 4.4, "Row 2", "Column 2"); dataset.add(28.0, 2.1, "Row 2", "Column 3"); dataset.add(17.0, 1.3, "Row 2", "Column 4"); return dataset; } /** * Creates a sample chart. * * @param dataset a dataset. * * @return The chart. */ private static JFreeChart createChart(CategoryDataset dataset) { JFreeChart chart = ChartFactory.createLineChart( "Statistical Bar Chart Demo 1", "Type", "Value", dataset); CategoryPlot plot = (CategoryPlot) chart.getPlot(); // customise the range axis... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setAutoRangeIncludesZero(false); // customise the renderer... StatisticalBarRenderer renderer = new StatisticalBarRenderer(); renderer.setDrawBarOutline(false); renderer.setErrorIndicatorPaint(Color.black); renderer.setIncludeBaseInRange(false); plot.setRenderer(renderer); // ensure the current theme is applied to the renderer just added ChartUtils.applyCurrentTheme(chart); renderer.setDefaultItemLabelGenerator( new StandardCategoryItemLabelGenerator()); renderer.setDefaultItemLabelsVisible(true); renderer.setDefaultItemLabelPaint(Color.yellow); renderer.setDefaultPositiveItemLabelPosition(new ItemLabelPosition( ItemLabelAnchor.INSIDE6, TextAnchor.BOTTOM_CENTER)); // set up gradient paints for series... GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 0, 64)); GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(0, 64, 0)); renderer.setSeriesPaint(0, gp0); renderer.setSeriesPaint(1, gp1); return chart; } /** * Starting point for the demo. * * @param args ignored. * * @throws IOException */ public static void main(String[] args) throws IOException { JFreeChart chart = createChart(createDataset()); SVGGraphics2D g2 = new SVGGraphics2D(600, 400); Rectangle r = new Rectangle(0, 0, 600, 400); chart.draw(g2, r); File f = new File("SVGBarChartDemo1.svg"); SVGUtils.writeToSVG(f, g2.getSVGElement()); } }