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.plot.PlotOrientation; 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.canvas.CanvasGraphics2D; import org.jfree.graphics2d.canvas.CanvasUtils; /** * A demo using {@link CanvasGraphics2D} and JFreeChart to write a bar chart * to an HTML file where a JavaScript function draws the chart on an * HTML5 canvas. */ public class CanvasBarChartDemo1 { /** * 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) { // create the chart... JFreeChart chart = ChartFactory.createLineChart("Statistical Bar Chart Demo 1", // chart title "Type", // domain axis label "Value", // range axis label 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()); CanvasGraphics2D g2 = new CanvasGraphics2D("id"); int width = 600; int height = 400; Rectangle r = new Rectangle(0, 0, width, height); chart.draw(g2, r); File f = new File("CanvasBarChartDemo1.html"); CanvasUtils.writeToHTML(f, "CanvasBarChartDemo1", g2.getCanvasID(), width, height, g2.getScript()); } }