zoukankan      html  css  js  c++  java
  • Jfreechart初案例--饼图

    1.action

    @Controller(value = "pieAction")
    @Scope("prototype")
    public class PieAction extends ActionSupport {
        @Autowired
        private PieService pieService;
        // 图表
        private JFreeChart jFreeChart;
    
        @Override
        public String execute() throws Exception {
            // 1准备数据集
            DefaultPieDataset pieDate = new DefaultPieDataset();
            // 2查询数据
            List<Pie> list = pieService.find();
            // 3循环放入数据集
            for (Pie pie : list) {
                pieDate.setValue(pie.getName(), pie.getNum());
            }
            // 设置主题及编码(省略主题设置)
            StandardChartTheme sct = new StandardChartTheme("CN");
            ChartFactory.setChartTheme(sct);
            // 生成char
            this.jFreeChart = ChartFactory.createPieChart3D("标题-测试", pieDate);
            
            // 乱码解决
            TextTitle textTitle = jFreeChart.getTitle();
            textTitle.setFont(new Font("微软雅黑", Font.BOLD, 12));
            PiePlot plot = (PiePlot) jFreeChart.getPlot();
            // 设置饼状图体里的的各个标签字体
            plot.setLabelFont(new Font("微软雅黑", Font.BOLD, 12));
            // 设置图表下方的图例说明字体
            jFreeChart.getLegend().setItemFont(new Font("微软雅黑", Font.BOLD, 12));
    
            // 获取到要保存的路径
            String realPath = ServletActionContext.getRequest().getRealPath("img");
            // 保存图片到路径
            FileOutputStream fos = new FileOutputStream(realPath + "/test.jpg");
            ChartUtilities.writeChartAsJPEG(fos, 1, jFreeChart, 400, 300, null);
            fos.close();
            return SUCCESS;
        }
    
        public PieService getPieService() {
            return pieService;
        }
    
        public void setPieService(PieService pieService) {
            this.pieService = pieService;
        }
    
        public JFreeChart getjFreeChart() {
            return jFreeChart;
        }
    
        public void setjFreeChart(JFreeChart jFreeChart) {
            this.jFreeChart = jFreeChart;
        }
    
    }

    2.jsp页面

    <body>
        <img alt="图表" src="${pageContext.request.contextPath}/img/test.jpg">
    </body>
  • 相关阅读:
    python列表解析和正同表达式
    python实现屏幕截图
    pngCanvas 是一个使用纯Python代码的生成png图像的工具
    windows 7 提示升级到windows 10补丁
    openerp所用QWEB2的调试笔记
    修改 Ubuntu 13.04 LAMP 服务器端口号
    DEB方式在UBUNTU安装ODOO 8.0
    在openerp撰写消息中增加图片
    ubuntu挂载3T新硬盘并更换home分区
    MPEG-4 压缩编码标准
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/6144089.html
Copyright © 2011-2022 走看看