14.dwr 图片数据的传递和柱状图片生成与显示

只是重要知识点的一些代码记录,解释有点少

 

1.页面代码:

<img border="1" height="400" width="50%" id="imgObj">

<script type="text/javascript">

var aa;

       var ab;

        function getData(){

        aa = new Array();

        ab = new Array();

        for(i=0;i<10;i++){

           aa[i]=i;

           ab[i]=i+1;

           }

        getImgURL();

       }

      

       function getImgURL(){

           service.getChartURLID(aa,ab,changeURL);//dwr服务

        }

   

       //改变当前图片对象的路径

       function changeURL(url){

           $("imgObj").src = url;

           setTimeout("getImgURL()",100000);

       }

</script>

 

 

2.后台代码:

   Dwr配置:

<allow>

     <create creator="new" javascript="service">

         <param name="class" value="com.nantian.service.Service" />

     </create>

     </allow>

  web.xml配置

<!--图表图片 -->

<servlet>

<servlet-name>DisplayChart</servlet-name>

<servlet-class>

org.jfree.chart.servlet.DisplayChart

</servlet-class>

</servlet>

 <servlet-mapping>

<servlet-name>DisplayChart</servlet-name>

<url-pattern>/DisplayChart/*</url-pattern>

 </servlet-mapping>

 

    Service服务:

public String getChartURLID(int[] aa,int[] ab,HttpServletRequest request)

    throws IOException {

              String[] aas = new  String[aa.length];

              for (int i=0;i<aa.length;i++) {

                  aas[i]=aa[i]+"";

              }

               JFreeChart chart = createBarChart(aas,ab,"月份");

String filename = ServletUtilities.saveChartAsPNG(chart, 400, 300,request.getSession());

              // 调用DisplayChart(配置在web.xml中),生成图形

              String graphURL = request.getContextPath() + "/DisplayChart?filename="+ filename;

              return graphURL;

           }

 

//柱形图生成

public JFreeChart createBarChart(String []a,int[] b,String type){

       DefaultCategoryDataset dataset = new DefaultCategoryDataset();

       //添加数据

       for(int i=0;i<a.length;i++){

           dataset.addValue(b[i],"用电量",a[i]);

       }

       JFreeChart  chart = ChartFactory.createBarChart3D("用电统计图", type,"用电量", dataset, PlotOrientation.VERTICAL, true, false, false);

       // 设置总的背景颜色

         chart.setBackgroundPaint(ChartColor.green);

           // 设置标题颜色

         chart.getTitle().setPaint(ChartColor.black);

         // 获得图表对象

         CategoryPlot p =chart.getCategoryPlot();

         // 没有数据时显示

         p.setNoDataMessage("无对应的数据,请重新查询。");

         // 设置图的背景颜色

         p.setBackgroundPaint(ChartColor.yellow);

         // 设置表格线颜色

         p.setRangeGridlinePaint(ChartColor.red);

        

         BarRenderer3D   renderer   =   new   BarRenderer3D();

            //显示每个柱的数值,并修改该数值的字体属性

           renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());

            renderer.setItemLabelFont(new  Font( "黑体 ",Font.PLAIN,12));

            renderer.setItemLabelsVisible(true);

            p.setRenderer(renderer);

       return chart;

       }

你可能感兴趣的:(DWR,jfreechart)