本文主要是介绍使用JFreeChart生成饼图,不做任何注释,只求应用,生成的饼图输出到网页中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/**
* 使用JFreeChar生成饼图,不做任何注释,只求应用,生成的饼图输出到网页中
*
* 1.加入jcommon-1.0.5.jar和jfreechart-1.0.2.jar
*
* 2.设置值
* dataset.setValue("苹果", 100);
* dataset.setValue("梨子", 200);
* dataset.setValue("葡萄", 300);
* dataset.setValue("香蕉", 400);
* dataset.setValue("荔枝", 500);
*
* 在浏览器地址栏中直接访问servlet
*/
package com.bjpowernode.drp.statreport.web;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
@SuppressWarnings("serial")
public class ChartMemoServlet extends HttpServlet {
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType("image/jpeg");
DefaultPieDataset data = getDataSet();
JFreeChart chart = ChartFactory.createPieChart3D("水果产量图",
data,
true,
false,
false);
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})"));
ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1.0f, chart,
400, 300, null);
}
private static DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("苹果", 100);
dataset.setValue("梨子", 200);
dataset.setValue("葡萄", 300);
dataset.setValue("香蕉", 400);
dataset.setValue("荔枝", 500);
return dataset;
}
}
这篇关于使用JFreeChart生成饼图,不做任何注释,只求应用,生成的饼图输出到网页中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!