public static void main(String[] args) throws IOException { JFreeChart chart = ChartFactory.createBarChart3D( "图书销量统计图", "图书", // 目录轴的显示标签 "销量", // 数值轴的显示标签 getDataSet(), PlotOrientation.VERTICAL, // 设置图表方向 false, false, false); // 设置标题 chart.setTitle(new TextTitle("图书销量统计图", new Font("黑体", Font.ITALIC, 22))); // 设置图表部分 CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryAxis categoryAxis = plot.getDomainAxis(); // 取得横轴 categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22)); // 设置横轴显示标签的字体 categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 分类标签以45度倾斜 categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18)); // 分类标签字体 NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis(); // 取得纵轴 numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 42)); // 设置纵轴显示标签字体 FileOutputStream fos = null; fos = new FileOutputStream("book1.jpg"); ChartUtilities.writeChartAsJPEG(fos, 1, chart, 800, 600, null); fos.close(); }
public void jpeg() throws IOException { this.response.setContentType("image/jpeg"); StandardChartTheme standardChartTheme = new StandardChartTheme(theme); setChartTheme(standardChartTheme); JFreeChart jFreeChart = createChart(); preRender(jFreeChart); ChartUtilities.writeChartAsJPEG(this.response.getOutputStream(), jFreeChart, width, height); }
@SuppressWarnings("unchecked") public void exportTo(OutputStream os, int width, int height) { Query query = buildQuery(); List<GroupedDataTuple<T>> results = query.list(); JFreeChart finalChart = chart.build(convertTo(results)); try { ChartUtilities.writeChartAsJPEG(os, finalChart, width, height); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void createITextObject(FacesContext context) { if (getBorderBackgroundPaint() != null) { chart.setBackgroundPaint(findColor(getBorderBackgroundPaint())); } if (getBorderPaint() != null) { chart.setBorderPaint(findColor(getBorderPaint())); } if (getBorderStroke() != null) { chart.setBorderStroke(findStroke(getBorderStroke())); } chart.setBorderVisible(getBorderVisible()); configurePlot(chart.getPlot()); try { UIDocument doc = (UIDocument) findITextParent(getParent(), UIDocument.class); if (doc != null) { PdfWriter writer = (PdfWriter) doc.getWriter(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(getWidth(), getHeight()); UIFont font = (UIFont) findITextParent(this, UIFont.class); DefaultFontMapper fontMapper; if (font == null) { fontMapper = new DefaultFontMapper(); } else { fontMapper = new AsianFontMapper(font.getName(), font.getEncoding()); } Graphics2D g2 = tp.createGraphics(getWidth(), getHeight(), fontMapper); chart.draw(g2, new Rectangle2D.Double(0, 0, getWidth(), getHeight())); g2.dispose(); image = new ImgTemplate(tp); } else { ByteArrayOutputStream stream = new ByteArrayOutputStream(); ChartUtilities.writeChartAsJPEG(stream, chart, getWidth(), getHeight()); imageData = stream.toByteArray(); stream.close(); } } catch (Exception e) { throw new RuntimeException(e); } }
/** * Creates a new demo. * * @param title the frame title. */ public RoundNumberGlobal(final String title, ArrayList<Double> pf) { super(title); final BoxAndWhiskerCategoryDataset dataset = createSampleDataset(pf); final CategoryAxis xAxis = new CategoryAxis(""); // final NumberAxis yAxis = new NumberAxis("Round number"); final NumberAxis yAxis = new NumberAxis(""); yAxis.setAutoRangeIncludesZero(false); final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer(); renderer.setFillBox(false); renderer.setToolTipGenerator(new BoxAndWhiskerToolTipGenerator()); final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); Font font = new Font("Dialog", Font.PLAIN, 16); xAxis.setTickLabelFont(font); yAxis.setTickLabelFont(font); yAxis.setLabelFont(font); final JFreeChart chart = new JFreeChart( "Round Number" + getTitle(pf), new Font("SansSerif", Font.BOLD, 18), plot, true); final ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new java.awt.Dimension(650, 370)); setContentPane(chartPanel); TextTitle legendText = null; if (pf.size() == 1) { legendText = new TextTitle("Population Size"); } else { legendText = new TextTitle("Population Size - Probability of Failure"); } legendText.setFont(font); legendText.setPosition(RectangleEdge.BOTTOM); chart.addSubtitle(legendText); chart.getLegend().setItemFont(font); FileOutputStream output; try { output = new FileOutputStream("roundGlobalNumber" + pf + ".jpg"); ChartUtilities.writeChartAsJPEG(output, 1.0f, chart, 400, 400, null); } catch (FileNotFoundException ex) { Logger.getLogger(RoundNumberGlobal.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(RoundNumberGlobal.class.getName()).log(Level.SEVERE, null, ex); } }
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { List list = (List) ((HttpServletRequest) req).getSession().getAttribute("regionhistoryinfo"); String historydate = (String) ((HttpServletRequest) req).getSession().getAttribute("mystatdate"); res.setContentType("image/jpeg"); String title = null; if (list.size() == 0) { logger.info("list is null"); } else { title = historydate + "各地市短信发送情况显示图"; JFreeChart jfreechart = createChart(getDataSet(list), title); int width = 700; int height = 490; Font font = new Font("黑体", Font.CENTER_BASELINE, 12); ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1.0f, jfreechart, width, height, null); } }
@RequestMapping("/ajaxChartGantt.html") public String ajaxGantt( @RequestParam("sym") String param, @RequestParam("name") String name, ModelMap model, HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception { log.debug("ChartController "); List<Document> list = (List<Document>) session.getAttribute("resultsSingle"); OutputStream stream = response.getOutputStream(); response.setContentType("image/jpeg"); ChartUtilities.writeChartAsJPEG( stream, ChartUtils.createGanttChart("Gantt", "Fechas", "Tarea", list), 900, 600); stream.close(); return null; }
@RequestMapping("/ajaxChart.html") public String ajaxFill( @RequestParam("sym") String param, @RequestParam("name") String name, ModelMap model, HttpServletRequest request, HttpSession session, HttpServletResponse response) throws Exception { log.debug("ChartController "); List<Document> list = (List<Document>) session.getAttribute("results"); OutputStream stream = response.getOutputStream(); response.setContentType("image/jpeg"); ChartUtilities.writeChartAsJPEG( stream, ChartUtils.createChart(ChartUtils.sortListDocument(list, param), "BAR", name), 400, 300); stream.close(); return null; }
/* (non-Javadoc) * @see com.lily.dap.service.report2.impl.chart.ChartStrategy#outputChart(java.lang.String, int, int, java.util.Map, java.util.List, java.io.OutputStream) */ public void outputChart( String type, int height, int width, Map<String, String> paramMap, Map<String, Object> variableMap, List<Map<String, Object>> dataList, OutputStream os) throws IOException { String nameField = ChartUtils.evaluateParam(paramMap.get("namefield"), variableMap); String valueField = ChartUtils.evaluateParam(paramMap.get("valuefield"), variableMap); DefaultPieDataset dataset = createDataset(dataList, nameField, valueField); String title = ChartUtils.evaluateParam(paramMap.get("title"), variableMap); boolean legend = paramMap.get("legend") == null ? true : "true" .equals( ChartUtils.evaluateParam(paramMap.get("legend"), variableMap).toLowerCase()); JFreeChart chart; if (ChartConstants.TYPE_PIE3D.equals(type)) chart = ChartFactory.createPieChart3D( title, // 图表标题 dataset, // 数据集 legend, // 是否显示图例 false, // 是否生成工具 false // 是否生成URL链接 ); else chart = ChartFactory.createPieChart( title, // 图表标题 dataset, // 数据集 legend, // 是否显示图例 false, // 是否生成工具 false // 是否生成URL链接 ); // 设置标题字体 if (chart.getTitle() != null) chart.getTitle().setFont(titleFont); // 设置图例字体 if (chart.getLegend() != null) chart.getLegend().setItemFont(legendFont); PiePlot pie = (PiePlot) chart.getPlot(); // if (pie instanceof PiePlot3D) { // PiePlot3D pie3d = (PiePlot3D)pie; // } // 设置前景透明度 if (paramMap.get("foregroundAlpha") != null) { float f = Float.parseFloat(ChartUtils.evaluateParam(paramMap.get("foregroundAlpha"), variableMap)); pie.setForegroundAlpha(f); } // 设置标签的最大宽度 if (paramMap.get("maximumlabelwidth") != null) { float f = Float.parseFloat( ChartUtils.evaluateParam(paramMap.get("maximumlabelwidth"), variableMap)); pie.setMaximumLabelWidth(f); } // 设置标饼上标签的标签字体 pie.setLabelFont(labelFont); String labelFormat = paramMap.get("labelformat"); if (labelFormat != null) { /* * 因为labelformat参数可能包含{0}、{1}、{2}这样的字符串,所以要通过检查'{'后面跟的字符是不是数字,来判断labelformat参数是不是表达式 */ int index = labelFormat.indexOf('{'); if (index >= 0) { char ch = labelFormat.charAt(index + 1); if (!CharUtils.isAsciiNumeric(ch)) labelFormat = ChartUtils.evaluateParam(labelFormat, variableMap); } // 设置饼上标签的显示格式,其中,0:数据名称,1:数据值,2:数据占整个百分比 StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(labelFormat); pie.setLabelGenerator(generator); } String legendformat = paramMap.get("legendformat"); if (legendformat != null) { /* * 因为legendformat参数可能包含{0}、{1}、{2}这样的字符串,所以要通过检查'{'后面跟的字符是不是数字,来判断legendformat参数是不是表达式 */ int index = legendformat.indexOf('{'); if (index >= 0) { char ch = legendformat.charAt(index + 1); if (!CharUtils.isAsciiNumeric(ch)) legendformat = ChartUtils.evaluateParam(legendformat, variableMap); } // 设置饼上标签的显示格式,其中,0:数据名称,1:数据值,2:数据占整个百分比 StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(legendformat); pie.setLegendLabelGenerator(generator); } if (paramMap.get("color") != null) { int index = 0; String[] colGroups = ChartUtils.evaluateParam(paramMap.get("color"), variableMap).split(","); for (String colVal : colGroups) { Color color = ChartUtils.createColor(colVal.trim()); pie.setSectionPaint(index++, color); } } // 设置饼图是圆形还是椭圆形 if (paramMap.get("circular") != null) { boolean isCircular = "true" .equals( ChartUtils.evaluateParam(paramMap.get("circular"), variableMap).toLowerCase()); pie.setCircular(isCircular); } ChartUtilities.writeChartAsJPEG(os, 1, chart, width, height, null); }