浏览 3474 次
锁定老帖子 主题:JfreeChart 3D饼图总结
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-08
最后修改:2009-12-09
今天抽空,也总结了一下3D饼图的画法,这里大致先写个思路。 1、取得数据集所需数据源。3D饼图的数据集通常用DefaultPieDataset,下面将我的代码粘一下: private static PieDataset createDataset(double[] datas, String foodfect, String bmr, String daily, String sport) { // String foodfect = "食物热效应"; // String bmr = "基础代谢率"; // String daily = "日常活动消耗的能量"; // String sport = "额外运动消耗"; double fectper = datas[3] / datas[1] * 100; double bmrper = datas[2] / datas[1] * 100; double dailper = datas[4] / datas[1] * 100; double sportper = datas[7] / datas[1] * 100; if (fectper >= 0 && bmrper >= 0 && dailper >= 0 && sportper >= 0) { fectper = Double.parseDouble(BarChartData .formAtCaloriepercent(fectper)); bmrper = Double.parseDouble(BarChartData .formAtCaloriepercent(bmrper)); dailper = Double.parseDouble(BarChartData .formAtCaloriepercent(dailper)); sportper = Double.parseDouble(BarChartData .formAtCaloriepercent(sportper)); } DefaultPieDataset pset = new DefaultPieDataset(); pset.setValue(foodfect, -fectper); pset.setValue(bmr, -bmrper); pset.setValue(daily, -dailper); pset.setValue(sport, -sportper); return pset; }
2、调用画图API /** * 饼状图 * @param dataset * 数据集 * @param chartTitle * 图标题 * @param charName * 生成图的名字 * @param pieKeys * 分饼的名字集 * @return */ public static JFreeChart createValidityComparePimChar(PieDataset dataset, String chartTitle, String nodatamess) { JFreeChart chart = ChartFactory.createPieChart3D("Pie Chart 3D Demo 3", dataset, true, true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setStartAngle(290D); plot.setDirection(Rotation.CLOCKWISE); plot.setForegroundAlpha(0.5F); plot.setNoDataMessage("No data to display"); // 使下说明标签字体清晰,去锯齿类似于的效果 chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // chart.setAntiAlias(false); // 图片背景色 chart.setBackgroundPaint(Color.WHITE); // 设置图标题的字体重新设置title // 设置标题的颜色 TextTitle text = new TextTitle(chartTitle); text.setPaint(new Color(102, 102, 102)); chart.setTitle(text); plot.setBackgroundPaint(new Color(255, 253, 246)); plot.setOutlineStroke(new BasicStroke(1.5f)); plot.setMaximumLabelWidth(0.25d); // 图片中显示百分比:默认方式 // 设置各色块的颜色 plot.setSectionPaint(0, new Color(87, 149, 117)); plot.setSectionPaint(1, new Color(234, 162, 40)); plot.setSectionPaint(2, new Color(197, 180, 127)); plot.setSectionPaint(3, new Color(75, 178, 197)); plot.setSectionPaint(4, new Color(131, 149, 87)); plot.setShadowXOffset(1.0d); plot.setIgnoreZeroValues(false); plot.setStartAngle(90); // 设置旋转角度 plot.setDirection(Rotation.CLOCKWISE); // 设置旋转方向 plot.setForegroundAlpha(0.5f); plot.setLabelLinkMargin(0.1); plot.setSectionOutlinesVisible(false); plot.setDepthFactor(0.1d);// 饼图的Z轴高度。 // 设置无数据时的信息 plot.setNoDataMessage(nodatamess); plot.setNoDataMessageFont(new Font("", Font.BOLD, 14)); // 设置无数据时的信息显示颜色 plot.setNoDataMessagePaint(new Color(87, 149, 117)); // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位 plot.setLabelGenerator(new StandardPieSectionLabelGenerator( "{0} ={1}%", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例 plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( "{0} ={1}%")); plot.setLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 12)); // 指定图片的透明度(0.0-1.0) plot.setForegroundAlpha(0.65f); return chart; }
3、生成Chart图,返回图片URL public static void drawPieChart(IrisIoInterface io, Log log, double[] datas, String nodatamess, String foodfect, String bmr, String daily, String sport) { PieDataset dataset = createDataset(datas, foodfect, bmr, daily, sport); JFreeChart chart = createValidityComparePimChar(dataset, "", nodatamess); HttpServletRequest request = io.getRequest(); String filename = ""; String graphURL = ""; try { filename = ServletUtilities.saveChartAsPNG(chart, 320, 177, null, io.getSession()); graphURL = request.getContextPath() + "/displayChart?filename=" + filename; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); log.error(e); } io.setData("filename1", filename, BeanShare.BEAN_SHARE_REQUEST); io.setData("pieurl", graphURL, BeanShare.BEAN_SHARE_REQUEST); }
现在就OK了,效果图:
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |