Java Code Examples for org.jfree.chart.ChartUtilities#saveChartAsPNG()

The following examples show how to use org.jfree.chart.ChartUtilities#saveChartAsPNG() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Timer.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private void saveChart(String op) {
    XYSeries series = new XYSeries(op);
    int i = 0;
    for (Long duration : stats.get(op)) {
        series.add(++i, duration);
    }
    final XYSeriesCollection data = new XYSeriesCollection(series);
    final JFreeChart chart = ChartFactory.createXYLineChart(
            op,
            "Operations",
            "Duration (ms)",
            data,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
    );
    try {
        ChartUtilities.saveChartAsPNG(
                new File(CHARTS_DIR, op.replace(" ", "_") + ".png"),
                chart, 640, 480);
    } catch (IOException ex) {
        log.warn("Unable to save chart for operation '" + op + "'.");
    }
}
 
Example 2
Source File: ChartComposite.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart
 * in PNG format.
 *
 * @throws IOException if there is an I/O error.
 */
public void doSaveAs() throws IOException {
    FileDialog fileDialog = new FileDialog(this.canvas.getShell(), 
            SWT.SAVE);
    String[] extensions = { "*.png" };
    fileDialog.setFilterExtensions(extensions);
    String filename = fileDialog.open();
    if (filename != null) {
        if (isEnforceFileExtensions()) {
            if (!filename.endsWith(".png")) {
                filename = filename + ".png";
            }
        }
        //TODO replace getSize by getBounds ?
        ChartUtilities.saveChartAsPNG(new File(filename), this.chart, 
                this.canvas.getSize().x, this.canvas.getSize().y);
    }
}
 
Example 3
Source File: ServletUtilities.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Saves the chart as a PNG format file in the temporary directory and
 * populates the {@link ChartRenderingInfo} object which can be used to
 * generate an HTML image map.
 *
 * @param chart  the chart to be saved (<code>null</code> not permitted).
 * @param width  the width of the chart.
 * @param height  the height of the chart.
 * @param info  the ChartRenderingInfo object to be populated
 *              (<code>null</code> permitted).
 * @param session  the HttpSession of the client (if <code>null</code>, the
 *                 temporary file is marked as "one-time" and deleted by
 *                 the {@link DisplayChart} servlet right after it is
 *                 streamed to the client).
 *
 * @return The filename of the chart saved in the temporary directory.
 *
 * @throws IOException if there is a problem saving the file.
 */
