JavaScript highcharts 传值

waitingaminute · 2014年07月25日 · 最后由 fengkuok 回复于 2014年07月25日 · 3219 次阅读

今天自学 highcharts,数据返回前台,我按 demo 上的格式给拼成 [["男",7],["女",5]] 的格式,传过去,不显示饼图,谁能帮我看看是哪里出问题了,多谢

<script type="text/javascript">
$(function () {
var rand = Math.random();
    $.ajax( {
        url : "./pietest.do",
        type : "post",
        async : false,
        data: "rand="+rand,
        dataType: "json",
        contentType : "application/x-www-form-urlencoded; charset=utf-8",
        success : function(data, textStatus) {
             //var piedata = eval(data);
             var l = data.length;
             var p = "[";
              debugger;
             for(var i=0;i<l;i++){
                    if(i==(l-1)){
                      p+="['"+data[i].sex+"', "+data[i].mrs+" ]";
                    }else{
                      p+="['"+data[i].sex+"', "+data[i].mrs+" ],";
                    }
                }
               p+="]";
            // p.prepend("[");
            // p.append("]");

            xspie(p);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert("服务器请求失败!");
            }
    });

});
 function xspie(p){      
        $('#container').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: 1,//null,
            plotShadow: false
        },
        title: {    
            text: 'Browser market shares at a specific website , 2014'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: p
        }]
    });
    } 

        </script>

不用 lazy_highcharts?

要么把用 ajax 请求回来的 data 做成符合出饼图的数据结构直接丢到 xspie 方法里面,要么把拼接好的那一串[xxxx]变成 js 的 object。直接丢个 string 过去是不行的,虽然看起来像 json 格式

拼接那有误;

#1 楼 @wcc526 五月份刚换工作,不做 ruby 了,所以这个是我学习 java 遇到的

#2 楼 @ywjno 哦,我理解你的意思了,多谢,下午试试

#3 楼 @fengkuok 额,太宽泛了……同样感谢

#7 楼 @waitingaminute [["男",7],["女",5]] 这种是数组结构啊,你拼接后是字符串;

需要 登录 后方可回复, 如果你还没有账号请 注册新账号