Example usage for org.jfree.chart ChartFactory createPieChart

List of usage examples for org.jfree.chart ChartFactory createPieChart

Introduction

In this page you can find the example usage for org.jfree.chart ChartFactory createPieChart.

Prototype

public static JFreeChart createPieChart(String title, PieDataset dataset, boolean legend, boolean tooltips,
        boolean urls) 

Source Link

Document

Creates a pie chart with default settings.

Usage

From source file:org.jfree.chart.demo.FirstDemo.java

public static void main(String args[]) {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("Category 1", 43.200000000000003D);
    defaultpiedataset.setValue("Category 2", 27.899999999999999D);
    defaultpiedataset.setValue("Category 3", 79.5D);
    org.jfree.chart.JFreeChart jfreechart = ChartFactory.createPieChart("Sample Pie Chart", defaultpiedataset,
            true, true, false);/* w ww  . j a  v a 2  s  .  c om*/
    ChartFrame chartframe = new ChartFrame("First", jfreechart);
    chartframe.pack();
    chartframe.setVisible(true);
}

From source file:urbanchart.java

public static void main(String[] arg) {
    final DefaultPieDataset data = new DefaultPieDataset();
    data.setValue("Housing(35%)($1050)", new Double(35));
    data.setValue("Debt(15%)($450)", new Double(15));
    data.setValue("transportation(15%)($450)", new Double(15));
    data.setValue("Food(15%)($450)", new Double(25));
    data.setValue("Saving(10%)($300)", new Double(10));
    JFreeChart chart = ChartFactory.createPieChart(
            "Average Distribution of expenses on income assuming 3000$/month (Urban areas)", data, true, true,
            false);// w  w  w.  ja  va  2s. co  m
    final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
    final File file1 = new File("E:\\java projects\\hackathon1\\web\\piecharturban.jpg");
    try {
        ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
    } catch (IOException ex) {
        Logger.getLogger(urbanchart.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.jfree.chart.demo.LegendTitleToImageDemo1.java

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("A", 1.0D);
    defaultpiedataset.setValue("B", 2D);
    defaultpiedataset.setValue("C", 3D);
    JFreeChart jfreechart = ChartFactory.createPieChart("Test", defaultpiedataset, true, false, false);
    LegendTitle legendtitle = jfreechart.getLegend();
    legendtitle.setMargin(0.0D, 0.0D, 1.0D, 1.0D);
    BufferedImage bufferedimage = new BufferedImage(1, 1, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    Size2D size2d = legendtitle.arrange(graphics2d);
    graphics2d.dispose();//from  w  ww.j a  va  2 s. c  o  m
    int i = (int) Math.rint(size2d.width);
    int j = (int) Math.rint(size2d.height);
    BufferedImage bufferedimage1 = new BufferedImage(i, j, 2);
    Graphics2D graphics2d1 = bufferedimage1.createGraphics();
    legendtitle.draw(graphics2d1, new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j));
    graphics2d1.dispose();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
            new FileOutputStream(new File("LegendTitleToImageDemo1.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

From source file:ruralchart.java

public static void main(String[] arg) {
    final DefaultPieDataset data = new DefaultPieDataset();
    data.setValue("Housing(35%)($350)", new Double(35));
    data.setValue("Debt(15%)($150)", new Double(15));
    data.setValue("transportation(15%)($150)", new Double(15));
    data.setValue("Food(15%)($250)", new Double(25));
    data.setValue("Saving(10%)($100)", new Double(10));
    JFreeChart chart = ChartFactory.createPieChart(
            "Average Distribution of expenses on income assuming 1000$/month (Rural areas)", data, true, true,
            false);/*from  w  w  w  .j a v  a  2s  .  c  om*/
    final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
    final File file1 = new File("E:\\java projects\\hackathon1\\web\\piechartrural.jpg");
    try {
        ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
    } catch (IOException ex) {
        Logger.getLogger(urbanchart.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.ohalo.cn.awt.JFreeChartTest3.java

public static void main(String[] args) throws Exception {
    JFreeChart chart = ChartFactory.createPieChart("???", getDataset(), true, true,
            false);/*from w  w w  .j  a  v a2  s.  c  om*/
    chart.setTitle(new TextTitle("??", new Font("", Font.BOLD + Font.ITALIC, 20)));

    LegendTitle legend = chart.getLegend(0);// Legend
    legend.setItemFont(new Font("", Font.BOLD, 14));
    PiePlot plot = (PiePlot) chart.getPlot();// Plot
    plot.setLabelFont(new Font("", Font.BOLD, 16));

    OutputStream os = new FileOutputStream("company.jpeg");// ??FileOutputStream?
    ChartUtilities.writeChartAsJPEG(os, chart, 1000, 800);
    // ??applicationchart??JPEG?3?4?

    os.close();// ?
}

From source file:History.PieChart_DB.java

public static void main(String[] args) throws Exception {

    String mobilebrands[] = { "IPhone 5s", "SamSung Grand", "MotoG", "Nokia Lumia" };
    int statOfRepair = 0;
    java.util.Date now = new java.util.Date();
    String adminDate = (now.getYear() + 1900) + "-" + (now.getMonth() + 1) + "-" + now.getDate();
    /* Create MySQL Database Connection */
    Class.forName("com.mysql.jdbc.Driver");
    Connection connect = ConnectDatabase.connectDb("win", "win016");

    Statement statement = connect.createStatement();
    ResultSet resultSet = statement
            .executeQuery("SELECT COUNT(transID) AS statRepair FROM `Transaction` WHERE dateTime LIKE \'"
                    + adminDate + "%\' AND action LIKE 'Repair'");
    DefaultPieDataset dataset = new DefaultPieDataset();

    while (resultSet.next()) {
        dataset.setValue(resultSet.getString("statRepair"),
                Double.parseDouble(resultSet.getString("unit_sale")));
    }/*from   w  w  w. ja  v a 2 s  .co  m*/

    JFreeChart chart = ChartFactory.createPieChart("History", // chart title           
            dataset, // data           
            true, // include legend          
            true, false);

    int width = 560; /* Width of the image */
    int height = 370; /* Height of the image */
    File pieChart = new File("Pie_Chart.jpeg");
    ChartUtilities.saveChartAsJPEG(pieChart, chart, width, height);
}

From source file:org.jfree.chart.demo.LegendTitleToImageDemo2.java

public static void main(String args[]) throws IOException {
    DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    defaultpiedataset.setValue("England", 1.0D);
    defaultpiedataset.setValue("France", 2D);
    defaultpiedataset.setValue("Germany", 3D);
    defaultpiedataset.setValue("Italy", 4D);
    defaultpiedataset.setValue("Scotland", 5D);
    defaultpiedataset.setValue("Belgium", 6D);
    defaultpiedataset.setValue("Poland", 7D);
    defaultpiedataset.setValue("Spain", 8D);
    defaultpiedataset.setValue("Portugal", 9D);
    defaultpiedataset.setValue("Switzerland", 10D);
    defaultpiedataset.setValue("Austria", 11D);
    defaultpiedataset.setValue("Luxembourg", 12D);
    JFreeChart jfreechart = ChartFactory.createPieChart("Test", defaultpiedataset, true, false, false);
    LegendTitle legendtitle = jfreechart.getLegend();
    legendtitle.setMargin(0.0D, 0.0D, 1.0D, 1.0D);
    BufferedImage bufferedimage = new BufferedImage(1, 1, 2);
    Graphics2D graphics2d = bufferedimage.createGraphics();
    Size2D size2d = legendtitle.arrange(graphics2d, new RectangleConstraint(250D, new Range(0.0D, 10000D)));
    graphics2d.dispose();/*from w w  w  .ja  va 2  s .co  m*/
    int i = (int) Math.rint(size2d.width);
    int j = (int) Math.rint(size2d.height);
    BufferedImage bufferedimage1 = new BufferedImage(i, j, 2);
    Graphics2D graphics2d1 = bufferedimage1.createGraphics();
    legendtitle.draw(graphics2d1, new java.awt.geom.Rectangle2D.Double(0.0D, 0.0D, i, j));
    graphics2d1.dispose();
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
            new FileOutputStream(new File("LegendTitleToImageDemo2.png")));
    ChartUtilities.writeBufferedImageAsPNG(bufferedoutputstream, bufferedimage1);
    bufferedoutputstream.close();
}

From source file:org.jfree.chart.demo.First.java

/**
 * The starting point for the demo./*from ww w .  ja  va  2 s .c o  m*/
 *
 * @param args  ignored.
 */
public static void main(final String[] args) {

    // create a dataset...
    final DefaultPieDataset data = new DefaultPieDataset();
    data.setValue("Category 1", 43.2);
    data.setValue("Category 2", 27.9);
    data.setValue("Category 3", 79.5);

    // create a chart...
    final JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart", data, true, // legend?
            true, // tooltips?
            false // URLs?
    );

    // create and display a frame...
    final ChartFrame frame = new ChartFrame("First", chart);
    frame.pack();
    frame.setVisible(true);

}

From source file:org.jfree.chart.demo.ImageMapDemo2.java

/**
 * The starting point for the demo.//from ww w .  j av a  2s . c om
 *
 * @param args  ignored.
 */
public static void main(final String[] args) {

    // create a chart
    final DefaultPieDataset data = new DefaultPieDataset();
    data.setValue("One", new Double(43.2));
    data.setValue("Two", new Double(10.0));
    data.setValue("Three", new Double(27.5));
    data.setValue("Four", new Double(17.5));
    data.setValue("Five", new Double(11.0));
    data.setValue("Six", new Double(19.4));

    JFreeChart chart = null;
    final boolean drilldown = true;

    // create the chart...
    if (drilldown) {
        final PiePlot plot = new PiePlot(data);
        //          plot.setInsets(new Insets(0, 5, 5, 5));
        plot.setToolTipGenerator(new StandardPieItemLabelGenerator());
        plot.setURLGenerator(new StandardPieURLGenerator("pie_chart_detail.jsp"));
        chart = new JFreeChart("Pie Chart Demo 1", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    } else {
        chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart title
                data, // data
                true, // include legend
                true, false);
    }
    chart.setBackgroundPaint(java.awt.Color.white);

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************

    // save it to an image
    try {
        final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        final File file1 = new File("piechart100.png");
        ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);

        // write an HTML page incorporating the image with an image map
        final File file2 = new File("piechart100.html");
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
        final PrintWriter writer = new PrintWriter(out);
        writer.println("<HTML>");
        writer.println("<HEAD><TITLE>JFreeChart Image Map Demo 2</TITLE></HEAD>");
        writer.println("<BODY>");
        //            ChartUtilities.writeImageMap(writer, "chart", info);
        writer.println("<IMG SRC=\"piechart100.png\" "
                + "WIDTH=\"600\" HEIGHT=\"400\" BORDER=\"0\" USEMAP=\"#chart\">");
        writer.println("</BODY>");
        writer.println("</HTML>");
        writer.close();

    } catch (IOException e) {
        System.out.println(e.toString());
    }

}

From source file:DataCharts.PieGraph.java

public static JFreeChart createChart(PieDataset dataset) {

    JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", dataset, true, true, false);

    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
    plot.setNoDataMessage("Data Unavailable");
    plot.setCircular(false);//from w w  w  .j  a v  a2 s  . c om
    plot.setLabelGap(0.01);
    plot.setBackgroundPaint(null);
    chart.setBorderPaint(null);
    chart.setBackgroundPaint(null);
    return chart;

}