• 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>
  • 相关阅读:
    视频直播架构
    day1 python 入门
    python 多用户登录
    mysql innobackup 备份脚本
    ADT离线安装
    真机调试adb:wait for device 解决方案
    php中的魔术方法
    整理资料
    PostgreSQL表空间_数据库_模式_表_用户角色之间的关系[转]
    PHP获取文件夹的大小
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/6144089.html
Copyright © 2020-2023  润新知