List of usage examples for org.jfree.chart ChartUtilities saveChartAsJPEG
public static void saveChartAsJPEG(File file, JFreeChart chart, int width, int height) throws IOException
From source file:mil.tatrc.physiology.biogears.verification.ScenarioPlotTool.java
public void createGraph(String toDir, Paint color, String title, String XAxisLabel, String YAxisLabel, XYSeries... xyData) {/*from w ww .jav a2 s .co m*/ new File(toDir).mkdir(); Log.info("Creating Graph " + toDir + title); double resMin0 = 1.e6; double resMax0 = -1.e6; double resMin1 = 1.e6; double resMax1 = -1.e6; XYSeriesCollection dataSet = new XYSeriesCollection(); for (XYSeries data : xyData) dataSet.addSeries(data); JFreeChart chart = ChartFactory.createXYLineChart(title, // chart title XAxisLabel, // x axis label YAxisLabel, // y axis label dataSet, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); XYPlot plot = (XYPlot) chart.getPlot(); if (title.contains("Residual")) { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); if (Math.abs(xyData[0].getMinY()) > Math.abs(xyData[0].getMaxY())) resMax0 = Math.abs(resMin0); if (Math.abs(xyData[0].getMaxY()) > Math.abs(xyData[0].getMinY())) resMin0 = -1.0 * Math.abs(resMax0); if ((resMin0 == 0.0) && (resMax0 == 0.0)) { resMin0 = -0.00001; resMax0 = 0.00001; } ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05 * resMin0, resMax0 + 0.05 * resMax0);//5% buffer so we can see top and bottom clearly } else if (title.contains("Error")) { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); if ((resMin0 == 0.0) && (resMax0 == 0.0)) { resMin0 = -0.00001; resMax0 = 0.00001; } if (resMin0 >= 0.0) resMin0 = -0.01; ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05 * resMin0, resMax0 + 0.05 * resMax0);//5% buffer so we can see top and bottom clearly /* yAxis.setTickLabelPaint(new Color(1,0,0)); yAxis.setTickMarkPaint(new Color(1,0,0)); yAxis.setAxisLinePaint(new Color(1,0,0)); yAxis.setLabelPaint(new Color(1,0,0)); ValueAxis xAxis = plot.getDomainAxis(); xAxis.setTickLabelPaint(new Color(1,0,0)); xAxis.setTickMarkPaint(new Color(1,0,0)); yAxis.setAxisLinePaint(new Color(1,0,0)); yAxis.setLabelPaint(new Color(1,0,0)); */ } else { if (xyData.length > 1) { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); resMax1 = xyData[1].getMaxY(); resMin1 = xyData[1].getMinY(); if (resMin1 < resMin0) resMin0 = resMin1; if (resMax1 > resMax0) resMax0 = resMax1; if ((resMin0 == 0.0) && (resMax0 == 0.0)) { resMin0 = -0.00001; resMax0 = 0.00001; } if (resMin0 >= 0.0) resMin0 = -0.01; if (YAxisLabel.indexOf("PlasmaConcentration") > -1) plot.setRangeAxis(new LogarithmicAxis("Log(" + YAxisLabel + ")")); else { ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05 * resMin0, resMax0 + 0.05 * resMax0);//5% buffer so we can see top and bottom clearly } } else { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); if ((resMin0 == 0.0) && (resMax0 == 0.0)) { resMin0 = -0.00001; resMax0 = 0.00001; } if (resMin0 >= 0.0) resMin0 = -0.01; if (YAxisLabel.indexOf("PlasmaConcentration") > -1) plot.setRangeAxis(new LogarithmicAxis("Log(" + YAxisLabel + ")")); else { ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05 * resMin0, resMax0 + 0.05 * resMax0);//5% buffer so we can see top and bottom clearly } } } formatXYPlot(chart, color); //Changing line widths and colors XYItemRenderer r = plot.getRenderer(); BasicStroke wideLine = new BasicStroke(lineWidth); r.setSeriesStroke(0, wideLine); r.setSeriesStroke(1, wideLine); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); if (xyData.length > 1) { renderer.setSeriesStroke(//makes a dashed line 0, //argument below float[]{I,K} -> alternates between solid and opaque (solid for I, opaque for K) new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] { 15.0f, 30.0f }, 0.0f)); renderer.setDrawSeriesLineAsPath(true); renderer.setUseFillPaint(true); } renderer.setBaseShapesVisible(false); renderer.setSeriesFillPaint(0, expectedLineColor); renderer.setSeriesFillPaint(1, computedLineColor); renderer.setSeriesPaint(0, expectedLineColor); renderer.setSeriesPaint(1, computedLineColor); try { if (toDir == null || toDir.isEmpty()) toDir = "."; File JPGFile = new File(toDir + "/" + StringUtils.removeParens(title) + ".jpg"); ChartUtilities.saveChartAsJPEG(JPGFile, chart, 1600, 800); } catch (IOException e) { Log.error(e.getMessage()); } }
From source file:skoa.helpers.Graficos.java
private void evolucionDual() { TimeSeries serie;// w w w . jav a 2 s . c om XYDataset dataset = null; ui = 0; nombreFichero = nombresFicheros.elementAt(0); unidad = buscarUnidad(nombreFichero); serie = obtenerSerieEvolucion(); dataset = new TimeSeriesCollection(serie); //Para generar el grfico se usa createTimeSeriesChart para ver la evolucin de las fechas. JFreeChart grafica = ChartFactory.createTimeSeriesChart("Valores medidos de las direcciones de grupo", //titulo "Fechas", //titulo eje x "Mediciones (" + unidad + ")", //titulo eje y dataset, //dataset true, //leyenda true, //tooltips false); //configure chart to generate URLs? //Dar color a cada categoria grafica.setBackgroundPaint(Color.WHITE); //Color del fondo del grfico //CREACIN DEL SEGUNDO EJE CON SU CORRESPONDIENTE DATASET. XYDataset dataset2 = null; ui = 0; nombreFichero = nombresFicheros.elementAt(1); unidad = buscarUnidad(nombreFichero); final XYPlot plot = grafica.getXYPlot(); final NumberAxis axis2 = new NumberAxis("Mediciones (" + unidad + ")"); axis2.setAutoRangeIncludesZero(false); plot.setRangeAxis(1, axis2); serie = obtenerSerieEvolucion(); dataset2 = new TimeSeriesCollection(serie); plot.setDataset(1, dataset2); plot.mapDatasetToRangeAxis(1, 1); final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer(); renderer2.setSeriesPaint(0, Color.blue); plot.setRenderer(1, renderer2); plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));//igual color de fondo que el de barrasDual plot.setDomainGridlinesVisible(true); //Ver lineas divisorias. plot.setRangeGridlinePaint(Color.BLACK);//Color de las lineas divisorias. //--------------------------------------------------------------------- if (fechaInicial.equals("") & fechaFinal.equals("")) { //Si estn vacas es porque no hay resultados para ese intervalo. fechaInicial = " ? "; fechaFinal = " ? "; } TextTitle t = new TextTitle("desde " + fechaInicial + " hasta " + fechaFinal, new Font("SanSerif", Font.ITALIC, 12)); grafica.addSubtitle(t); try { ChartUtilities.saveChartAsJPEG(new File(ruta + "EvolucionSmall.jpg"), grafica, 400, 300); ChartUtilities.saveChartAsJPEG(new File(ruta + "EvolucionBig.jpg"), grafica, 900, 600); } catch (IOException e1) { System.err.println("Problem occurred creating chart." + e1); } }
From source file:at.ac.tuwien.inso.subcat.ui.widgets.TimeChartControlPanel.java
public void saveChartAsJPEG(File file, int width, int height) throws IOException { assert (chart != null); ChartUtilities.saveChartAsJPEG(file, chart, width, height); }
From source file:de.mpg.mpi_inf.bioinf.netanalyzer.ui.charts.JFreeChartConn.java
/** * Saves the given chart to a JPEG image file. * /*ww w. j a v a2 s . c om*/ * @param aFile * File to be saved to. * @param aChart * Chart to be saved. * @param aWidth * Desired width of the image. * @param aHeight * Desired height of the image. * @throws IOException * If I/O error occurs. */ public static void saveAsJpeg(File aFile, JFreeChart aChart, int aWidth, int aHeight) throws IOException { // Force white background because JPEG does not support transparency aChart.setBackgroundPaint(Color.WHITE); ChartUtilities.saveChartAsJPEG(aFile, aChart, aWidth, aHeight); aChart.setBackgroundPaint(null); }
From source file:keel.Algorithms.UnsupervisedLearning.AssociationRules.Visualization.keelassotiationrulesboxplot.ResultsProccessor.java
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();// www. j a v a 2 s . c o m 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()); } }
From source file:spec.reporter.Utils.java
public static void generateMainChart(double compositeScore, TreeMap<String, Double> scores) { // Valid benchmarks + room for all possible extra - compiler, crypto, scimark, scimark.small, scimark.large, startup, xml, composite score Color[] colors = new Color[scores.size() + 8]; // create the dataset... DefaultCategoryDataset dataset = new DefaultCategoryDataset(); int count = 0; Iterator<String> iteratorBenchmarks = scores.keySet().iterator(); while (iteratorBenchmarks.hasNext()) { String key = iteratorBenchmarks.next(); Double score = scores.get(key); if (Utils.isValidScore(score)) { dataset.addValue(score, key, key); colors[count++] = (Color) colorMap.get(key); }// ww w. ja v a 2 s .c om } if (Utils.isValidScore(compositeScore)) { dataset.addValue(compositeScore, Utils.CSCORE_NAME, Utils.CSCORE_NAME); colors[count++] = (Color) colorMap.get(Utils.CSCORE_NAME); } JFreeChart chart = ChartFactory.createStackedBarChart("Scores", // chart title "", // domain axis label "", dataset, // data PlotOrientation.HORIZONTAL, // orientation false, // include legend false, // tooltips? false // URLs? ); CategoryItemRenderer renderer = chart.getCategoryPlot().getRendererForDataset(dataset); for (int i = 0; i < count; i++) { Color paint = (Color) colors[i]; if (paint != null) { renderer.setSeriesPaint(i, paint); } } try { ChartUtilities.saveChartAsJPEG(new File(getFullImageName("all")), chart, 600, 50 + (dataset.getRowCount()) * 20); } catch (Exception e) { e.printStackTrace(); } }
From source file:mil.tatrc.physiology.utilities.csv.plots.CSVPlotTool.java
public void createGraph(String toDir, Paint color, String title, String XAxisLabel, String YAxisLabel, XYSeries... xyData) {//from w w w .j ava 2s. c o m new File(toDir).mkdir(); Log.info("Creating Graph " + toDir + "/" + title); double resMin0 = 1.e6; double resMax0 = -1.e6; double resMin1 = 1.e6; double resMax1 = -1.e6; XYSeriesCollection dataSet = new XYSeriesCollection(); for (XYSeries data : xyData) { if (data != null && !data.isEmpty()) dataSet.addSeries(data); } JFreeChart chart = ChartFactory.createXYLineChart(title, // chart title XAxisLabel, // x axis label YAxisLabel, // y axis label dataSet, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); XYPlot plot = (XYPlot) chart.getPlot(); /* I have residual and error plots turned off, there are some plots that contain these names in their title, and I don't want this code running, need a better way to see if we are doing a special plot rather than title contents if(title.contains("Residual")) { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); if (Math.abs(xyData[0].getMinY()) > Math.abs(xyData[0].getMaxY())) resMax0 = Math.abs(resMin0); if (Math.abs(xyData[0].getMaxY()) > Math.abs(xyData[0].getMinY())) resMin0 = -1.0*Math.abs(resMax0); if((resMin0==0.0) && (resMax0==0.0)) { resMin0 = -0.00001; resMax0 = 0.00001; } ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05*resMin0, resMax0 + 0.05*resMax0);//5% buffer so we can see top and bottom clearly } else if(title.contains("Error")) { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); if((resMin0==0.0) && (resMax0==0.0)) { resMin0 = -0.00001; resMax0 = 0.00001; } if(resMin0>=0.0) resMin0 = -0.01; ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05*resMin0, resMax0 + 0.05*resMax0);//5% buffer so we can see top and bottom clearly /* yAxis.setTickLabelPaint(new Color(1,0,0)); yAxis.setTickMarkPaint(new Color(1,0,0)); yAxis.setAxisLinePaint(new Color(1,0,0)); yAxis.setLabelPaint(new Color(1,0,0)); ValueAxis xAxis = plot.getDomainAxis(); xAxis.setTickLabelPaint(new Color(1,0,0)); xAxis.setTickMarkPaint(new Color(1,0,0)); yAxis.setAxisLinePaint(new Color(1,0,0)); yAxis.setLabelPaint(new Color(1,0,0)); * } else */ { if (title.indexOf("Hemoglobin-GlomerularFilterability") > -1) System.out.println("stop"); if (xyData.length > 1) { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); resMax1 = xyData[1].getMaxY(); resMin1 = xyData[1].getMinY(); if (resMin1 < resMin0) resMin0 = resMin1; if (resMax1 > resMax0) resMax0 = resMax1; if (DoubleUtils.isZero(resMin0)) resMin0 = -0.001; if (DoubleUtils.isZero(resMax0)) resMax0 = 0.001; if (resMin0 >= 0.0) resMin0 = -0.01; if (YAxisLabel.indexOf("PlasmaConcentration") > -1) plot.setRangeAxis(new LogarithmicAxis("Log(" + YAxisLabel + ")")); else { ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05 * resMin0, resMax0 + 0.15 * Math.abs(resMax0));//5% buffer so we can see top and bottom clearly } String NaNCheck = ""; if (Double.isNaN(resMax0) || Double.isNaN(resMin0)) NaNCheck += "Expected is NaN "; if (Double.isNaN(resMax1) || Double.isNaN(resMin1)) NaNCheck += "Computed is NaN "; if (!NaNCheck.isEmpty()) plot.getDomainAxis().setLabel(NaNCheck); } else { // Make plot symmetric about x axis resMax0 = xyData[0].getMaxY(); resMin0 = xyData[0].getMinY(); if (Double.isNaN(resMax0) || Double.isNaN(resMin0)) plot.getDomainAxis().setLabel("Computed is NaN"); if (DoubleUtils.isZero(resMin0)) resMin0 = -0.001; if (DoubleUtils.isZero(resMax0)) resMax0 = 0.001; if (resMin0 >= 0.0) resMin0 = -0.01; if (YAxisLabel.indexOf("PlasmaConcentration") > -1) plot.setRangeAxis(new LogarithmicAxis("Log(" + YAxisLabel + ")")); else { ValueAxis yAxis = plot.getRangeAxis(); yAxis.setRange(resMin0 + 0.05 * resMin0, resMax0 + 0.15 * Math.abs(resMax0));//5% buffer so we can see top and bottom clearly } } } formatXYPlot(chart, color); //Changing line widths and colors XYItemRenderer r = plot.getRenderer(); BasicStroke wideLine = new BasicStroke(lineWidth); r.setSeriesStroke(0, wideLine); r.setSeriesStroke(1, wideLine); XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer(); if (xyData.length > 1) { renderer.setSeriesStroke(//makes a dashed line 0, //argument below float[]{I,K} -> alternates between solid and opaque (solid for I, opaque for K) new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] { 15.0f, 30.0f }, 0.0f)); renderer.setDrawSeriesLineAsPath(true); renderer.setUseFillPaint(true); } renderer.setBaseShapesVisible(false); renderer.setSeriesFillPaint(0, expectedLineColor); renderer.setSeriesFillPaint(1, computedLineColor); renderer.setSeriesPaint(0, expectedLineColor); renderer.setSeriesPaint(1, computedLineColor); try { if (toDir == null || toDir.isEmpty()) toDir = "."; File JPGFile = new File(toDir + "/" + MakeFileName(title) + ".jpg"); ChartUtilities.saveChartAsJPEG(JPGFile, chart, 1600, 800); } catch (IOException e) { Log.error(e.getMessage()); } }
From source file:keel.Algorithms.UnsupervisedLearning.AssociationRules.Visualization.keelassotiationrulesbarchart.ResultsProccessor.java
public void writeToFile(String outName) throws FileNotFoundException, UnsupportedEncodingException, IOException { calcMeans();/* w w w . j ava 2 s . c o m*/ 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); } } */ }
From source file:skoa.helpers.Graficos.java
/**************************************************************************************** * Funcion evolucion(): dados los ficheros a leer, obtiene la serie de cada uno de ellos* * y la aade a la grfica en cuestion. * ****************************************************************************************/ private void evolucion() { TimeSeries serie;// www. j a va 2 s . c o m TimeSeriesCollection dataset = null; for (int i = 0; i < nombresFicheros.size(); i++) { ui = 0; nombreFichero = nombresFicheros.elementAt(i); unidad = unidad + "," + buscarUnidad(nombreFichero); serie = obtenerSerieEvolucion(); if (i == 0) dataset = new TimeSeriesCollection(serie); else dataset.addSeries(serie); } unidad = unidad.substring(1); //Para quitar la coma del principio, introducida por la primera unidad. //Para generar el grfico se usa createTimeSeriesChart para ver la evolucin de las fechas. JFreeChart grafica = ChartFactory.createTimeSeriesChart("Valores medidos de las direcciones de grupo", //titulo "Fechas", //titulo eje x "Mediciones (" + unidad + ")", //titulo eje y dataset, //dataset true, //leyenda true, //tooltips false); //configure chart to generate URLs? //Dar color a cada categoria grafica.setBackgroundPaint(Color.WHITE); //Color del fondo del grfico if (fechaInicial.equals("") & fechaFinal.equals("")) { //Si estn vacas es porque no hay resultados para ese intervalo. fechaInicial = " ? "; fechaFinal = " ? "; } TextTitle t = new TextTitle("desde " + fechaInicial + " hasta " + fechaFinal, new Font("SanSerif", Font.ITALIC, 12)); grafica.addSubtitle(t); try { ChartUtilities.saveChartAsJPEG(new File(ruta + "EvolucionSmall.jpg"), grafica, 400, 300); ChartUtilities.saveChartAsJPEG(new File(ruta + "EvolucionBig.jpg"), grafica, 900, 600); } catch (IOException e1) { System.err.println("Problem occurred creating chart." + e1); } }
From source file:com.comcast.cqs.test.stress.CqsStressTester.java
private byte[] generateChart(String title, ConcurrentHashMap<Integer, AtomicInteger> metric, String id, Date startTime, Date endTime) throws IOException { XYSeries series = new XYSeries(title); for (Entry<Integer, AtomicInteger> entry : metric.entrySet()) { series.add(entry.getKey().intValue(), entry.getValue().intValue()); }/* w w w .ja v a2 s.com*/ XYSeriesCollection dataset = new XYSeriesCollection(series); JFreeChart chart = ChartFactory.createXYBarChart( "Start: " + startTime + " End: " + endTime + " Message Count: " + messageCount, "Test Second", false, "Number of Messages", dataset, PlotOrientation.VERTICAL, true, true, false); File file = new File("/tmp/" + id + ".jpeg"); //File file = new File(getServletContext().getRealPath("WEB-INF" + "/" + id + ".jpeg")); ChartUtilities.saveChartAsJPEG(file, chart, 1600, 400); //byte b[] = Files.toByteArray(file); //return b; ByteArrayOutputStream bos = new ByteArrayOutputStream(); ChartUtilities.writeChartAsJPEG(bos, chart, 2400, 400); return bos.toByteArray(); }