zoukankan      html  css  js  c++  java
  • Struts2整合JFreeChar

    1,导入struts2开发的jar包和struts2-jfreechart-plugin-2.3.16.3.jar

    2,web.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>Struts2JFreeChart</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <filter>
           <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      
      <filter-mapping>
              <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
              <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>

    3,edu.hhxy.chart.bar.BarCharAction.java(Action)

    package edu.hhxy.chart.bar;
    
    import java.awt.Color;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.labels.ItemLabelAnchor;
    import org.jfree.chart.labels.ItemLabelPosition;
    import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.BarRenderer3D;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.general.DatasetUtilities;
    import org.jfree.ui.TextAnchor;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class BarCharAction extends ActionSupport {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        private JFreeChart chart;
    
        public JFreeChart getChart() {
            return chart;
        }
    
        @Override
        public String execute() throws Exception {
            double[][] data = new double[][] { { 1320, 1110, 1123, 321 },
                    { 720, 210, 1423, 1321 }, { 830, 1310, 123, 521 },
                    { 400, 1110, 623, 321 } };
            String[] rowKeys = { "苹果", "香蕉", "橘子", "梨子" };
            String[] columnKeys = { "深圳", "北京", "上海", "南京" };
            CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
                    rowKeys, columnKeys, data);
            chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset,
                    PlotOrientation.VERTICAL, true, true, true);
    
            CategoryPlot plot = chart.getCategoryPlot();
            // 设置网格背景颜色
            plot.setBackgroundPaint(Color.white);
            // 设置网格竖线颜色
            plot.setDomainGridlinePaint(Color.pink);
            // 设置网格横线颜色
            plot.setRangeGridlinePaint(Color.pink);
    
            // 显示每个柱的数值,并修改该数值的字体属性
            BarRenderer3D renderer = new BarRenderer3D();
            renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
            renderer.setBaseItemLabelsVisible(true);
    
            renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                    ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
            renderer.setItemLabelAnchorOffset(10D);
    
            // 设置平行柱的之间距离
            renderer.setItemMargin(0.4);
    
            plot.setRenderer(renderer);
            return SUCCESS;
        }
    }

    4,struts.xml中配置Action

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
        
        <package name="jfreechart" extends="jfreechart-default">
            <action name="barChart" class="edu.hhxy.chart.bar.BarCharAction">
                <result name="success" type="chart">
                   <param name="value">chart</param>
                   <param name="type">png</param>
                   <param name="width">700</param>
                   <param name="height">500</param>
                 </result>
            </action>
        </package>
        
    </struts>

    5,index.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="ISO-8859-1"%>
    <!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>Insert title here</title>
    </head>
    <body>
        <img src="barChart" />
    </body>
    </html>

    Result:

  • 相关阅读:
    html表单的创建
    mysql数据库连接标准操作
    关于Apache+MySQL+PHP下载及配置注意事项
    两个范例
    Stack类
    Collections类集
    key可以重复的map集合:IdentityHashMap
    foreach对集合的输出作用
    ListIterator接口
    【官方方法】xcode7免证书真机调试
  • 原文地址:https://www.cnblogs.com/jianfengyun/p/3813864.html
Copyright © 2011-2022 走看看