JFreeChart 制作3D饼状图分析数据

来源:互联网 发布:安心360定位软件 编辑:程序博客网 时间:2024/05/17 12:01

 代码:

CharUtil.java

package com.chart;import java.awt.Font;import java.text.NumberFormat;import org.jfree.chart.ChartFactory;import org.jfree.chart.JFreeChart;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.PiePlot;import org.jfree.data.general.DefaultPieDataset;import org.jfree.data.general.PieDataset;public class CharUtil {  public static PieDataset initDataSet(){   double []values = {300,110,160,240,220,50};   String []keys ={"数码产品","家用电器","日常用品","服装","水果蔬菜","其他"};   DefaultPieDataset dataset = new DefaultPieDataset();   for (int i = 0; i < values.length; i++) {     dataset.setValue(keys[i], values[i]);   }   System.out.println("数据集:"+dataset);   return dataset;   }    public static JFreeChart createChart(){  //创建3D饼图  JFreeChart chart = ChartFactory.createPieChart3D("海哥商城月销量统计", initDataSet(), true, true, false);  chart.getTitle().setFont(new Font("隶书", Font.BOLD, 15));  //设置标题字体  PiePlot plot = (PiePlot) chart.getPlot();  plot.setForegroundAlpha(0.5f);  //设置前景透明度  plot.setLabelFont(new Font("宋体", Font.PLAIN, 12));  plot.setCircular(true);  //设置分类标签的格式  plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()));  return chart;  }}

ChartUtil.jsp

<%@page import="com.chart.CharUtil"%><%@page import="org.jfree.chart.servlet.ServletUtilities"%><%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JFreeChart显示饼状图</title></head><body><%   String fileName = ServletUtilities.saveChartAsJPEG(CharUtil.createChart(), 550, 380, session);  //String graphURL = request.getContextPath()+"/DisplayChart?fileName="+fileName;%><div align="center">   <img alt="商城月销量统计" src="DisplayChart?filename=<%=fileName %>" border="1"></div></body></html>

截图:


原创粉丝点击