public static String saveChartAsPNG(JFreeChart chart, int width, int height,
        ChartRenderingInfo info, HttpSession session) throws IOException {

    if (chart == null) {
        throw new IllegalArgumentException("Null 'chart' argument.");
    }
    ServletUtilities.createTempDir();
    String prefix = ServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = ServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".png",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 4
Source File: ChartComposite.java    From ccu-historian with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart
 * in PNG format.
 *
 * @throws IOException if there is an I/O error.
 */
public void doSaveAs() throws IOException {
    FileDialog fileDialog = new FileDialog(this.canvas.getShell(),
            SWT.SAVE);
    String[] extensions = {"*.png"};
    fileDialog.setFilterExtensions(extensions);
    String filename = fileDialog.open();
    if (filename != null) {
        if (isEnforceFileExtensions()) {
            if (!filename.endsWith(".png")) {
                filename = filename + ".png";
            }
        }
        //TODO replace getSize by getBounds ?
        ChartUtilities.saveChartAsPNG(new File(filename), this.chart,
                this.canvas.getSize().x, this.canvas.getSize().y);
    }
}
 
Example 5
Source File: ChartComposite.java    From SIMVA-SoS with Apache License 2.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart
 * in PNG format.
 *
 * @throws IOException if there is an I/O error.
 */
public void doSaveAs() throws IOException {
    FileDialog fileDialog = new FileDialog(this.canvas.getShell(),
            SWT.SAVE);
    String[] extensions = {"*.png"};
    fileDialog.setFilterExtensions(extensions);
    String filename = fileDialog.open();
    if (filename != null) {
        if (isEnforceFileExtensions()) {
            if (!filename.endsWith(".png")) {
                filename = filename + ".png";
            }
        }
        //TODO replace getSize by getBounds ?
        ChartUtilities.saveChartAsPNG(new File(filename), this.chart,
                this.canvas.getSize().x, this.canvas.getSize().y);
    }
}
 
Example 6
Source File: AbstractChartPanel.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Opens a file chooser and gives the user an opportunity to save the chart in PNG format.
 * 
 * @throws IOException
 *             if there is an I/O error.
 */

@Override
public void doSaveAs() throws IOException {

	JFileChooser fileChooser = new JFileChooser();
	fileChooser.setCurrentDirectory(this.defaultDirectoryForSaveAs);
	ExtensionFileFilter filter = new ExtensionFileFilter(localizationResources.getString("PNG_Image_Files"), ".png");
	fileChooser.addChoosableFileFilter(filter);

	int option = fileChooser.showSaveDialog(this);
	if (option == JFileChooser.APPROVE_OPTION) {
		String filename = fileChooser.getSelectedFile().getPath();
		if (isEnforceFileExtensions()) {
			if (!filename.endsWith(".png")) {
				filename = filename + ".png";
			}
		}
		ChartUtilities.saveChartAsPNG(new File(filename), this.chart, getWidth(), getHeight());
	}

}
 
Example 7
Source File: cfCHART.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private String saveChartToFile(JFreeChart chart, byte _format, int width, int height, ChartRenderingInfo info) throws IOException {
	File tempFile;
	if (_format == FORMAT_JPG) {
		tempFile = File.createTempFile("cfchart", ".jpeg", cfchartDirectory);
		ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
	} else {
		tempFile = File.createTempFile("cfchart", ".png", cfchartDirectory);
		ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
	}
	String filename = tempFile.getName();

	// Check if charts are being cached
	if (storageCacheSize > 0) {
		synchronized (storageCache) {
			// If we've reached the cache limit then delete the oldest cached chart.
			if (storageCache.size() == storageCacheSize) {
				String oldestChart = storageCache.remove(0);
				File fileToDelete = new File(cfchartDirectory, oldestChart);
				fileToDelete.delete();
			}

			// Add the new chart to the end of the list of cached charts
			storageCache.add(filename);
		}
	}

	return filename;
}
 
Example 8
Source File: BaseChartPanel.java    From nmonvisualizer with Apache License 2.0 5 votes vote down vote up
public final void saveChart(String directory, String filename) {
    filename = validateSaveFileName(filename);

    File chartFile = new File(directory, filename);

    try {
        ChartUtilities.saveChartAsPNG(chartFile, getChart(), saveWidth, saveHeight);
    }
    catch (IOException ioe) {
        logger.error("could not save chart '" + filename + "' to directory '" + directory + "'", ioe);
    }
}
 
Example 9
Source File: ImagePanel.java    From moa with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Method for save the images.
 *
 * @throws IOException
 */
@Override
public void doSaveAs() throws IOException {

    JFileChooser fileChooser = new JFileChooser();
    ExtensionFileFilter filterPNG = new ExtensionFileFilter("PNG Image Files", ".png");
    fileChooser.addChoosableFileFilter(filterPNG);

    ExtensionFileFilter filterJPG = new ExtensionFileFilter("JPG Image Files", ".jpg");
    fileChooser.addChoosableFileFilter(filterJPG);

    ExtensionFileFilter filterEPS = new ExtensionFileFilter("EPS Image Files", ".eps");
    fileChooser.addChoosableFileFilter(filterEPS);

    ExtensionFileFilter filterSVG = new ExtensionFileFilter("SVG Image Files", ".svg");
    fileChooser.addChoosableFileFilter(filterSVG);
    fileChooser.setCurrentDirectory(null);
    int option = fileChooser.showSaveDialog(this);
    if (option == JFileChooser.APPROVE_OPTION) {
        String fileDesc = fileChooser.getFileFilter().getDescription();
        if (fileDesc.startsWith("PNG")) {
            if (!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("PNG")) {
                ChartUtilities.saveChartAsPNG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".png"), this.chart, this.getWidth(), this.getHeight());
            } else {
                ChartUtilities.saveChartAsPNG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
            }
        } else if (fileDesc.startsWith("JPG")) {
            if (!fileChooser.getSelectedFile().getName().toUpperCase().endsWith("JPG")) {
                ChartUtilities.saveChartAsJPEG(new File(fileChooser.getSelectedFile().getAbsolutePath() + ".jpg"), this.chart, this.getWidth(), this.getHeight());
            } else {
                ChartUtilities.saveChartAsJPEG(fileChooser.getSelectedFile(), this.chart, this.getWidth(), this.getHeight());
            }
        }

    }//else
}
 
