001/* ====================
002 * PieChartFXDemo1.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.BasicStroke;
039import java.awt.Color;
040import java.awt.Font;
041import java.awt.GradientPaint;
042import java.awt.Point;
043import java.awt.RadialGradientPaint;
044import java.awt.geom.Point2D;
045import javafx.application.Application;
046import javafx.scene.Scene;
047import javafx.stage.Stage;
048import org.jfree.chart.ChartFactory;
049import org.jfree.chart.JFreeChart;
050import org.jfree.chart.fx.ChartViewer;
051import org.jfree.chart.plot.PiePlot;
052import org.jfree.chart.title.TextTitle;
053import org.jfree.data.general.DefaultPieDataset;
054import org.jfree.data.general.PieDataset;
055import org.jfree.ui.HorizontalAlignment;
056import org.jfree.ui.RectangleEdge;
057
058/**
059 * A pie chart demo in JavaFX.
060 */
061public class PieChartFXDemo1 extends Application {
062
063    /**
064     * Creates a sample dataset.
065     * 
066     * Source: http://www.bbc.co.uk/news/business-15489523
067     *
068     * @return A sample dataset.
069     */
070    private static PieDataset createDataset() {
071        DefaultPieDataset dataset = new DefaultPieDataset();
072        dataset.setValue("Samsung", new Double(27.8));
073        dataset.setValue("Others", new Double(55.3));
074        dataset.setValue("Nokia", new Double(16.8));
075        dataset.setValue("Apple", new Double(17.1));
076        return dataset;
077    }
078    
079    /**
080     * Creates a chart.
081     *
082     * @param dataset  the dataset.
083     *
084     * @return A chart.
085     */
086    private static JFreeChart createChart(PieDataset dataset) {
087
088        JFreeChart chart = ChartFactory.createPieChart(
089            "Smart Phones Manufactured / Q3 2011",  // chart title
090            dataset,            // data
091            false,              // no legend
092            true,               // tooltips
093            false               // no URL generation
094        );
095
096        // set a custom background for the chart
097        chart.setBackgroundPaint(new GradientPaint(new Point(0, 0), 
098                new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));
099
100        // customise the title position and font
101        TextTitle t = chart.getTitle();
102        t.setHorizontalAlignment(HorizontalAlignment.LEFT);
103        t.setPaint(new Color(240, 240, 240));
104        t.setFont(new Font("Arial", Font.BOLD, 26));
105
106        PiePlot plot = (PiePlot) chart.getPlot();
107        plot.setBackgroundPaint(null);
108        plot.setInteriorGap(0.04);
109        plot.setOutlineVisible(false);
110
111        // use gradients and white borders for the section colours
112        plot.setSectionPaint("Others", createGradientPaint(new Color(200, 200, 255), Color.BLUE));
113        plot.setSectionPaint("Samsung", createGradientPaint(new Color(255, 200, 200), Color.RED));
114        plot.setSectionPaint("Apple", createGradientPaint(new Color(200, 255, 200), Color.GREEN));
115        plot.setSectionPaint("Nokia", createGradientPaint(new Color(200, 255, 200), Color.YELLOW));
116        plot.setBaseSectionOutlinePaint(Color.WHITE);
117        plot.setSectionOutlinesVisible(true);
118        plot.setBaseSectionOutlineStroke(new BasicStroke(2.0f));
119
120        // customise the section label appearance
121        plot.setLabelFont(new Font("Courier New", Font.BOLD, 20));
122        plot.setLabelLinkPaint(Color.WHITE);
123        plot.setLabelLinkStroke(new BasicStroke(2.0f));
124        plot.setLabelOutlineStroke(null);
125        plot.setLabelPaint(Color.WHITE);
126        plot.setLabelBackgroundPaint(null);
127        
128        // add a subtitle giving the data source
129        TextTitle source = new TextTitle("Source: http://www.bbc.co.uk/news/business-15489523", 
130                new Font("Courier New", Font.PLAIN, 12));
131        source.setPaint(Color.WHITE);
132        source.setPosition(RectangleEdge.BOTTOM);
133        source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
134        chart.addSubtitle(source);
135        return chart;
136
137    }
138
139    /**
140     * A utility method for creating gradient paints.
141     * 
142     * @param c1  color 1.
143     * @param c2  color 2.
144     * 
145     * @return A radial gradient paint.
146     */
147    private static RadialGradientPaint createGradientPaint(Color c1, Color c2) {
148        Point2D center = new Point2D.Float(0, 0);
149        float radius = 200;
150        float[] dist = {0.0f, 1.0f};
151        return new RadialGradientPaint(center, radius, dist,
152                new Color[] {c1, c2});
153    }
154
155    @Override 
156    public void start(Stage stage) throws Exception {
157        PieDataset dataset = createDataset();
158        JFreeChart chart = createChart(dataset); 
159        ChartViewer viewer = new ChartViewer(chart);  
160        stage.setScene(new Scene(viewer)); 
161        stage.setTitle("JFreeChart: PieChartFXDemo1.java"); 
162        stage.setWidth(700);
163        stage.setHeight(390);
164        stage.show(); 
165    }
166    
167    /**
168     * @param args the command line arguments
169     */
170    public static void main(String[] args) {
171        launch(args);
172    }
173  
174}