Java tutorial
/* =================================================== * JFreeSVG : an SVG library for the Java(tm) platform * =================================================== * * (C)opyright 2013, 2014, by Object Refinery Limited. All rights reserved. * * Project Info: http://www.jfree.org/jfreesvg/index.html * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. * Other names may be trademarks of their respective owners.] * * If you do not wish to be bound by the terms of the AGPL, an alternative * commercial license can be purchased. For details, please see visit the * JFreeSVG home page: * * http://www.jfree.org/jfreesvg */ package org.jfree.graphics2d.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.ChartUtilities; 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.graphics2d.svg.SVGGraphics2D; import org.jfree.graphics2d.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 ChartUtilities.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()); } }