`

opensource for struts2-jfreechart-plugin-2.1.8.1

阅读更多

I wish we all could share our knowledge each other,especially the open project

 

*Brief introduction::extend the function of jfreechart-plugin to display the tips' info you've set in the web browser.
 *    And fix the bug of jpg undisplayed.(just use response.setContentType("image/jpeg") instead of response.setContentType("image/jpg"))
 *

 

About struts2,we know it use the mechanism of plugin to hook others' third part. However we'll find some blemish or bugs with going deep into using them,for example,one is struts2-jasperreport-plugin-2.1.6 introduced by us in my blog's article http://redsnow-fenglin.iteye.com/blog/464213 and http://redsnow-fenglin.iteye.com/blog/461927, and the other one is the one which we're discussing now the struts2-jfreechart-plugin-2.1.8.1,whoe bug is that could not display the tips' info you've set in the web browser and the jpg's format.

 

Here, I just introduce the later instance. I extend the class ChartResult to extend the function of jfreechart-plugin to display the tips' info you've set in the web browser.And fix the bug of jpg undisplayed.(just use response.setContentType("image/jpeg") instead of response.setContentType("image/jpg")). There is an used example in the source coe of EnhancedChartResult.java, smple codes as following:

 

 <!-- START SNIPPET: example -->

public class ExampleChartAction extends ActionSupport {

private JFreeChart chart;

public String execute() throws Exception {
        String title = "Report Title Here";
        String xAxisLabel = "X Axis Mark";
        String yAxisLabel = "Y Axis Mark";

        double[][] yKeysVale = new double[][] {{621,890,591,591,528,621}};
        String[] yKeys = {"Error Files"};
        String[] xKeys = {"2009-01","2009-02","2009-03","2009-04","2009-05","2009-06"};
        CategoryDataset dataset = DatasetUtilities.createCategoryDataset(yKeys,xKeys,yKeysVale);
        chart = ChartFactory.createLineChart(title, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, true, true, false);
        chart.setBackgroundPaint(Color.WHITE);
        CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
        // x轴 // 分类轴网格是否可见
        categoryplot.setDomainGridlinesVisible(true);
        // y轴 //数据轴网格是否可见
        categoryplot.setRangeGridlinesVisible(true);
        categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩
        categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩
        categoryplot.setBackgroundPaint(Color.lightGray);
        // 设置轴和面板之间的距离
        categoryplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D));
        // 获得renderer 注意这里是下转型到lineandshaperenderer!!
        LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
        lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
        lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见
  lineandshaperenderer.setBaseItemURLGenerator(new StandardCategoryURLGenerator("link.jsp","series","hits")); // 设置超链接
        // 显示折点数据
         lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
         lineandshaperenderer.setBaseItemLabelsVisible(true);
         

  // 横轴上的 Lable 45度倾斜
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        numberaxis.setAutoRangeIncludesZero(false);     

            return SUCCESS;
     }

      // this method will get called if we specify <param name="value">chart</param>
     public JFreeChart getChart() {
            return chart;
     }
  }

 

<result name="success" type="chart">
   <param name="mode">enchanced</param>
   <param name="value">chart</param>
   <param name="type">png</param>
   <param name="width">640</param>
   <param name="height">480</param>
</result>

 
  Tips: enchanced is the default value of the param mode , and another value you can use is stand,which stands for the original value of struts-plugin-2.1.8.jar.
<!-- END SNIPPET: example -->

 

If you want to know the more details , you could download the source code from the appurtenance.

 

Tips:My apache topic is http://www.quicktopic.com/43/H/23NsPYee3Y4Eg.

 

分享到:
评论
2 楼 redsnow_fenglin 2009-11-04  
fejay 写道
英文的,看不懂啊

不好意思,因为原本要把他发到apache网站,所以,写了英文;
本文的大致意思是:
希望大家分享自己改良的开源代码,因为第三方插件并不是完美无瑕的;
本文说的是官方struts2-jfreechart-plugin-2.1.8.jar存在不足:1、仅仅只是展示jfreechart报表,不能呈现鼠标提示及超链接,即丢失jfreechart中我们设置的urltips及tooltips;2、jfreechart默认的是png格式,当我们配成jpg或jpeg格式时,web页面就无法展示图片而变成你的action下载提示了;
本文给出的源码及jar就是解决上面两个问题的,其中的example是展示如何使用我给出的struts2-jfreechart-plugin-2.1.8.1.jar的使用方法,当然,我们还可以从中学习到jfreechart折线图的一种产生方法及是否展示节点、节点数据、如何设置tooltips和urltips等jfreechart的知识;我们需要注意的是<param name="mode">enchanced</param>这个配置,参数mode若配为stand即采用官方标准模式(当然是解决了bug 2),若配为enchanced即采用我们给出的加强模式(解决了bug 1和2),这个也是默认模式;

同时也提及了在我的blog中所指出的关于struts2-jasperreport-plugin-2.1.6.jar的不足及改良,详细信息可以参考  http://redsnow-fenglin.iteye.com/blog/464213 and http://redsnow-fenglin.iteye.com/blog/461927,关于源码及jar可以访问 http://redsnow-fenglin.iteye.com/blog/508715
1 楼 fejay 2009-11-02  
英文的,看不懂啊

相关推荐

Global site tag (gtag.js) - Google Analytics