Example 10
Source File: APAvsDistance.java    From Juicebox with MIT License 5 votes vote down vote up
private static void plotChart(String SaveFolderPath, XYSeries results) {
    File file = new File(SaveFolderPath + "/results.png");

    final XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(results);

    JFreeChart Chart = ChartFactory.createXYLineChart(
            "APA vs Distance",
            "Distance Bucket",
            "APA Score",
            dataset,
            PlotOrientation.VERTICAL,
            true, true, false);

   /*
   LogarithmicAxis logAxis= new LogarithmicAxis("Distance (log)");
   XYPlot plot= Chart.getXYPlot();
   plot.setDomainAxis(logAxis);
   XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)plot.getRenderer();
   renderer.setSeriesShapesVisible(0, true);
   ChartFrame frame = new ChartFrame("My Chart", Chart);
   frame.pack();
   frame.setVisible(true);
   */

    int width = 640;   /* Width of the image */
    int height = 480;  /* Height of the image */

    try {
        ChartUtilities.saveChartAsPNG(file, Chart, width, height);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
 
Example 11
Source File: ReportGenerator.java    From nmonvisualizer with Apache License 2.0 5 votes vote down vote up
private boolean saveChart(BaseChartDefinition definition, Iterable<? extends DataSet> dataSets,
        File saveDirectory) {
    JFreeChart chart = factory.createChart(definition, dataSets);

    if (chartHasData(chart)) {
        File chartFile = new File(saveDirectory, definition.getShortName().replace(" ", "_") + ".png");

        try {
            ChartUtilities.saveChartAsPNG(chartFile, chart, definition.getWidth(), definition.getHeight());
        }
        catch (IOException ioe) {
            System.err.println("cannot create chart " + chartFile.getName());
        }

        if (writeChartData) {
            writeChartData(chart, definition, saveDirectory);
        }

        System.out.print('.');
        System.out.flush();

        return true;
    }
    else {
        return false;
    }
}
 
Example 12
Source File: VisualizePanelCharts2D.java    From KEEL with GNU General Public License v3.0 5 votes vote down vote up
private void topngjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topngjButtonActionPerformed
    // Save chart as a PNG image
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Save chart");
    KeelFileFilter fileFilter = new KeelFileFilter();
    fileFilter.addExtension("png");
    fileFilter.setFilterName("PNG images (.png)");
    chooser.setFileFilter(fileFilter);
    chooser.setCurrentDirectory(Path.getFilePath());
    int opcion = chooser.showSaveDialog(this);
    Path.setFilePath(chooser.getCurrentDirectory());

    if (opcion == JFileChooser.APPROVE_OPTION) {
        String nombre = chooser.getSelectedFile().getAbsolutePath();
        if (!nombre.toLowerCase().endsWith(".png")) {
            // Add correct extension
            nombre += ".png";
        }
        File tmp = new File(nombre);
        if (!tmp.exists() || JOptionPane.showConfirmDialog(this, "File " + nombre + " already exists. Do you want to replace it?",
                "Confirm", JOptionPane.YES_NO_OPTION, 3) == JOptionPane.YES_OPTION) {
            try {
                chart2.setBackgroundPaint(Color.white);
                ChartUtilities.saveChartAsPNG(new File(nombre), chart2,
                        1024, 768);
            } catch (Exception exc) {
            }
        }
    }
}
 
Example 13
Source File: DerivedCounter.java    From mts with GNU General Public License v3.0 5 votes vote down vote up
protected void writeChartToFile(String path, JFreeChart chart)
{
    try
    {
        ChartUtilities.saveChartAsPNG(new File(path), chart, 800, 400);
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
 
Example 14
Source File: ChartComponent.java    From software-demo with MIT License 5 votes vote down vote up
/**
 * 输出成图片
 */
public static boolean saveChartPNG(File target, String materialsId, String year, int month1, int month2) { 
	try {
		FileOutputStream fos = new FileOutputStream(target);
		JFreeChart chart = createChart(materialsId, year, month1, month2);
		//保存为图片 大小 1100 * 800
		ChartUtilities.saveChartAsPNG(target, chart, 1100, 800);
		fos.flush();
		fos.close();
		return true;
	} catch (Exception e) {
		return false;
	}
}
 
Example 15
Source File: ServletUtilities.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * Saves the chart as a PNG format file in the temporary directory and
 * populates the {@link ChartRenderingInfo} object which can be used to
 * generate an HTML image map.
 *
 * @param chart  the chart to be saved (<code>null</code> not permitted).
 * @param width  the width of the chart.
 * @param height  the height of the chart.
 * @param info  the ChartRenderingInfo object to be populated
 *              (<code>null</code> permitted).
 * @param session  the HttpSession of the client (if <code>null</code>, the
 *                 temporary file is marked as "one-time" and deleted by
 *                 the {@link DisplayChart} servlet right after it is
 *                 streamed to the client).
 *
 * @return The filename of the chart saved in the temporary directory.
 *
 * @throws IOException if there is a problem saving the file.
 */
public static String saveChartAsPNG(JFreeChart chart, int width, int height,
        ChartRenderingInfo info, HttpSession session) throws IOException {

    ParamChecks.nullNotPermitted(chart, "chart");
    ServletUtilities.createTempDir();
    String prefix = ServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = ServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".png",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 16
Source File: ServletUtilities.java    From ccu-historian with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Saves the chart as a PNG format file in the temporary directory and
 * populates the {@link ChartRenderingInfo} object which can be used to
 * generate an HTML image map.
 *
 * @param chart  the chart to be saved (<code>null</code> not permitted).
 * @param width  the width of the chart.
 * @param height  the height of the chart.
 * @param info  the ChartRenderingInfo object to be populated
 *              (<code>null</code> permitted).
 * @param session  the HttpSession of the client (if <code>null</code>, the
 *                 temporary file is marked as "one-time" and deleted by
 *                 the {@link DisplayChart} servlet right after it is
 *                 streamed to the client).
 *
 * @return The filename of the chart saved in the temporary directory.
 *
 * @throws IOException if there is a problem saving the file.
 */
public static String saveChartAsPNG(JFreeChart chart, int width, int height,
        ChartRenderingInfo info, HttpSession session) throws IOException {

    ParamChecks.nullNotPermitted(chart, "chart");
    ServletUtilities.createTempDir();
    String prefix = ServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = ServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".png",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 17
Source File: ServletUtilities.java    From ECG-Viewer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Saves the chart as a PNG format file in the temporary directory and
 * populates the {@link ChartRenderingInfo} object which can be used to
 * generate an HTML image map.
 *
 * @param chart  the chart to be saved (<code>null</code> not permitted).
 * @param width  the width of the chart.
 * @param height  the height of the chart.
 * @param info  the ChartRenderingInfo object to be populated
 *              (<code>null</code> permitted).
 * @param session  the HttpSession of the client (if <code>null</code>, the
 *                 temporary file is marked as "one-time" and deleted by
 *                 the {@link DisplayChart} servlet right after it is
 *                 streamed to the client).
 *
 * @return The filename of the chart saved in the temporary directory.
 *
 * @throws IOException if there is a problem saving the file.
 */
public static String saveChartAsPNG(JFreeChart chart, int width, int height,
        ChartRenderingInfo info, HttpSession session) throws IOException {

    ParamChecks.nullNotPermitted(chart, "chart");
    ServletUtilities.createTempDir();
    String prefix = ServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = ServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".png",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 18
Source File: AnomalyGraphGenerator.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
public void writeChartToFile(JFreeChart chart, String filepath) throws IOException {
  File file = new File(filepath);
  LOG.info("Writing to file: {}", file.getAbsolutePath());
  ChartUtilities.saveChartAsPNG(file, chart, DEFAULT_CHART_WIDTH, DEFAULT_CHART_HEIGHT);
}
 
Example 19
Source File: ServletUtilities.java    From buffer_bci with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Saves the chart as a PNG format file in the temporary directory and
 * populates the {@link ChartRenderingInfo} object which can be used to
 * generate an HTML image map.
 *
 * @param chart  the chart to be saved (<code>null</code> not permitted).
 * @param width  the width of the chart.
 * @param height  the height of the chart.
 * @param info  the ChartRenderingInfo object to be populated
 *              (<code>null</code> permitted).
 * @param session  the HttpSession of the client (if <code>null</code>, the
 *                 temporary file is marked as "one-time" and deleted by
 *                 the {@link DisplayChart} servlet right after it is
 *                 streamed to the client).
 *
 * @return The filename of the chart saved in the temporary directory.
 *
 * @throws IOException if there is a problem saving the file.
 */
public static String saveChartAsPNG(JFreeChart chart, int width, int height,
        ChartRenderingInfo info, HttpSession session) throws IOException {

    ParamChecks.nullNotPermitted(chart, "chart");
    ServletUtilities.createTempDir();
    String prefix = ServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = ServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".png",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 20
Source File: ServletUtilities.java    From buffer_bci with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Saves the chart as a PNG format file in the temporary directory and
 * populates the {@link ChartRenderingInfo} object which can be used to
 * generate an HTML image map.
 *
 * @param chart  the chart to be saved (<code>null</code> not permitted).
 * @param width  the width of the chart.
 * @param height  the height of the chart.
 * @param info  the ChartRenderingInfo object to be populated
 *              (<code>null</code> permitted).
 * @param session  the HttpSession of the client (if <code>null</code>, the
 *                 temporary file is marked as "one-time" and deleted by
 *                 the {@link DisplayChart} servlet right after it is
 *                 streamed to the client).
 *
 * @return The filename of the chart saved in the temporary directory.
 *
 * @throws IOException if there is a problem saving the file.
 */
public static String saveChartAsPNG(JFreeChart chart, int width, int height,
        ChartRenderingInfo info, HttpSession session) throws IOException {

    ParamChecks.nullNotPermitted(chart, "chart");
    ServletUtilities.createTempDir();
    String prefix = ServletUtilities.tempFilePrefix;
    if (session == null) {
        prefix = ServletUtilities.tempOneTimeFilePrefix;
    }
    File tempFile = File.createTempFile(prefix, ".png",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}