将json值从后面的.net代码传递给highcharts

时间:2013-07-11 09:02:23

标签: c# .net highcharts

var Javascriptxvalue= $.parseJSON($("#hdnXaxis").val());
var Javascriptyvalue= $.parseJSON($("#hdnYaxis").val());  

$(document).ready(DrawMyGraph1);
      function DrawMyGraph1() {

       chart = new Highcharts.Chart(
       {
        chart: {
            type: 'column',
            renderTo: 'container3',
            defaultSeriesType: 'area'

        },
        title: {
            text: ''
        },
        subtitle: {
            text: ''
        },
        xAxis: {
            categories: Javascriptxvalue,
            labels: {
                enabled: false
            }
        },
        yAxis: {
            title: {
                text: 'No of Patients'
            }
        },

        credits: {
            enabled: false
        },
        tooltip: {
            formatter: function () {
                return this.series.name + ' - ' + Highcharts.numberFormat(this.y, 0);
            }
        },
        series: Javascriptyvalue
    });

}

c#代码  void FastMovingStocksBarChart(string date1,string date2,string selperiod,string sql)     {         DataSet dschart = new DataSet();         dschart = _obj_MIS.DoctorpatientreportChart(date1,date2,selperiod,sql);         List lstXaxis = new List();         List lstcolors = new List();

    lstcolors.Add("#3366DD");
    //lstcolors.Add("#FFEE22");
    //lstcolors.Add("#33BBCC");
    lstcolors.Add("#CC0022");
    //lstcolors.Add("#FF0000");
    lstcolors.Add("#339900");
    lstcolors.Add("#FF7700");
    lstcolors.Add("#33BBCC");
    lstcolors.Add("#99EEEE");
    lstcolors.Add("#6699FF");
    lstcolors.Add("#9966BB");
    lstcolors.Add("#99BB66");
    lstcolors.Add("#FF7700");
    lstcolors.Add("#FFEE22");
    lstcolors.Add("#FFCBB9");
    lstcolors.Add("EAEC93");
    lstcolors.Add("D7FBE6");
    lstcolors.Add("FFCACA");

    for (int i = 0; i < dschart.Tables[0].Rows.Count; i++)
    {
        lstXaxis.Add(dschart.Tables[0].Rows[i]["Doctor Name"].ToString());

    }
    List<ChartEx> lstseries = new List<ChartEx>();
    int count = 0;
    for (int i = 0; i < dschart.Tables[0].Rows.Count; i++)
    {

        ChartEx oEx = new ChartEx();
        oEx.name = dschart.Tables[0].Rows[i]["Doctor Name"].ToString();
        //oEx.data.Add(Convert.ToInt32(dschart.Tables[0].Rows[i]["Patients"]));
        oEx.data = new List<int>() { Convert.ToInt32(dschart.Tables[0].Rows[i]["Patients"]) };
        oEx.color = lstcolors[count];
        lstseries.Add(oEx);

        count++;
        if (count >= lstcolors.Count)
         count = 0; 


    }
    //Convert X axis data to JSON
    JavaScriptSerializer oSerializer1 = new JavaScriptSerializer();
    hdnXaxis.Value = oSerializer1.Serialize(lstXaxis);




    //Convert Y axis data to JSON
    JavaScriptSerializer oSerializer2 = new JavaScriptSerializer();
    hdnYaxis.Value = oSerializer1.Serialize(lstseries);

}

我没有在图表函数中获取“Javascriptxvalue”和“Javascriptyvalue”的值

任何人都可以帮助我

此致 帕布

1 个答案:

答案 0 :(得分:0)

据推测,'hdnXaxis'是HiddenFieldControl服务器控件的id?也许这不是你的想法

var Javascriptxvalue= $.parseJSON($("#"+ <%= hdnXaxis.ClientId %>).val());

您可以使用服务器标记直接将值注入页面,而不是通过输入传递字符串。像这样:

<%= "alert('" + MyPublicProperty + "')" %>

这会提醒您注意代码中定义的属性的值。然后你可以将它设置为js变量,如下所示:

<%= "var Javascriptxvalue = '" + xProperty + "';"  %>

你需要直接在aspx / ascx / razor页面中运行这段代码来设置变量,但我认为这比依赖具有特定id的控件要好。