zoukankan      html  css  js  c++  java
  • [六]JFreeChart实践五之与Struts2整合

    1.Action,返回Chart

    package com.java1234.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;
    }

    }

    2.配置返回的chart

    <?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="com.java1234.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>

    3.配置struts2拦截器

    <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>

  • 相关阅读:
    compose 函数实现
    垂直居中的实现方式
    数组去重方法总结
    前端性能优化
    简简单单的几个正则表达式
    ES6
    Vue不兼容IE8原因以及Object.defineProperty详解
    Vuex(二)——关于store
    Vuex(一)——vuejs的状态管理模式
    关于REST的浅显了解
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/5081088.html
Copyright © 2011-2022 走看看