2 qq 22591781 qq_22591781 于 2015.07.09 15:42 提问

ajax 返回的参数为什么在highcharts中不能被调用
 var smoney;
    $(function () {
          var nf;
          $("#selectYear").change(function(){
              nf= $('#selectYear option:selected') .val();
              $.ajax({
                     //要用post方式     
                   type: "Post",
                   //方法所在页面和方法名     
                   url: '<%=request.getContextPath() %>/servlet/InfoCountServlet?year='+nf,
                   async:false,
                  /*  contentType: "application/json; charset=utf-8",*/
                   dataType: "text", 
                   success: function (data) {
                       smoney = data;
                       tj(smoney);
                   }
                });
            });
      });

  function tj(smoney){
      alert(smoney);
        $('#container').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: '进销信息统计直方图'
            },
            subtitle: {
                text: '统计年份: 2015年'
            }, 
            xAxis: {
                categories: [
                    '一月',
                    '二月',
                    '三月',
                    '四月',
                    '五月',
                    '六月',
                    '七月',
                    '八月',
                    '九月',
                    '十月',
                    '十一月',
                    '十二月'
                ]
            },
            yAxis: {
                min: 0,
                title: {
                    text: '金额 (万元)'
                }
            },
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                    '<td style="padding:0"><b>{point.y:.1f} 万元</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: [{
                name: '进货额',
                data:[smoney]

            }, {
                name: '销售额',
                data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]

            }, {
                name: '生活开支',
                data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]

            }, {
                name: '利润',
                data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]

            }]
        });
  }

下面是servlet:

 public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String year = request.getParameter("year");
        List<CountJavaBean> sellerList = countJavaBeanService.getSellerCountByMonths(year);
        if(Validator.isNotEmpty(sellerList)){
            Map<Integer,Double> map=new HashMap<Integer,Double>(); 
            for(int j=0;j<12;j++){
                map.put(j, 0.00);
                for(int i=0;i<sellerList.size();i++){
                    CountJavaBean bean = sellerList.get(i);
                    if(bean.getMonths()-1==j){
                        map.put(j, bean.getMoney());
                    }
                }
            }
            Collection<Double> c = map.values();
            Iterator it = c.iterator();
            StringBuffer moneys = new StringBuffer();
            for(;it.hasNext();){
                Double money = (Double) it.next();
                moneys.append(money+",");
            }
            moneys.deleteCharAt(moneys.length()-1);
            System.out.println("------------"+moneys.toString());
            response.getWriter().print(moneys.toString());
        }
    }

小弟初来咋到,请各位大能帮忙看下,是什么问题,纠结了N天了,感激不尽

1个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.07.09 15:48
 series: [{
name: '进货额',
data:[smoney]//这么写就是一个字符串的数组,数组长度是1也就是数组内容是"1,2,3"这么样子的,而不是你想象的data[0]是1,data[1]是2
改成
series: [{
name: '进货额',
data:smoney.split(",")
Csdn user default icon
上传中...
上传图片
插入图片