1

你如何将一个函数包装到另一个函数中,以便我可以在后面的代码中调用它?

所以这是我需要包装并将其调用到函数的代码:

$(function () { 
$('#container').highcharts({
    chart: {
        type: 'bar'
    },
    title: {
        text: 'Fruit Consumption'
    },
    xAxis: {
        categories: ['Apples', 'Bananas', 'Oranges']
    },
    yAxis: {
        title: {
            text: 'Fruit eaten'
        }
    },
    series: [{
        name: 'Jane',
        data: [1, 0, 4]
    }, {
        name: 'John',
        data: [5, 7, 3]
    }]
    });
 });​

<div id="container" style="width:340px; height:270px;"></div>

有人告诉我我可以把它包装起来,然后调用这个函数。如何才能做到这一点?

该数据是虚拟的,我的数据来自服务器。我想要的是在后面的代码中重绘图表,以便使用 RegisterClientScriptBlock 刷新数据。

4

3 回答 3

2

在 aspx 页面中

<script type="text/javascript">
    function container_highcharts() {
        $('#container').highcharts({
            chart: {
                type: 'bar'
            },
            title: {
                text: 'Fruit Consumption'
            },
            xAxis: {
                categories: ['Apples', 'Bananas', 'Oranges']
            },
            yAxis: {
                title: {
                    text: 'Fruit eaten'
                }
            },
            series: [{
                name: 'Jane',
                data: [1, 0, 4]
            }, {
                name: 'John',
                data: [5, 7, 3]
            }]
        });
    }
</script>

在后面的代码中

protected void Page_Load(object sender, EventArgs e){
     string someScript = "<script language='javascript'>container_highcharts();</script>";
     Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", someScript);
}
于 2013-07-10T09:31:05.297 回答
2

定义以下通用函数:

<script type="text/javascript">
    function container_highcharts(chartTitle, xAxisCategories, yAxisTitle, seriesArray)
    {
        $(document).ready(function()
        {
            $('#container').highcharts({
                chart: {
                    type: 'bar'
                },
                title: {
                    text: chartTitle
                },
                xAxis: {
                    categories: xAxisCategories
                },
                yAxis: {
                    title: {
                        text: yAxisTitle
                    }
                },
                series: seriesArray
            });
        });
    }
</script>

然后在代码隐藏中,使用如下动态参数注册此函数调用:

protected void Page_Load(object sender, EventArgs e)
{
     string chartTitle = GetChartTitle();
     string xAxisCategories = GetXAxisCategories();
     string yAxisTitle = GetYAxisTitle();
     string seriesArray = GetSeriesArrayAsString();

     string someScript = String.Format("<script language='javascript'>container_highcharts({0}, {1}, {2}, {3});</script>", chartTitle, xAxisCategories, yAxisTitle, seriesArray);
     Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", someScript);
}

这将注册您的脚本,该脚本将在页面加载时运行。

于 2013-07-10T09:55:33.920 回答
1

这就是你需要的:

protected void Page_Load(object sender, EventArgs e){
 string someScript = "<script language='javascript'>container_highcharts();</script>";
 Page.ClientScript.RegisterStartupScript(this.GetType(), "onload", someScript);
}
于 2013-07-11T10:42:26.397 回答