将变量传递给Data for Jquery Plot

时间:2014-07-05 10:20:26

标签: javascript jquery plot

我有一个Jquery.Flot.Pie图表,当我直接将数字传递给Data时,它可以工作(就像这样),并且只有一个切片。

 <script type="text/javascript">
$(document).ready(function () {


    var ord = $("#MyUserInfoBoxControl_txtOrd").val();        
    var lav = $("#MyUserInfoBoxControl_txtLav").val();
    var pro = $("#MyUserInfoBoxControl_txtPro").val();
    var con = $("#MyUserInfoBoxControl_txtCon").val();
    var ins = $("#MyUserInfoBoxControl_txtIns").val();

    alert(ord + "," + lav + "," + pro + "," + con + "," + ins);

    var data = [
        { label: "INS", data: 0, color: "#FFFFFF" },
        { label: "LAV", data: 0, color: "#4572A7" },
        { label: "PRO", data: 0, color: "#80699B" },
        { label: "CON", data: 0, color: "#AA4643" },
        { label: "ORD", data: 100, color: "#3D96AE"}
    ];

    alert(data[0].toString())

    $.plot($("#Grafico"), data, {
        series: {
            pie: {
                show: true,
                threshold: 0,
                tilt: 1

            }
        },
        legend: {
            show: false
        }
    });
});

相反,当我尝试将变量传递给数据数组时,它不再起作用了(警报没关系,所以数据是正确的)我也试过使用parseFloat()但没有做任何事情:

 <script type="text/javascript">
$(document).ready(function () {


    var ord = $("#MyUserInfoBoxControl_txtOrd").val();        
    var lav = $("#MyUserInfoBoxControl_txtLav").val();
    var pro = $("#MyUserInfoBoxControl_txtPro").val();
    var con = $("#MyUserInfoBoxControl_txtCon").val();
    var ins = $("#MyUserInfoBoxControl_txtIns").val();

    alert(ord + "," + lav + "," + pro + "," + con + "," + ins);

    var data = [
        { label: "INS", data: ins, color: "#FFFFFF" },
        { label: "LAV", data: lav, color: "#4572A7" },
        { label: "PRO", data: pro, color: "#80699B" },
        { label: "CON", data: con, color: "#AA4643" },
        { label: "ORD", data: ord, color: "#3D96AE"}
    ];

    alert(data[0].toString())

    $.plot($("#Grafico"), data, {
        series: {
            pie: {
                show: true,
                threshold: 0,
                tilt: 1

            }
        },
        legend: {
            show: false
        }
    });
});

有人能帮帮我吗?谢谢!

2 个答案:

答案 0 :(得分:1)

由于.val()方法将值返回为string,您需要将它们转换为integer

var ord = +$("#MyUserInfoBoxControl_txtOrd").val();        
var lav = +$("#MyUserInfoBoxControl_txtLav").val();
var pro = +$("#MyUserInfoBoxControl_txtPro").val();
var con = +$("#MyUserInfoBoxControl_txtCon").val();
var ins = +$("#MyUserInfoBoxControl_txtIns").val();

前缀+以将其转换为integer或代替前缀+,您也可以使用parseInt()方法。

答案 1 :(得分:1)

使用如下

{ label: "INS", data: parseInt(ins), color: "#FFFFFF" },

遵循相同的其他变量

DEMO