浏览 4130 次
锁定老帖子 主题:jfreechart初步
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-09-09
package com.xmddl.jfreechart; import java.awt.Color; import java.awt.Font; import java.io.IOException; import java.sql.Connection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.AxisLocation; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.chart.renderer.category.StackedBarRenderer3D; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.jdbc.JDBCXYDataset; import com.xmddl.common.DBService; public class JFreeChartServlet1 extends HttpServlet { static Connection con = DBService.getInstance().getConnection(); /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ public JFreeChartServlet1() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); DefaultCategoryDataset data = getDataSet(); JFreeChart chart = ChartFactory.createStackedBarChart("水果销量图统计", null, null, data, PlotOrientation.VERTICAL, true, false, false); chart.setBackgroundPaint(Color.WHITE); CategoryPlot plot = chart.getCategoryPlot(); CategoryAxis domainAxis = plot.getDomainAxis(); // domainAxis.setVerticalCategoryLabels(false); plot.setDomainAxis(domainAxis); ValueAxis rangeAxis = plot.getRangeAxis(); // 设置最高的一个 Item 与图片顶端的距离 rangeAxis.setUpperMargin(0.15); // 设置最低的一个 Item 与图片底端的距离 rangeAxis.setLowerMargin(0.15); plot.setRangeAxis(rangeAxis); // BarRenderer3D renderer = new BarRenderer3D(); StackedBarRenderer3D renderer = new StackedBarRenderer3D(); renderer.setBaseOutlinePaint(Color.BLACK); // 设置 Wall 的颜色 renderer.setWallPaint(Color.gray); // 设置每种水果代表的柱的颜色 renderer.setSeriesPaint(0, new Color(0, 0, 255)); renderer.setSeriesPaint(1, new Color(0, 100, 255)); renderer.setSeriesPaint(2, Color.GREEN); // 设置每种水果代表的柱的 Outline 颜色 renderer.setSeriesOutlinePaint(0, Color.BLACK); renderer.setSeriesOutlinePaint(1, Color.BLACK); renderer.setSeriesOutlinePaint(2, Color.BLACK); // 设置每个地区所包含的平行柱的之间距离 renderer.setItemMargin(0.1); // 显示每个柱的数值,并修改该数值的字体属性 renderer .setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setItemLabelFont(new Font("黑体", Font.PLAIN, 12)); renderer.setItemLabelsVisible(true); renderer.setMaximumBarWidth(0.08D); plot.setRenderer(renderer); // 设置柱的透明度<BR> plot.setForegroundAlpha(0.6f); // 设置地区、销量的显示位置<BR> plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT); plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 500, chart, 600, 400, null); } /** * 获取一个演示用的简单数据集对象 * * @return */ private static DefaultCategoryDataset getDataSet() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(100, "北京", "苹果"); dataset.addValue(100, "上海", "苹果"); dataset.addValue(100, "广州", "苹果"); dataset.addValue(200, "北京", "梨子"); dataset.addValue(200, "上海", "梨子"); dataset.addValue(200, "广州", "梨子"); dataset.addValue(300, "北京", "葡萄"); dataset.addValue(300, "上海", "葡萄"); dataset.addValue(300, "广州", "葡萄"); dataset.addValue(400, "北京", "香蕉"); dataset.addValue(400, "上海", "香蕉"); dataset.addValue(400, "广州", "香蕉"); dataset.addValue(500, "北京", "荔枝"); dataset.addValue(500, "上海", "荔枝"); dataset.addValue(500, "广州", "荔枝"); return dataset; // dataset.setValue("苹果",100); // dataset.setValue("梨子", 200); // dataset.setValue("葡萄", 300); // dataset.setValue("香蕉", 200); // dataset.setValue("荔枝", 200); // return dataset; } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occure */ public void init() throws ServletException { // Put your code here } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |