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) throws IOException 

Source Link

Document

Saves a chart to the specified file in PNG format.

Usage

From source file:de.suse.swamp.test.TestStatisticsGraph.java

public void testStatistic() {

    XYSeries series = new XYSeries("Running Workflows");
    series.add(1995, 0.5);/*from  w ww  .  j  a  v a  2s .c om*/
    series.add(2000, 3.0);
    series.add(2010, 20.0);
    series.add(2020, 50.0);
    XYDataset dataset = new XYSeriesCollection(series);

    JFreeChart chart = ChartFactory.createTimeSeriesChart("test", "time", "value", dataset, false, false,
            false);

    JFreeChart chart4;
    DefaultPieDataset dataset2 = new DefaultPieDataset();
    // Initialize the dataset
    dataset2.setValue("California", new Double(10.0));
    dataset2.setValue("Arizona", new Double(8.0));
    dataset2.setValue("New Mexico", new Double(8.0));
    dataset2.setValue("Texas", new Double(40.0));
    dataset2.setValue("Louisiana", new Double(8.0));
    dataset2.setValue("Mississippi", new Double(4.0));
    dataset2.setValue("Alabama", new Double(2.0));
    dataset2.setValue("Florida", new Double(20.0));

    chart4 = ChartFactory.createPieChart3D("Driving Time Spent Per State (3D with Transparency)", // The chart title
            dataset2, // The dataset for the chart
            true, // Is a legend required?
            true, // Use tooltips
            false // Configure chart to generate URLs?
    );
    PiePlot3D plot4 = (PiePlot3D) chart4.getPlot();
    plot4.setForegroundAlpha(0.6f);

    try {
        ChartUtilities.saveChartAsPNG(new java.io.File("test.png"), chart, 500, 300);

        ChartUtilities.saveChartAsPNG(new java.io.File("test2.png"), chart4, 500, 300);
    } catch (java.io.IOException exc) {
        System.err.println("Error writing image to file");
    }

}

From source file:test.integ.be.fedict.performance.util.PerformanceResultDialog.java

public PerformanceResultDialog(PerformanceResultsData data) {

    super((Frame) null, "Performance test results");
    setSize(1000, 800);/* ww  w . ja v  a  2s .  co  m*/

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    JMenuItem savePerformanceMenuItem = new JMenuItem("Save Performance");
    fileMenu.add(savePerformanceMenuItem);
    savePerformanceMenuItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle("Save as PNG...");
            int result = fileChooser.showSaveDialog(PerformanceResultDialog.this);
            if (JFileChooser.APPROVE_OPTION == result) {
                File file = fileChooser.getSelectedFile();
                try {
                    ChartUtilities.saveChartAsPNG(file, performanceChart, 1024, 768);
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, "error saving to file: " + e.getMessage());
                }
            }
        }

    });
    JMenuItem saveMemoryMenuItem = new JMenuItem("Save Memory");
    fileMenu.add(saveMemoryMenuItem);
    saveMemoryMenuItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogTitle("Save as PNG...");
            int result = fileChooser.showSaveDialog(PerformanceResultDialog.this);
            if (JFileChooser.APPROVE_OPTION == result) {
                File file = fileChooser.getSelectedFile();
                try {
                    ChartUtilities.saveChartAsPNG(file, memoryChart, 1024, 768);
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null, "error saving to file: " + e.getMessage());
                }
            }
        }

    });

    // memory chart
    memoryChart = getMemoryChart(data.getIntervalSize(), data.getMemory());

    // performance chart
    performanceChart = getPerformanceChart(data.getIntervalSize(), data.getPerformance(),
            data.getExpectedRevokedCount());

    Container container = getContentPane();
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    if (null != performanceChart) {
        splitPane.setTopComponent(new ChartPanel(performanceChart));
    }
    if (null != memoryChart) {
        splitPane.setBottomComponent(new ChartPanel(memoryChart));
    }
    splitPane.setDividerLocation(getHeight() / 2);
    splitPane.setDividerSize(1);
    container.add(splitPane);

    setVisible(true);
}

From source file:net.sf.statcvs.output.xml.chart.AbstractChart.java

public void save(int width, int height, String filename) throws IOException {
    ChartUtilities.saveChartAsPNG(new File(FileUtils.getFilenameWithDirectory(filename)), chart, width, height);
    logger.fine("saved chart '" + title + "' as '" + filename + "'");
}

From source file:playground.artemc.socialCost.MeanTravelTimeWriter.java

public void writeGraphic(final String filename, String[] legModes, double[][] data) {
    try {//from  w  w  w.  j av  a2s  . c o m
        ChartUtilities.saveChartAsPNG(new File(filename), getGraphic(legModes, data), 1024, 768);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:statUtil.TurnMovementPlot.java

public TurnMovementPlot(String title) throws IOException {
    super(title);
    Data data = CSVData.getCSVData(TURN_CSV_LOG);
    JFreeChart chart = ChartFactory.createXYLineChart(title, "X", "Y",
            XYDatasetGenerator.generateXYDataset(data.csvData));
    final XYPlot xyPlot = chart.getXYPlot();
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesStroke(0, new BasicStroke(3f));
    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(0, false);
    renderer.setSeriesLinesVisible(1, false);
    renderer.setSeriesShapesVisible(1, true);
    Shape cross = ShapeUtilities.createDiagonalCross(2f, 0.5f);
    renderer.setSeriesShape(1, cross);/*from ww  w . ja  v a2s.  co m*/
    xyPlot.setRenderer(renderer);
    xyPlot.setQuadrantOrigin(new Point(0, 0));

    int i = 0;
    for (Double[] csvRow : data.csvData) {
        if (i % 20 == 1) {
            final XYTextAnnotation annotation = new XYTextAnnotation(Double.toString(csvRow[3]), csvRow[0],
                    csvRow[1]);
            annotation.setFont(new Font("SansSerif", Font.PLAIN, 10));
            xyPlot.addAnnotation(annotation);
        }
        i++;
    }

    int width = (int) Math.round(data.maxX - data.minX) + 50;
    int height = (int) Math.round(data.maxY - data.minY) + 50;
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(width, height));
    setContentPane(chartPanel);
    File XYChart = new File(FILE_PATH + "\\TurnMovementPlot.png");
    ChartUtilities.saveChartAsPNG(XYChart, chart, width, height);
}

From source file:ws.moor.bt.grapher.ReportMainline.java

public void create(OutputStream stream, File directory) throws IOException {
    buildCharts();//  w ww  .  j  a  v  a  2s . c om
    directory.mkdirs();
    StringBuilder htmlFile = new StringBuilder();
    htmlFile.append("<html>\n");
    htmlFile.append("<h1 style=\"page-break-before: always;\">").append(getName()).append("</h1>\n");
    htmlFile.append("Running Time: ").append(getRunningTime() / 1000 / 60).append("min").append("<br/>\n");
    htmlFile.append("Total Down: ").append(getLastValue("downTotal") / 1024 / 1024).append("MB")
            .append("<br/>\n");
    htmlFile.append("Total Up: ").append(getLastValue("upTotal") / 1024 / 1024).append("MB").append("<br/>\n");
    File rawbytefile = new File(directory, "rawbytes.png");
    ChartUtilities.saveChartAsPNG(rawbytefile, rawBytesChart, ReportMainline.WIDTH, ReportMainline.HEIGHT);
    appendHTMLForGraph(htmlFile, rawbytefile, "Raw Bytes", ReportMainline.WIDTH, ReportMainline.HEIGHT);
    File trackerfile = new File(directory, "tracker.png");
    if (trackerChart != null) {
        ChartUtilities.saveChartAsPNG(trackerfile, trackerChart, ReportMainline.WIDTH, ReportMainline.HEIGHT);
        appendHTMLForGraph(htmlFile, trackerfile, "Tracker", ReportMainline.WIDTH, ReportMainline.HEIGHT);
    }
    File connectionfile = new File(directory, "connections.png");
    ChartUtilities.saveChartAsPNG(connectionfile, connectionChart, ReportMainline.WIDTH, ReportMainline.HEIGHT);
    appendHTMLForGraph(htmlFile, connectionfile, "Connections", ReportMainline.WIDTH, ReportMainline.HEIGHT);

    stream.write(htmlFile.toString().getBytes());
}

From source file:org.neo4j.bench.chart.GenerateOpsPerSecChart.java

private void generateChart() throws Exception {
    DefaultCategoryDataset dataset = generateDataset();
    JFreeChart chart = ChartFactory.createBarChart("Performance chart", "Bench case", "Operations per sec",
            dataset, PlotOrientation.VERTICAL, true, true, false);

    Dimension dimensions = new Dimension(1600, 900);
    File chartFile = new File(outputFilename);
    if (alarm) {/*w  w w  .  ja va  2 s .co m*/
        chart.setBackgroundPaint(Color.RED);
    }
    ChartUtilities.saveChartAsPNG(chartFile, chart, (int) dimensions.getWidth(), (int) dimensions.getHeight());
}

From source file:org.matsim.analysis.LegHistogramChart.java

/**
* Writes a graphic showing the number of departures, arrivals and vehicles
* en route of all legs/trips with the specified transportation mode to the
* specified file./* www .j  a  v a2 s . com*/
*
* @param legHistogram
 * @param filename
* @param legMode
*
*/
public static void writeGraphic(LegHistogram legHistogram, final String filename, final String legMode) {
    try {
        ChartUtilities.saveChartAsPNG(new File(filename),
                getGraphic(legHistogram.getDataForMode(legMode), legMode, legHistogram.getIteration()), 1024,
                768);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:clienteescritorio.Grafica.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    try {/*from  w  w  w  .j ava 2 s.  com*/
        String directorio = System.getProperty("user.dir") + "/temporal/";
        String imagen = "grafica.png";
        String so = System.getProperty("os.name");
        System.err.println("El sso " + so);
        File f = new File(directorio + imagen);
        ChartUtilities.saveChartAsPNG(f, chart, 600, 400);
        Runtime run = Runtime.getRuntime();
        if (so.equals("Linux"))
            run.exec("eog " + directorio + imagen);
        else {
            Desktop dt = Desktop.getDesktop();
            dt.open(f);
        }
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(rootPane, "No se pudo guardar la grfica :(, Intntelo ms tarde!");
        ex.printStackTrace();
    }

}

From source file:cz.muni.fi.nbs.utils.Helpers.java

private static void exportToPNG(Map<String, Collection<Result>> results) {

    for (Entry<String, Collection<Result>> entry : results.entrySet()) {
        double number = 0;
        String key = entry.getKey();
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        Iterator<Result> resultsIterator = results.get(key).iterator();
        while (resultsIterator.hasNext()) {
            Result r = resultsIterator.next();
            dataset.addValue(r.getScore(), r.getLabel(), "");
            number += 0.8;/* w  w  w  .j a v a2  s .co m*/
        }
        double width = number > 1 ? number * 200 : 300;
        String unit = entry.getValue().iterator().next().getScoreUnit();
        String[] splitKey = key.split("_");

        JFreeChart chart = ChartFactory.createBarChart3D(splitKey[0], null, unit, dataset);

        int len = splitKey.length / 2;
        for (int i = 0; i < len; i++) {
            String subtitle = splitKey[i * 2 + 1] + ":" + splitKey[i * 2 + 2];
            TextTitle title = new TextTitle(subtitle);
            Font oldFont = title.getFont();
            int fontSize = (int) Math.round(oldFont.getSize() * 1.2);
            int fontStyle = oldFont.getStyle();
            String fontName = oldFont.getName();

            title.setFont(new Font(fontName, fontStyle, fontSize));
            chart.addSubtitle(title);
        }
        try {
            ChartUtilities.saveChartAsPNG(new File(resultsDir + "/charts/" + key + "Chart.png"), chart,
                    (int) Math.round(width), 800);
        } catch (IOException ex) {
            Logger.getLogger(Helpers.class.getName()).log(Level.SEVERE,
                    "Could not export chart to PNG file for " + key, ex);
        }
    }
}