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>
  • 相关阅读:
    【OCP12c】CUUG最新考试原题整理及答案(07110)
    OCP 12c最新考试原题及答案(0718)
    【OCP12c】CUUG最新考试原题整理及答案(0719)
    OCP 12c最新考试原题及答案(0714)
    《PHP与MySQL程序设计》面向对象的PHP
    Linux常用命令之文件管理
    《锋利的jQuery》之jQuery简介
    《锋利的jQuery》之jQuery与Ajax
    Google AppEngine上部署PHP应用
    《PHP与MySQL程序设计》第三章 PHP基础
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/6144089.html
Copyright © 2011-2022 走看看