本文主要是介绍使用JFreeChar生成柱状图,不做任何注释,只求应用,生成的柱状图输出到网页中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 使用JFreeChar生成柱状图,不做任何注释,只求应用,生成的柱状图输出到网页中* * 1.加入jcommon-1.0.5.jar和jfreechart-1.0.2.jar* * 2.设置值* dataset.addValue(100, "", "苹果");* dataset.addValue(200, "", "梨子");* dataset.addValue(300, "", "葡萄");* dataset.addValue(400, "", "香蕉");* dataset.addValue(500, "", "荔枝");* * * JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", // 图表标题"水果", // 目录轴的显示标签"产量", // 数值轴的显示标签dataset, // 数据集PlotOrientation.VERTICAL, true,false, false);** 在浏览器地址栏中直接访问servlet*/
package com.bjpowernode.drp.statreport.web;import java.io.IOException;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.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;@SuppressWarnings("serial")
public class BarChartServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("image/jpeg");CategoryDataset dataset = getDataSet();JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", // 图表标题"水果", // 目录轴的显示标签"产量", // 数值轴的显示标签dataset, // 数据集PlotOrientation.VERTICAL, true,false, false);CategoryPlot plot = (CategoryPlot)chart.getPlot(); StackedBarRenderer3D renderer=new StackedBarRenderer3D();renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setItemLabelsVisible(true);plot.setRenderer(renderer);ChartUtilities.writeChartAsJPEG(resp.getOutputStream(),1.0f, chart,400,300, null);}private CategoryDataset getDataSet() {DefaultCategoryDataset dataset = new DefaultCategoryDataset();dataset.addValue(100, "", "苹果");dataset.addValue(200, "", "梨子");dataset.addValue(300, "", "葡萄");dataset.addValue(400, "", "香蕉");dataset.addValue(500, "", "荔枝");return dataset;}}
这篇关于使用JFreeChar生成柱状图,不做任何注释,只求应用,生成的柱状图输出到网页中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!