Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Diagramas; /** * * @author Alejandro */ import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; import org.jfree.ui.ApplicationFrame; import org.jfree.ui.RefineryUtilities; public class PieChart_AWT extends ApplicationFrame { public PieChart_AWT(String title) throws IOException { super(title); setContentPane(createDemoPanel()); } private static PieDataset createDataset() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("IPhone 5s", new Double(20)); dataset.setValue("SamSung Grand", new Double(20)); dataset.setValue("MotoG", new Double(40)); dataset.setValue("Nokia Lumia", new Double(10)); return dataset; } private static JFreeChart createChart(PieDataset dataset) { JFreeChart chart = ChartFactory.createPieChart("Mobile Sales", // chart title dataset, // data true, // include legend true, false); return chart; } public static JPanel createDemoPanel() throws FileNotFoundException, IOException { JFreeChart chart = createChart(createDataset()); return new ChartPanel(chart); } public static void main(String[] args) throws IOException { PieChart_AWT demo = new PieChart_AWT("Mobile Sales"); demo.setSize(560, 367); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); JFreeChart chart = createChart(createDataset()); OutputStream output = new FileOutputStream("img.png"); ChartUtilities.writeChartAsPNG(output, chart, 500, 600); output.close(); } }