001/* ====================
002 * BarChartFXDemo1.java
003 * ====================
004 * 
005 * Copyright (c) 2014, Object Refinery Limited.
006 * All rights reserved.
007 *
008 * http://www.jfree.org/jfreechart/index.html
009 *
010 * Redistribution and use in source and binary forms, with or without
011 * modification, are permitted provided that the following conditions are met:
012 *   - Redistributions of source code must retain the above copyright
013 *     notice, this list of conditions and the following disclaimer.
014 *   - Redistributions in binary form must reproduce the above copyright
015 *     notice, this list of conditions and the following disclaimer in the
016 *     documentation and/or other materials provided with the distribution.
017 *   - Neither the name of the Object Refinery Limited nor the
018 *     names of its contributors may be used to endorse or promote products
019 *     derived from this software without specific prior written permission.
020 *
021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
024 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
025 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
026 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
027 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
028 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
029 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031 * 
032 */
033
034package org.jfree.chart.fx.demo;
035
036import static javafx.application.Application.launch;
037
038import java.awt.Color;
039import javafx.application.Application;
040import javafx.scene.Scene;
041import javafx.stage.Stage;
042import org.jfree.chart.ChartFactory;
043import org.jfree.chart.JFreeChart;
044import org.jfree.chart.axis.NumberAxis;
045import org.jfree.chart.block.BlockBorder;
046import org.jfree.chart.fx.ChartViewer;
047import org.jfree.chart.fx.interaction.ChartMouseEventFX;
048import org.jfree.chart.fx.interaction.ChartMouseListenerFX;
049import org.jfree.chart.plot.CategoryPlot;
050import org.jfree.chart.renderer.category.BarRenderer;
051import org.jfree.chart.title.TextTitle;
052import org.jfree.data.category.CategoryDataset;
053import org.jfree.data.category.DefaultCategoryDataset;
054
055/**
056 * A demo showing the display of JFreeChart within a JavaFX application.
057 * For more information about the JFreeSVG vs Batik performance test, see
058 * this link: http://www.object-refinery.com/blog/blog-20140423.html
059 */
060public class BarChartFXDemo1 extends Application implements ChartMouseListenerFX {
061
062    /**
063     * Returns a sample dataset.
064     *
065     * @return The dataset.
066     */
067    private static CategoryDataset createDataset() {
068        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
069        dataset.addValue(7445, "JFreeSVG", "Warm-up");
070        dataset.addValue(24448, "Batik", "Warm-up");
071        dataset.addValue(4297, "JFreeSVG", "Test");
072        dataset.addValue(21022, "Batik", "Test");
073        return dataset;
074    }
075    
076    /**
077     * Creates a sample chart.
078     *
079     * @param dataset  the dataset.
080     *
081     * @return The chart.
082     */
083    private static JFreeChart createChart(CategoryDataset dataset) {
084        JFreeChart chart = ChartFactory.createBarChart(
085            "Performance: JFreeSVG vs Batik", null /* x-axis label*/, 
086                "Milliseconds" /* y-axis label */, dataset);
087        chart.addSubtitle(new TextTitle("Time to generate 1000 charts in SVG " 
088                + "format (lower bars = better performance)"));
089        chart.setBackgroundPaint(Color.white);
090        CategoryPlot plot = (CategoryPlot) chart.getPlot();
091
092        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
093        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
094        BarRenderer renderer = (BarRenderer) plot.getRenderer();
095        renderer.setDrawBarOutline(false);
096        chart.getLegend().setFrame(BlockBorder.NONE);
097        return chart;
098    }
099
100    /**
101     * Adds a chart viewer to the stage and displays it.
102     * 
103     * @param stage  the stage.
104     * @throws Exception if something goes wrong.
105     */
106    @Override 
107    public void start(Stage stage) throws Exception {
108        CategoryDataset dataset = createDataset();
109        JFreeChart chart = createChart(dataset); 
110        ChartViewer viewer = new ChartViewer(chart);
111        viewer.addChartMouseListener(this);
112        stage.setScene(new Scene(viewer)); 
113        stage.setTitle("JFreeChart: BarChartFXDemo1.java"); 
114        stage.setWidth(700);
115        stage.setHeight(390);
116        stage.show(); 
117    }
118    
119    /**
120     * Entry point.
121     * 
122     * @param args the command line arguments
123     */
124    public static void main(String[] args) {
125        launch(args);
126    }
127
128    /**
129     * Write the event to the console, just to illustrate.
130     * 
131     * @param event  event info. 
132     */
133    @Override
134    public void chartMouseClicked(ChartMouseEventFX event) {
135        System.out.println(event);
136    }
137
138    /**
139     * Write the event to the console, just to illustrate.
140     * 
141     * @param event  event info. 
142     */
143    @Override
144    public void chartMouseMoved(ChartMouseEventFX event) {
145        System.out.println(event);
146    }
147  
148}