java 生成报表

package com.scinvest.web.controller.statsys;


import java.awt.Font;
import java.awt.RenderingHints;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.TimeSeries;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.scinvest.web.controller.BaseController;
@Controller
@RequestMapping("/statsys/westlestatsys")
public class WestleStatsysController extends BaseController{
/**
 * 查询
 * @param model
 * @return
 */
@RequestMapping(value="",method= RequestMethod.GET)
   @RequiresPermissions("westle:view")
   public String view(Model model){
model.addAttribute("idprefix","westle_sts_view");
return "statsys/westle_sts_view";
}
@RequestMapping(value="view",method= RequestMethod.GET)
public void jfreechart(Model model,HttpServletResponse response,String viewmethod) throws IOException{ 
response.setContentType("image/jpeg");

JFreeChart chart = null;  
//设置标题字体
//创建主题样式  
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");  
//设置标题字体  
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));  
//设置图例的字体 
standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));  
//设置轴向的字体  
standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15));  
//应用主题样式  
ChartFactory.setChartTheme(standardChartTheme); 


if(viewmethod.equals("3") || viewmethod.equals("")){
//定义数据
DefaultPieDataset pd=new DefaultPieDataset();
pd.setValue("大专",100);
pd.setValue("本科",50);
pd.setValue("硕士",10);
pd.setValue("博士",2);
//生成好报表
chart=ChartFactory.createPieChart("公司雇员学历统计表",pd,true,true,false);
}else if(viewmethod.equals("4")){
TimeSeries java=new TimeSeries("java编程",Month.class);
java.add(new Month(8,2006),3000);
java.add(new Month(9,2006),3500);
java.add(new Month(10,2006),5000);
java.add(new Month(11,2006),4000);

TimeSeries net=new TimeSeries("net编程",Month.class);
net.add(new Month(8,2006),4000);
net.add(new Month(9,2006),3500);
net.add(new Month(10,2006),3000);
net.add(new Month(11,2006),4000);
TimeSeriesCollection dataset=new TimeSeriesCollection();
dataset.addSeries(java);
dataset.addSeries(net);
chart=ChartFactory.createTimeSeriesChart("编程语言排行","语言","数量",dataset, true,false,false);
}else if(viewmethod.equals("1") || viewmethod.equals("2")){
DefaultCategoryDataset pd=new DefaultCategoryDataset();
pd.addValue(100,"bb","大专");
pd.addValue(50,"cc","大专");
pd.addValue(100,"dd","大专");
pd.addValue(50,"ee","大专");
pd.addValue(10,"bb","硕士");
pd.addValue(2,"cc","硕士");
pd.addValue(10,"dd","硕士");
pd.addValue(2,"ee","硕士");
if(viewmethod.equals("1")){
chart=ChartFactory.createBarChart("公司雇员学历统计表",//2D
"学历",
"人数",
pd,PlotOrientation.VERTICAL,
false,false,false);
}else{
chart=ChartFactory.createBarChart3D("公司雇员学历统计表",//3D
"学历",
"人数",
pd,PlotOrientation.VERTICAL,
false,false,false);
}
CategoryPlot polt=(CategoryPlot)chart.getPlot();
//得到横轴
CategoryAxis heng=(CategoryAxis)polt.getDomainAxis();
//得到纵轴
NumberAxis number=(NumberAxis)polt.getRangeAxis();
}

//设置字体的清晰度
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
//输出数据
ChartUtilities.writeChartAsJPEG(response.getOutputStream(),chart,400,300);

}
}
<div>
<select name="vtype" id="sel" οnchange="fun()">
    <option value="1" >2D柱线图</option>
    <option value="2" >3D柱线图</option>
    <option value="3" selected>饼图</option>
    <option value="4" >折现图</option>
    </select>
</div>
<div>
<img src="${rootPath}/statsys/westlestatsys/view?viewmethod" id="captcha_img" width=500 height=300 border=0 usemap="#${filename}">
</div>



HTML:
<script type="text/javascript">
function fun(){
var method=$('#sel').val();
$('#captcha_img').attr('src','${rootPath}/statsys/westlestatsys/view?viewmethod='+method);
}
</script>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值