List of usage examples for org.jfree.chart ChartUtilities saveChartAsPNG
public static void saveChartAsPNG(File file, JFreeChart chart, int width, int height) throws IOException
From source file:com.che.software.testato.web.controller.SelectivePrioritizationController.java
/** * Getter for the private field value fullFitRiskChart. * /* w w w . j a va2 s . co m*/ * @return the fullFitRiskChart field value. */ public StreamedContent getFullFitRiskChart() { if (null == fullFitRiskChart) { LOGGER.debug("getFullFitRiskChart(): intialization."); File file = new File("fullFitRiskChart"); try { ChartUtilities.saveChartAsPNG(file, LineChartGraphistUtil.getColorizedChartFromChart( selectiveChartManager.createSelectiveChart(getSelectedFitResults(), getSelectedRiskResults(), LocaleUtil.getResourceBundleStringByName(LocaleUtil.FIT_CRITERION_NAME) + "/" + LocaleUtil.getResourceBundleStringByName( LocaleUtil.RISK_CRITERION_NAME), LocaleUtil.getResourceBundleStringByName(LocaleUtil.FIT_CRITERION_NAME), LocaleUtil.getResourceBundleStringByName(LocaleUtil.RISK_CRITERION_NAME), getFitResults(), getRiskResults()), LineChartGraphistUtil.getMaxAbscissaValue(getSelectedFitResults(), getFitResults()), getSelectedFitResults(), getSelectedRiskResults(), getFitResults(), getRiskResults()), ColorUtil.FULL_SCREEN_SIZE.width, ColorUtil.FULL_SCREEN_SIZE.height); fullFitRiskChart = new DefaultStreamedContent(new FileInputStream(file)); } catch (IOException e) { LOGGER.error("Error during the full fit-risk chart recovery.", e); } } return fullFitRiskChart; }
From source file:asl.util.PlotMaker.java
public void plotSpecAmp2(double freq[], double[] amp1, double[] phase1, double[] amp2, double[] phase2, String plotTitle, String pngName) { /**/* w ww . j a v a 2s. co m*/ final String plotTitle = String.format("%04d%03d.%s.%s %s", date.get(Calendar.YEAR), date.get(Calendar.DAY_OF_YEAR) ,station, channel, plotString); final String pngName = String.format("%s/%04d%03d.%s.%s.%s.png", outputDir, date.get(Calendar.YEAR), date.get(Calendar.DAY_OF_YEAR) ,station, channel, plotString); **/ File outputFile = new File(pngName); // Check that we will be able to output the file without problems and if not --> return if (!checkFileOut(outputFile)) { System.out.format("== plotSpecAmp: request to output plot=[%s] but we are unable to create it " + " --> skip plot\n", pngName); return; } // Plot x-axis (frequency) range final double XMIN = .00009; final double XMAX = freq[freq.length - 1]; System.out.format("== plotSpecAmp2: nfreq=%d npts=%d pngName=%s\n", freq.length, amp2.length, pngName); final XYSeries series1 = new XYSeries("Amp_PZ"); final XYSeries series1b = new XYSeries("Amp_Cal"); final XYSeries series2 = new XYSeries("Phase_PZ"); final XYSeries series2b = new XYSeries("Phase_Cal"); double maxdB = 0.; for (int k = 0; k < freq.length; k++) { double dB = amp1[k]; //double dB = 20. * Math.log10( amp1[k] ); //series1.add( freq[k], dB ); //series1.add( freq[k], 20. * Math.log10( amp1[k] ) ); //series1b.add(freq[k], 20. * Math.log10( amp2[k] )); series1.add(freq[k], amp1[k]); series1b.add(freq[k], amp2[k]); series2.add(freq[k], phase1[k]); series2b.add(freq[k], phase2[k]); if (dB > maxdB) { maxdB = dB; } } //final XYItemRenderer renderer = new StandardXYItemRenderer(); final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); Rectangle rectangle = new Rectangle(3, 3); renderer.setSeriesShape(0, rectangle); //renderer.setSeriesShapesVisible(0, true); renderer.setSeriesShapesVisible(0, false); renderer.setSeriesLinesVisible(0, true); renderer.setSeriesShape(1, rectangle); renderer.setSeriesShapesVisible(1, true); renderer.setSeriesLinesVisible(1, false); Paint[] paints = new Paint[] { Color.red, Color.blue }; renderer.setSeriesPaint(0, paints[0]); //renderer.setSeriesPaint(1, paints[1]); final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(); renderer2.setSeriesPaint(0, paints[1]); renderer2.setSeriesShapesVisible(0, false); renderer2.setSeriesLinesVisible(0, true); // Stroke is part of Java Swing ... //renderer2.setBaseStroke( new Stroke( ... ) ); double ymax; if (maxdB < 10) { ymax = 10.; } else { ymax = maxdB + 2; ; } final NumberAxis verticalAxis = new NumberAxis("Spec Amp (dB)"); verticalAxis.setRange(new Range(-40, ymax)); verticalAxis.setTickUnit(new NumberTickUnit(5)); //final LogarithmicAxis verticalAxis = new LogarithmicAxis("Amplitude Response"); //verticalAxis.setRange( new Range(0.01 , 10) ); final LogarithmicAxis horizontalAxis = new LogarithmicAxis("Frequency (Hz)"); //horizontalAxis.setRange( new Range(0.0001 , 100.5) ); //horizontalAxis.setRange( new Range(0.00009 , 110) ); horizontalAxis.setRange(new Range(XMIN, XMAX)); final XYSeriesCollection seriesCollection = new XYSeriesCollection(); seriesCollection.addSeries(series1); seriesCollection.addSeries(series1b); final XYPlot xyplot = new XYPlot((XYDataset) seriesCollection, null, verticalAxis, renderer); //final XYPlot xyplot = new XYPlot((XYDataset)seriesCollection, horizontalAxis, verticalAxis, renderer); xyplot.setDomainGridlinesVisible(true); xyplot.setRangeGridlinesVisible(true); xyplot.setRangeGridlinePaint(Color.black); xyplot.setDomainGridlinePaint(Color.black); final NumberAxis phaseAxis = new NumberAxis("Phase (Deg)"); phaseAxis.setRange(new Range(-180, 180)); phaseAxis.setTickUnit(new NumberTickUnit(30)); final XYSeriesCollection seriesCollection2 = new XYSeriesCollection(); seriesCollection2.addSeries(series2); seriesCollection2.addSeries(series2b); final XYPlot xyplot2 = new XYPlot((XYDataset) seriesCollection2, null, phaseAxis, renderer2); //CombinedXYPlot combinedPlot = new CombinedXYPlot( horizontalAxis, CombinedXYPlot.VERTICAL ); CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(horizontalAxis); combinedPlot.add(xyplot, 1); combinedPlot.add(xyplot2, 1); combinedPlot.setGap(15.); //final JFreeChart chart = new JFreeChart(xyplot); final JFreeChart chart = new JFreeChart(combinedPlot); chart.setTitle(new TextTitle(plotTitle)); try { ChartUtilities.saveChartAsPNG(outputFile, chart, 1000, 800); } catch (IOException e) { System.err.println("Problem occurred creating chart."); } }
From source file:cs.cirg.cida.CIDAView.java
@Action public void savePlotPNG() { JFreeChart chartToSave = ((ChartPanel) chartPanel).getChart(); String name = chartToSave.getTitle().getText(); name = name.trim().replaceAll(" ", ""); try {//from w ww . ja v a 2 s . co m ChartUtilities.saveChartAsPNG(new File(name + CIDAConstants.EXT_PNG), chartToSave, CIDAConstants.DEFAULT_CHART_HORIZONTAL_RES, CIDAConstants.DEFAULT_CHART_VERTICAL_RES); } catch (IOException ex) { CIDAPromptDialog dialog = exceptionController.handleException(this.getFrame(), ex, CIDAConstants.DIALOG_NEW_NAME_MSG); dialog.displayPrompt(); } }
From source file:cish.CISH.java
@Override public String getHTMLReport(String HTMLFolderName) { String output = "<html>"; String lb = "\n"; if (!tss.isEmpty()) { // Save the parameters output += "<table><tr>" + lb + " <td colspan=2><i><u>CISH Parameters</u></i></td>" + lb + " </tr>" + lb + " <tr>" + lb + " <td><b>Point Radius:</b></td>" + lb + " <td>" + getParam_PointSignalRadius() + "</td>" + lb + " </tr>" + lb + " <tr>" + lb + " <td><b>Number of Random Points for Local Ratio:</b></td>" + lb + " <td>" + getParam_nPts() + "</td>" + lb + " </tr>" + lb + " <tr>" + lb + " <td><b>Polarity:</b></td>" + lb + " <td>" + (getParam_darkpoints() ? "Detect Dark Points" : "Detect Light Points") + "</td>" + lb + " </tr>" + lb; output += "</table><br>" + lb; // Save the CISH Table output += "<table>" + lb + " <tr>" + lb; for (int i = 0; i < jXTable1.getColumnCount(); i++) { output += " <th>" + jXTable1.getColumnName(i) + "</th>" + lb; }/*from w w w . jav a2 s . com*/ output += " </tr>" + lb; for (int i = 0; i < jXTable1.getRowCount(); i++) { output += " <tr>" + lb; for (int j = 0; j < jXTable1.getColumnCount(); j++) { output += " <td>" + jXTable1.getStringAt(i, j) + "</td>" + lb; } output += " </tr>" + lb; } output += "</table>" + lb; // Save the CISH plot image ChartPanel chartpanel = (ChartPanel) jPanel1.getComponent(0); if (chartpanel != null) { File file_tmp = new File(HTMLFolderName + "CISHPlot.png"); try { ChartUtilities.saveChartAsPNG(file_tmp, chartpanel.getChart(), chartpanel.getWidth(), chartpanel.getHeight()); } catch (IOException ex) { Logger.getLogger(CISH.class.getName()).log(Level.SEVERE, null, ex); } output += "<br><br>" + lb; output += "<img src=\"" + HTMLFolderName + "CISHPlot.png\" alt=\"Global vs. Local Ratio Plot\"><br><br>" + lb; } // Save the TMA spot images with CISH marks for (TMAspot ts : tss) { try { File file_tmp = new File(HTMLFolderName + ts.getName() + "_CISH.jpg"); BufferedImage bi = ts.getBufferedImage(); drawInformationPreNuclei(ts, bi.createGraphics(), 1, 0, 0, bi.getWidth(), bi.getHeight()); ImageIO.write(bi, "jpg", file_tmp); output += "<a href=\"" + HTMLFolderName + ts.getName() + "_CISH.jpg\">" + "<img src=\"" + HTMLFolderName + ts.getName() + "_CISH.jpg\" alt=\"" + ts.getName() + " CISH Points\" width=\"100\">" + "</a> " + lb; } catch (IOException ex) { Logger.getLogger(CISH.class.getName()).log(Level.SEVERE, null, ex); } } output += "<br>" + "<font color=\"" + Misc.Color2HTML(COLORS[0]) + "\"><b>centromer</b></font> " + "<font color=\"" + Misc.Color2HTML(COLORS[1]) + "\"><b>gene</b></font> " + "<font color=\"" + Misc.Color2HTML(COLORS[4]) + "\"><b>both</b></font> " + "<br><br>" + lb; } output += "</html>"; return output; }
From source file:eu.cassandra.utils.Utils.java
/** * This function is used for the visualization of a Line Diagram. * //from w w w . j av a 2s . co m * @param title * The title of the chart. * @param x * The unit on the X axis of the chart. * @param y * The unit on the Y axis of the chart. * @param data * The array of values. * @return a chart panel with the graphical representation. */ public static void createLineDiagram(String title, String x, String y, ArrayList<Double> data) { XYSeries series1 = new XYSeries("Active Power"); for (int i = 0; i < data.size(); i++) { series1.add(i, data.get(i)); } XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series1); PlotOrientation orientation = PlotOrientation.VERTICAL; boolean show = true; boolean toolTips = false; boolean urls = false; JFreeChart chart = ChartFactory.createXYLineChart(title, x, y, dataset, orientation, show, toolTips, urls); int width = 1024; int height = 768; try { ChartUtilities.saveChartAsPNG(new File(Constants.chartFolder + title + ".PNG"), chart, width, height); } catch (IOException e) { } }
From source file:org.matsim.integration.weekly.fundamentaldiagram.CreateAutomatedFDTest.java
private void scatterPlot(Map<Double, Map<String, Tuple<Double, Double>>> inputData, String outFile) { String mode1 = travelModes[0]; XYSeries carFlow = new XYSeries(mode1 + " flow"); XYSeries carSpeed = new XYSeries(mode1 + " speed"); XYSeries bikeFlow = null;/* w ww . ja v a 2 s. c o m*/ XYSeries bikeSpeed = null; if (travelModes.length == 2) { bikeFlow = new XYSeries(travelModes[1] + " flow"); bikeSpeed = new XYSeries(travelModes[1] + " speed"); } for (double d : inputData.keySet()) { carFlow.add(d, inputData.get(d).get(mode1).getFirst()); carSpeed.add(d, inputData.get(d).get(mode1).getSecond()); if (travelModes.length == 2) { bikeFlow.add(d, inputData.get(d).get(travelModes[1]).getFirst()); bikeSpeed.add(d, inputData.get(d).get(travelModes[1]).getSecond()); } } // flow vs density XYSeriesCollection flowDataset = new XYSeriesCollection(); flowDataset.addSeries(carFlow); NumberAxis flowAxis = new NumberAxis("Flow (PCU/h)"); flowAxis.setRange(0.0, 1700.0); XYPlot plot1 = new XYPlot(flowDataset, null, flowAxis, new XYLineAndShapeRenderer(false, true)); plot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); // speed vs density XYSeriesCollection speedDataset = new XYSeriesCollection(); speedDataset.addSeries(carSpeed); if (travelModes.length == 2) { flowDataset.addSeries(bikeFlow); speedDataset.addSeries(bikeSpeed); } NumberAxis speedAxis = new NumberAxis("Speed (m/s)"); speedAxis.setRange(0.0, 17.0); XYPlot plot2 = new XYPlot(speedDataset, null, speedAxis, new XYLineAndShapeRenderer(false, true)); plot2.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); NumberAxis densityAxis = new NumberAxis("Overall density (PCU/km)"); densityAxis.setRange(0.0, 140.00); CombinedDomainXYPlot plot = new CombinedDomainXYPlot(densityAxis); plot.setGap(10.); plot.add(plot1); plot.add(plot2); plot.setOrientation(PlotOrientation.VERTICAL); JFreeChart chart = new JFreeChart("Fundamental diagrams", JFreeChart.DEFAULT_TITLE_FONT, plot, true); try { ChartUtilities.saveChartAsPNG(new File(outFile), chart, 800, 600); } catch (IOException e) { throw new RuntimeException("Data is not plotted. Reason " + e); } }
From source file:org.mwc.cmap.grideditor.chart.FixedChartComposite.java
/** * Opens a file chooser and gives the user an opportunity to save the chart in * PNG format.//from w w w. j av a 2 s.c om * * @throws IOException * if there is an I/O error. */ public void doSaveAs() throws IOException { final FileDialog fileDialog = new FileDialog(this.canvas.getShell(), SWT.SAVE); final 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); } }
From source file:asl.util.PlotMaker.java
public void plotSpecAmp(double freq[], double[] amp, double[] phase, String plotString) { // plotTitle = "2012074.IU_ANMO.00-BHZ " + plotString final String plotTitle = String.format("%04d%03d.%s.%s %s", date.get(Calendar.YEAR), date.get(Calendar.DAY_OF_YEAR), station, channel, plotString); // plot filename = "2012074.IU_ANMO.00-BHZ" + plotString + ".png" final String pngName = String.format("%s/%04d%03d.%s.%s.%s.png", outputDir, date.get(Calendar.YEAR), date.get(Calendar.DAY_OF_YEAR), station, channel, plotString); File outputFile = new File(pngName); // Check that we will be able to output the file without problems and if not --> return if (!checkFileOut(outputFile)) { System.out.format("== plotSpecAmp: request to output plot=[%s] but we are unable to create it " + " --> skip plot\n", pngName); return;/*from www . ja v a2s. c o m*/ } final XYSeries series1 = new XYSeries("Amplitude"); final XYSeries series2 = new XYSeries("Phase"); double maxdB = 0.; for (int k = 0; k < freq.length; k++) { double dB = 20. * Math.log10(amp[k]); series1.add(freq[k], dB); series2.add(freq[k], phase[k]); if (dB > maxdB) { maxdB = dB; } } //final XYItemRenderer renderer = new StandardXYItemRenderer(); final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); Rectangle rectangle = new Rectangle(3, 3); renderer.setSeriesShape(0, rectangle); //renderer.setSeriesShapesVisible(0, true); renderer.setSeriesShapesVisible(0, false); renderer.setSeriesLinesVisible(0, true); renderer.setSeriesShape(1, rectangle); renderer.setSeriesShapesVisible(1, true); renderer.setSeriesLinesVisible(1, false); Paint[] paints = new Paint[] { Color.red, Color.blue }; renderer.setSeriesPaint(0, paints[0]); //renderer.setSeriesPaint(1, paints[1]); final XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer(); renderer2.setSeriesPaint(0, paints[1]); renderer2.setSeriesShapesVisible(0, false); renderer2.setSeriesLinesVisible(0, true); // Stroke is part of Java Swing ... //renderer2.setBaseStroke( new Stroke( ... ) ); double ymax; if (maxdB < 10) { ymax = 10.; } else { ymax = maxdB + 2; ; } final NumberAxis verticalAxis = new NumberAxis("Spec Amp (dB)"); verticalAxis.setRange(new Range(-40, ymax)); verticalAxis.setTickUnit(new NumberTickUnit(5)); //final LogarithmicAxis verticalAxis = new LogarithmicAxis("Amplitude Response"); //verticalAxis.setRange( new Range(0.01 , 10) ); final LogarithmicAxis horizontalAxis = new LogarithmicAxis("Frequency (Hz)"); //horizontalAxis.setRange( new Range(0.0001 , 100.5) ); horizontalAxis.setRange(new Range(0.00009, 110)); final XYSeriesCollection seriesCollection = new XYSeriesCollection(); seriesCollection.addSeries(series1); final XYPlot xyplot = new XYPlot((XYDataset) seriesCollection, null, verticalAxis, renderer); //final XYPlot xyplot = new XYPlot((XYDataset)seriesCollection, horizontalAxis, verticalAxis, renderer); xyplot.setDomainGridlinesVisible(true); xyplot.setRangeGridlinesVisible(true); xyplot.setRangeGridlinePaint(Color.black); xyplot.setDomainGridlinePaint(Color.black); final NumberAxis phaseAxis = new NumberAxis("Phase (Deg)"); phaseAxis.setRange(new Range(-180, 180)); phaseAxis.setTickUnit(new NumberTickUnit(30)); final XYSeriesCollection seriesCollection2 = new XYSeriesCollection(); seriesCollection2.addSeries(series2); final XYPlot xyplot2 = new XYPlot((XYDataset) seriesCollection2, null, phaseAxis, renderer2); //CombinedXYPlot combinedPlot = new CombinedXYPlot( horizontalAxis, CombinedXYPlot.VERTICAL ); CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(horizontalAxis); combinedPlot.add(xyplot, 1); combinedPlot.add(xyplot2, 1); combinedPlot.setGap(15.); //final JFreeChart chart = new JFreeChart(xyplot); final JFreeChart chart = new JFreeChart(combinedPlot); chart.setTitle(new TextTitle(plotTitle)); // Here we need to see if test dir exists and create it if necessary ... try { //ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500, 300); //ChartUtilities.saveChartAsPNG(outputFile, chart, 500, 300); ChartUtilities.saveChartAsPNG(outputFile, chart, 1000, 800); } catch (IOException e) { System.err.println("Problem occurred creating chart."); } }
From source file:es.uvigo.darwin.jmodeltest.io.HtmlReporter.java
private static void buildChart(File mOutputFile, InformationCriterion ic) { /* This line prevents Swing exceptions on headless environments */ if (GraphicsEnvironment.isHeadless()) { return;/*ww w .j a va2 s . co m*/ } int width = 500; int height = 300; try { if (!IMAGES_DIR.exists()) { IMAGES_DIR.mkdir(); } ChartUtilities.saveChartAsPNG( new File(IMAGES_DIR.getPath() + File.separator + mOutputFile.getName() + "_rf_" + ic + ".png"), RFHistogram.buildRFHistogram(ic), width, height); ChartUtilities.saveChartAsPNG( new File(IMAGES_DIR.getPath() + File.separator + mOutputFile.getName() + "_eu_" + ic + ".png"), RFHistogram.buildEuclideanHistogram(ic), width, height); } catch (IOException e) { // e.printStackTrace(); } }
From source file:keel.GraphInterKeel.datacf.visualizeData.VisualizePanelCharts2D.java
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);//from ww w . ja v a 2 s . c o m 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) { } } } }