Example usage for org.jfree.chart ChartUtilities saveChartAsPNG

List of usage examples for org.jfree.chart ChartUtilities saveChartAsPNG

Introduction

In this page you can find the example usage for org.jfree.chart ChartUtilities saveChartAsPNG.

Prototype

public static void saveChartAsPNG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info,
        boolean encodeAlpha, int compression) throws IOException 

Source Link

Document

Saves a chart to a file in PNG format.

Usage

From source file:CalcoloRitardiLotti.java

public static void main(String[] args) {
    String id_ref = "cbededce-269f-48d2-8c25-2359bf246f42";
    String requestString = "http://dati.openexpo2015.it/catalog/api/action/datastore_search?resource_id="
            + id_ref;/*from w w  w  .  ja  v  a2  s . co m*/
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(requestString);
    try {

        HttpResponse response = client.execute(request);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String result = "";
        String resline = "";
        Calendar c = Calendar.getInstance();
        Date current = Date.valueOf(
                c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH));
        while ((resline = rd.readLine()) != null)
            result += resline;

        //System.out.println(jsonObject.toString());
        if (result != null) {
            JSONObject jsonObject = new JSONObject(result);
            JSONObject resultJson = (JSONObject) jsonObject.get("result");
            JSONArray records = (JSONArray) resultJson.get("records");
            Date temp1, temp2;
            //System.out.printf(records.toString());
            long diffInizioFineLavori;
            long ritardo;
            long den = (24 * 60 * 60 * 1000);
            JSONObject temp;

            DefaultCategoryDataset cdata = new DefaultCategoryDataset();
            String partialQuery;
            DefaultPieDataset data = new DefaultPieDataset();

            String totalQuery = "";
            int countSospesi = 0;
            int countConclusi = 0;
            int countVerifica = 0;
            int countInCorso = 0;
            int countCollaudo = 0;
            String stato;
            for (int i = 0; i < records.length(); i++) {
                temp = (JSONObject) records.get(i);
                temp1 = Date.valueOf((temp.getString("Data Consegna Lavori")).substring(0, 10));
                temp2 = Date.valueOf((temp.getString("Data Fine lavori")).substring(0, 10));
                diffInizioFineLavori = (long) (temp2.getTime() - temp1.getTime()) / den;
                stato = temp.getString("STATO");
                if (stato.equals("Concluso"))
                    countConclusi++;
                else if (stato.equals("In corso"))
                    countInCorso++;
                else if (stato.contains("Verifiche"))
                    countVerifica++;
                else if (stato.contains("Collaudo sospeso") || stato.contains("sospeso"))
                    countSospesi++;
                else
                    countCollaudo++;

                if (!temp.getString("STATO").equals("Concluso") && temp2.getTime() < current.getTime())
                    ritardo = (long) (current.getTime() - temp2.getTime()) / den;
                else
                    ritardo = 0;

                cdata.setValue(ritardo, String.valueOf(i + 1), String.valueOf(i + 1));
                System.out.println(
                        "Opera: " + temp.getString("Oggetto del lotto") + " | id: " + temp.getInt("_id"));
                System.out.println("Data consegna lavoro: " + temp.getString("Data Consegna Lavori")
                        + " | Data fine lavoro: " + temp.getString("Data Fine lavori"));
                System.out.println("STATO: " + temp.getString("STATO"));
                System.out.println("Differenza in giorni: " + diffInizioFineLavori
                        + " | Numero giorni contrattuali: " + temp.getString("numero di giorni contrattuali"));
                System.out.println("Ritardo accumulato: " + ritardo);

                System.out.println("----------------------------------");

                partialQuery = "\nid: " + temp.getInt("_id") + "\nOpera:" + temp.getString("Oggetto del lotto")
                        + "\n" + "Data consegna lavoro: " + temp.getString("Data Consegna Lavori")
                        + "Data fine lavoro: " + temp.getString("Data Fine lavori") + "\n" + "STATO: "
                        + temp.getString("STATO") + "\n" + "Differenza in giorni: " + diffInizioFineLavori
                        + " - Numero giorni contrattuali: " + temp.getString("numero di giorni contrattuali")
                        + "\n" + "Ritardo accumulato: " + ritardo + "\n"
                        + "----------------------------------\n";
                totalQuery = totalQuery + partialQuery;

            }

            JFreeChart chart1 = ChartFactory.createBarChart3D("RITARDI AL " + current, "Id lotto",
                    "ritardo(in giorni)", cdata);
            ChartRenderingInfo info = null;
            ChartUtilities.saveChartAsPNG(
                    new File(System.getProperty("user.dir") + "/istogramma" + current + ".png"), chart1, 1500,
                    1500, info, true, 10);
            FileUtils.writeStringToFile(new File(current + "_1.txt"), totalQuery);

            data.setValue("Conclusi: " + countConclusi, countConclusi);
            data.setValue("Sospeso: " + countSospesi, countSospesi);
            data.setValue("In Corso: " + countInCorso, countInCorso);
            data.setValue("Verifica: " + countVerifica, countVerifica);
            data.setValue("Collaudo: " + countCollaudo, countCollaudo);
            JFreeChart chart2 = ChartFactory.createPieChart3D("Statistiche del " + current, data, true, true,
                    true);
            ChartUtilities.saveChartAsPNG(new File(System.getProperty("user.dir") + "/pie" + current + ".png"),
                    chart2, 800, 450);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:playground.dgrether.analysis.charts.utils.DgChartWriter.java

public static void writeToPng(String filename, JFreeChart jchart) {
    filename += ".png";
    try {/*from  w w  w. j  av  a  2 s  .com*/
        ChartUtilities.saveChartAsPNG(new File(filename), jchart, 1200, 800, null, true, 9);
        log.info("Chart written to : " + filename);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:playground.benjamin.scenarios.zurich.analysis.charts.BkChartWriter.java

public static void writeToPng(String filename, JFreeChart jchart) {
    filename += ".png";
    try {//from w  w  w . ja v a  2 s  .co m
        ChartUtilities.saveChartAsPNG(new File(filename), jchart, 1200, 800, null, true, 9);
        log.info("\n" + "==================================================" + "\n" + "Chart written to : "
                + filename);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:no.pritest.reporter.APFDGraph.java

public void saveAsPNG() throws IOException {
    ChartUtilities.saveChartAsPNG(output, chart, 640, 480, null, true, 0);

}

From source file:org.pathvisio.cytoscape.superpathways.PieGenerator.java

public void generatePie(int number) {
    // Defining the dataset
    DefaultPieDataset dataset = new DefaultPieDataset();
    for (int i = 0; i < number; i++) {
        String temp = String.valueOf(i);
        dataset.setValue(temp, 10);/*ww  w  .ja va  2s . co m*/
    }

    // Defining the chart
    JFreeChart chart = ChartFactory.createPieChart("", dataset, false, false, false);

    // Defining the chartPanel
    //final ChartPanel chartPanel = new ChartPanel(chart);
    //chartPanel.setPreferredSize(new java.awt.Dimension(350, 350));
    //setContentPane(chartPanel);

    // Defining the plot
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setLabelGenerator(null);
    plot.setInteriorGap(0.0);

    //add the following two lines to make the background transparent 
    chart.setBackgroundPaint(new Color(255, 255, 255, 0));
    plot.setBackgroundPaint(new Color(255, 255, 255, 0));
    //plot.setBackgroundAlpha(0.0f);

    // Specify the colors here

    PieRenderer renderer = new PieRenderer(colors);
    renderer.setColor(plot, dataset);

    try {
        // This will create a PNG image
        ChartUtilities.saveChartAsPNG(new File(imageLocation + "chart.png"), chart, 280, 280, null, true, // encodeAlpha
                0);
    } catch (Exception e) {
        System.out.println("Exception while creating the chart");
    }
}

From source file:org.matsim.core.utils.charts.ChartUtil.java

/**
 * Stores the chart as PNG image file.//from  w ww.j  a v a 2 s .co  m
 *
 * @param filename The filename of the resulting PNG file.
 * @param width The width of the chart.
 * @param height The height of the chart.
 */
public void saveAsPng(final String filename, final int width, final int height) {
    try {
        ChartUtilities.saveChartAsPNG(new File(filename), getChart(), width, height, null, true, 9);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        // I just had an out of bounds error inside the method; don't know what that means but does not feel like a reason 
        // to not continue.  kai, apr'30
    }
}

From source file:playground.yu.utils.charts.DoubleBarChart.java

public void saveAsPng(final String filename, final String title, final int width, final int height) {
    for (int i = 0; i < subCharts.length; i++)
        plot.add((CategoryPlot) subCharts[i].getChart().getPlot(), 1);

    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    // add MATSim logo
    Image image = MatsimResource.getAsImage("matsim_logo_transparent_small.png");
    Title subtitle = new ImageTitle(image, RectangleEdge.BOTTOM, HorizontalAlignment.RIGHT,
            VerticalAlignment.BOTTOM);//from   w w w . j a v a 2s. com
    chart.addSubtitle(subtitle);
    // add default Formatting
    chart.setBackgroundPaint(new Color(1.0f, 1.0f, 1.0f, 1.0f));
    chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);

    try {
        ChartUtilities.saveChartAsPNG(new File(filename), chart, width, height, null, true, 9);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:RDGraphGenerator.java

/**
 * Creates a new demo./*from www . j  av a2  s.  co m*/
 *
 * @param title  the frame title.
 */
public RDGraphGenerator() throws Exception {
    super();
    createDataset();
    Iterator riderKeys = riders.keySet().iterator();
    while (riderKeys.hasNext()) {
        String riderID = (String) riderKeys.next();
        System.out.println("Creating Dist Chart for " + riderID);
        JFreeChart chart = createDistChart(riderID);
        System.out.println("Saving Dist Chart for " + riderID);
        ChartUtilities.saveChartAsPNG(new File(riderID + "dist.png"), chart, 400, 300, null, true, 6);
        System.out.println("Creating Time Chart for " + riderID);
        chart = createTimeChart(riderID);
        System.out.println("Saving Time Chart for " + riderID);
        ChartUtilities.saveChartAsPNG(new File(riderID + "time.png"), chart, 400, 300, null, true, 6);
    }
}

From source file:playground.anhorni.counts.StdDevBoxPlot.java

public void saveAsPng(final String filename, final int width, final int height) {
    try {//from w  w  w .  j  ava 2 s.  c o  m
        ChartUtilities.saveChartAsPNG(new File(filename), createChart(), width, height, null, true, 9);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:playground.dgrether.signalsystems.cottbus.scripts.DgCottbusSignalPlanChartGenerator.java

public static void writeToPng(String filename, JFreeChart jchart, int width, int ySize) {
    filename += ".png";
    try {/* w ww  .  j ava  2 s . c o m*/
        ChartUtilities.saveChartAsPNG(new File(filename), jchart, width, ySize, null, true, 9);
        log.info("Chart written to : " + filename);
    } catch (IOException e) {
        e.printStackTrace();
    }
}