List of usage examples for org.jfree.chart ChartUtilities encodeAsPNG
public static byte[] encodeAsPNG(BufferedImage image) throws IOException
From source file:com.gallatinsystems.common.util.JFreechartChartUtil.java
/** * generates a pie chart with the set of labels and values passed in (the labels and values * arrays must both be non-null and contain the same number of elements). The chart is returned * as a byte array representing the image. * //from w w w .j a va 2s. c om * @param labels * @param values * @param title * @param width * @param height * @return - byte array containing the image, null if there is an error. */ public static byte[] getPieChart(List<String> labels, List<String> values, String title, int width, int height) { DefaultPieDataset pieDataset = new DefaultPieDataset(); for (int i = 0; i < labels.size(); i++) { pieDataset.setValue(labels.get(i) + " (" + values.get(i) + ")", Double.parseDouble(values.get(i))); } JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false); try { return ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height)); } catch (Exception e) { e.printStackTrace(System.err); return null; } }
From source file:ch.ksfx.util.ImageStreamResponse.java
public InputStream getStream() throws IOException { ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); ChartUtilities.encodeAsPNG(bufferedImage); return new ByteArrayInputStream(ChartUtilities.encodeAsPNG(bufferedImage)); }
From source file:br.com.caelum.vraptor.interceptor.download.JFreeChartDownload.java
public void write(HttpServletResponse response) throws IOException { byte[] bs = ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height)); new ByteArrayDownload(bs, "image/png", fileName, true).write(response); }
From source file:com.awesheet.models.charts.BarChart.java
@Override public boolean generateImageData() { // Create the dataset. DefaultCategoryDataset dataset = createDataset(); if (dataset == null) { return false; }/* w w w . j a v a2 s . c o m*/ // Create the chart. JFreeChart barChart = ChartFactory.createBarChart(title, nameX, nameY, dataset, PlotOrientation.VERTICAL, true, true, false); final CategoryPlot plot = barChart.getCategoryPlot(); ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter()); // Generate the image. try { imageData = ChartUtilities.encodeAsPNG(barChart.createBufferedImage(720, 480)); } catch (IOException e) { return false; } return true; }
From source file:com.awesheet.models.charts.LineChart.java
@Override public boolean generateImageData() { // Create the dataset. DefaultCategoryDataset dataset = createDataset(); if (dataset == null) { return false; }//w w w . ja v a2s. c o m // Create the chart. JFreeChart lineChart = ChartFactory.createLineChart(title, nameX, nameY, dataset, PlotOrientation.VERTICAL, true, true, false); //final CategoryPlot plot = lineChart.getCategoryPlot(); //((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter()); // Generate the image. try { imageData = ChartUtilities.encodeAsPNG(lineChart.createBufferedImage(720, 480)); } catch (IOException e) { return false; } return true; }
From source file:Com.Operaciones.java
public static InputStream generateBarras(String text, int h, int w) throws Exception { Charset charset = Charset.forName("ISO-8859-1"); CharsetEncoder encoder = charset.newEncoder(); byte[] b = null; ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(text)); b = bbuf.array();/* ww w . j a v a2 s.c o m*/ String data = new String(b, "ISO-8859-1"); // get a byte matrix for the data BitMatrix matrix = null; matrix = new Code128Writer().encode(data, BarcodeFormat.CODE_128, w, h, null); int height = matrix.getHeight(); int width = matrix.getWidth(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } byte[] imgByte = ChartUtilities.encodeAsPNG(image); InputStream myInputStream = new ByteArrayInputStream(imgByte); return myInputStream; }
From source file:org.xwiki.chart.internal.DefaultChartGenerator.java
/** * {@inheritDoc}// w ww. ja v a2s .c o m */ public byte[] generate(ChartModel model, Map<String, String> parameters) throws ChartGeneratorException { setDefaultParams(parameters); String type = parameters.get(TYPE_PARAM); String title = parameters.get(TITLE_PARAM); PlotGenerator generator = plotGenerators.get(type); if (null == generator) { throw new ChartGeneratorException(String.format("No such chart type : [%s].", type)); } Plot plot = generator.generate(model, parameters); JFreeChart jfchart = new JFreeChart(title, plot); int width = Integer.parseInt(parameters.get(WIDTH_PARAM)); int height = Integer.parseInt(parameters.get(HEIGHT_PARAM)); try { return ChartUtilities.encodeAsPNG(jfchart.createBufferedImage(width, height)); } catch (IOException ex) { throw new ChartGeneratorException("Error while png encoding the chart image."); } }
From source file:de.bund.bfr.knime.chart.ChartUtils.java
public static ImagePortObject getImage(JFreeChart chart, boolean asSvg, int width, int height) { if (asSvg) {//from www. j a v a2s . com SVGDocument document = (SVGDocument) new SVGDOMImplementation().createDocument(null, "svg", null); SVGGraphics2D g = new SVGGraphics2D(document); g.setSVGCanvasSize(new Dimension(width, height)); if (chart != null) { chart.draw(g, new Rectangle2D.Double(0, 0, width, height)); } g.dispose(); document.replaceChild(g.getRoot(), document.getDocumentElement()); return new ImagePortObject(new SvgImageContent(document), new ImagePortObjectSpec(SvgCell.TYPE)); } else { try { BufferedImage img = chart != null ? chart.createBufferedImage(width, height) : new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); return new ImagePortObject(new PNGImageContent(ChartUtilities.encodeAsPNG(img)), new ImagePortObjectSpec(PNGImageContent.TYPE)); } catch (IOException e) { e.printStackTrace(); return null; } } }
From source file:org.easyrec.controller.StatisticsController.java
@Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { if (Security.isSignedIn(request)) { int tenant; int month; int year; boolean flot; String actionType = request.getParameter("actionType"); try {/*from w w w. j av a2 s . c o m*/ tenant = Integer.parseInt(request.getParameter("tenant")); month = Integer.parseInt(request.getParameter("month")); year = Integer.parseInt(request.getParameter("year")); flot = Integer.parseInt(request.getParameter("flot")) != 0; } catch (Exception e) { logger.warn(e); return null; } ModelAndView mav = new ModelAndView(); XYSeriesCollection dataset = new XYSeriesCollection(); FlotDataSet flotDataSet = new FlotDataSet(); Calendar from = Calendar.getInstance(); Calendar to = Calendar.getInstance(); from.set(year, month, Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH), 0, 0, 0); to.set(year, month, from.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59); Integer actionTypeId = null; Integer assocTypeId = null; if (!Strings.isNullOrEmpty(actionType)) { if ("CLICKS_ON_RECS".equals(actionType)) assocTypeId = 1001; else if ("CLICKS_ON_CHARTS".equals(actionType)) assocTypeId = 998; else actionTypeId = typeMappingService.getIdOfActionType(tenant, actionType); } HashMap<Integer, HashMap<Integer, Integer>> actionBundleMap = statisticsDAO.getActionBundleMap(tenant, from.getTimeInMillis(), to.getTimeInMillis(), actionTypeId, assocTypeId); Iterator<Integer> iterator = actionBundleMap.keySet().iterator(); while (iterator.hasNext()) { actionTypeId = iterator.next(); if (actionTypeId == 1001) actionType = "clicks on recommendations"; else if (actionTypeId == 998) actionType = "clicks on rankings"; else actionType = typeMappingService.getActionTypeById(tenant, actionTypeId).toLowerCase() + " actions"; XYSeries xySeries = new XYSeries(actionType); FlotSeries flotSeries = new FlotSeries(); flotSeries.setTitle(actionType); for (int i = 1; i <= 31; i++) { Integer y = actionBundleMap.get(actionTypeId).get(i); xySeries.add(i, y != null ? y : 0); flotSeries.add(i, y != null ? y : 0); } //mav.addObject("data",flotDataSet.toString()); dataset.addSeries(xySeries); flotDataSet.add(flotSeries); } // create datapoints that are rendered in the clients browser // return array or html side that renders array if (flot) { boolean onlyData = (ServletUtils.getSafeParameter(request, "onlyData", 0) != 0); if (onlyData) { mav.setViewName("flot/dataOutput"); } else { mav.setViewName("flot/flotPlot"); } mav.addObject("data", flotDataSet.toString()); mav.addObject("flotDataSet", flotDataSet.getData()); mav.addObject("noActions", flotDataSet.getData().isEmpty()); return mav; // create a png } else { JFreeChart action_chart = ChartFactory.createXYLineChart("", "actions", "days", dataset, PlotOrientation.VERTICAL, true, // show legend true, // show tooltips false); // show urls XYPlot plot = action_chart.getXYPlot(); ValueAxis axis = plot.getDomainAxis(); axis.setRange(1, 31); plot.setDomainAxis(axis); BufferedImage bi = action_chart.createBufferedImage(300, 200); byte[] bytes = ChartUtilities.encodeAsPNG(bi); if (bytes != null & !flot) { OutputStream os = response.getOutputStream(); response.setContentType("image/png"); response.setContentLength(bytes.length); os.write(bytes); os.close(); } } return null; } else { return Security.redirectHome(request, response); } }