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

The following examples show how to use org.jfree.chart.ChartUtilities#saveChartAsJPEG() . 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: 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 2
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 3
Source File: ServletUtilities.java    From openstock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Saves the chart as a JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 4
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 JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 5
Source File: ServletUtilities.java    From SIMVA-SoS with Apache License 2.0 4 votes vote down vote up
/**
 * Saves the chart as a JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 6
Source File: ResultsProccessor.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
public void writeToFile(String outName) throws FileNotFoundException, UnsupportedEncodingException, IOException
{
    calcMeans();
    calcAvgRulesBySeed();
    
    // Create JFreeChart Dataset
    DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
    
    
    HashMap<String, Double> measuresFirst = algorithmMeasures.entrySet().iterator().next().getValue();
    for (Map.Entry<String, Double> measure : measuresFirst.entrySet())
    {
        String measureName = measure.getKey();
        //Double measureValue = measure.getValue();
        dataset.clear();
        
        for (Map.Entry<String, HashMap<String, Double>> entry : algorithmMeasures.entrySet())
        {
            String alg = entry.getKey();
            Double measureValue = entry.getValue().get(measureName);
            
            // Parse algorithm name to show it correctly
            String aName = alg.substring(0, alg.length()-1);
            int startAlgName = aName.lastIndexOf("/");
            aName = aName.substring(startAlgName + 1);
            
            dataset.addValue(measureValue, aName, measureName);
            
            ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
            JFreeChart barChart = ChartFactory.createBarChart("Assotiation Rules Measures", measureName, measureName, dataset, PlotOrientation.VERTICAL, true, true, false);
            StandardChartTheme.createLegacyTheme().apply(barChart);
            
            CategoryItemRenderer renderer = barChart.getCategoryPlot().getRenderer();
            
            // Black and White
            int numItems = algorithmMeasures.size();
            for(int i=0;i<numItems;i++)
            {
                Color color = Color.DARK_GRAY;
                if(i%2 == 1)
                {
                    color = Color.LIGHT_GRAY;
                }
                renderer.setSeriesPaint(i, color);
                renderer.setSeriesOutlinePaint(i, Color.BLACK);
            }
            
            
            int width = 640 * 2; /* Width of the image */
            int height = 480 * 2; /* Height of the image */ 
            
            // JPEG
            File BarChart = new File( outName + "_" + measureName + "_barchart.jpg" );
            ChartUtilities.saveChartAsJPEG( BarChart , barChart , width , height );
            
            // SVG
            SVGGraphics2D g2 = new SVGGraphics2D(width, height);
            Rectangle r = new Rectangle(0, 0, width, height);
            barChart.draw(g2, r);
            File BarChartSVG = new File( outName + "_" + measureName + "_barchart.svg" );
            SVGUtils.writeToSVG(BarChartSVG, g2.getSVGElement());
        }
    }
    /*
    for (Map.Entry<String, HashMap<String, Double>> entry : algorithmMeasures.entrySet())
    {
        String alg = entry.getKey();
        HashMap<String, Double> measures = entry.getValue();
        
        for (Map.Entry<String, Double> entry1 : measures.entrySet())
        {
            String measureName = entry1.getKey();
            Double measureValue = entry1.getValue();
            
            dataset.addValue(measureValue, alg, measureName);
        }
    }
            */
    
}
 
Example 7
Source File: ResultsProccessor.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
public void writeToFile(String outName) throws FileNotFoundException, UnsupportedEncodingException, IOException
{
    //calcMeans();
    
    // Create JFreeChart Dataset
    DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset( );
    
    
    HashMap<String, ArrayList<Double> > measuresFirst = algorithmMeasures.entrySet().iterator().next().getValue();
    for (Map.Entry<String, ArrayList<Double> > measure : measuresFirst.entrySet())
    {
        String measureName = measure.getKey();
        //Double measureValue = measure.getValue();
        dataset.clear();
        
        for (Map.Entry<String, HashMap<String, ArrayList<Double> >> entry : algorithmMeasures.entrySet())
        {
            String alg = entry.getKey();
            ArrayList<Double> measureValues = entry.getValue().get(measureName);
            
            // Parse algorithm name to show it correctly
            String aName = alg.substring(0, alg.length()-1);
            int startAlgName = aName.lastIndexOf("/");
            aName = aName.substring(startAlgName + 1);
            
            dataset.add(measureValues, aName, measureName);
        }
        
        // Tutorial: http://www.java2s.com/Code/Java/Chart/JFreeChartBoxAndWhiskerDemo.htm
        final CategoryAxis xAxis = new CategoryAxis("Algorithm");
        final NumberAxis yAxis = new NumberAxis("Value");
        yAxis.setAutoRangeIncludesZero(false);
        final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
        
        // Black and White
        int numItems = algorithmMeasures.size();
        for(int i=0;i<numItems;i++)
        {
            Color color = Color.DARK_GRAY;
            if(i%2 == 1)
            {
                color = Color.LIGHT_GRAY;
            }
            renderer.setSeriesPaint(i, color);
            renderer.setSeriesOutlinePaint(i, Color.BLACK);
        }
        
        renderer.setMeanVisible(false);
        renderer.setFillBox(false);
        renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator());
        final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

        Font font = new Font("SansSerif", Font.BOLD, 10);
        //ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
        JFreeChart jchart = new JFreeChart("Assotiation Rules Measures - BoxPlot", font, plot, true);
        //StandardChartTheme.createLegacyTheme().apply(jchart);
     
        int width = 640 * 2; /* Width of the image */
        int height = 480 * 2; /* Height of the image */ 

        // JPEG
        File chart = new File( outName + "_" + measureName + "_boxplot.jpg" );
        ChartUtilities.saveChartAsJPEG( chart , jchart , width , height );

        // SVG
        SVGGraphics2D g2 = new SVGGraphics2D(width, height);
        Rectangle r = new Rectangle(0, 0, width, height);
        jchart.draw(g2, r);
        File BarChartSVG = new File( outName + "_" + measureName + "_boxplot.svg" );
        SVGUtils.writeToSVG(BarChartSVG, g2.getSVGElement());
    }
    
}
 
Example 8
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 JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 9
Source File: ServletUtilities.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Saves the chart as a JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 10
Source File: ServletUtilities.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Saves the chart as a JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used 
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg", 
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 11
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 JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}
 
Example 12
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 JPEG format file in the temporary directory and
 * populates the <code>ChartRenderingInfo</code> object which can be used
 * to generate an HTML image map.
 * <p>
 * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,
 * it is a "lossy" format that introduces visible distortions in the
 * resulting image - use PNG instead.  In addition, note that JPEG output
 * is supported by JFreeChart only for JRE 1.4.2 or later.
 *
 * @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
 * @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 saveChartAsJPEG(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, ".jpeg",
            new File(System.getProperty("java.io.tmpdir")));
    ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);
    if (session != null) {
        ServletUtilities.registerChartForDeletion(tempFile, session);
    }
    return tempFile.getName();

}