1

我正在使用HighCharts JS在 ASP.NET 2.0 中创建条形图。

以下是图表在 javascript 中的操作方式:

$(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]
        }]
    });
});​

我在 c# 代码隐藏中有这个:

public string[] Names = new string[] { "Name1", "Name2", "Name3"};
public string[] Data = new  string[] { "[1,0,4]", "[5,7,3]", "[2,3,4]"};

如何在 javascript 中使用此字符串 [] 名称和数据来更改图表的值?

请帮助我陷入困境。:( 谢谢!

--为阿里斯托斯编辑--

我试过你的答案,但这不起作用。根据您的回答,此 javascript 将与您所做的相同。检查这个:

$(function () { 
    var seriesTest = "{ name: 'Jane', data: [1, 0, 4] }, { name: 'John', data: [5, 7, 3] }";

    $('#container').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
        },
        yAxis: {
            title: {
                text: 'Fruit eaten'
            }
        },
        series: [seriesTest]
    });
});​

我已经尝试过了,但这不起作用,因为我认为 series.data 只接受其括号([])内的整数。

我已经按照 KingCronus 的建议检查了 DotNet.HighCharts 但这不能在 asp.net 2.0 框架中使用。:(

4

2 回答 2

0

这是我对我的问题的解决方案,以防有人也需要这个。

这是JS函数:

function LoadChart() {
    var Names = <%= this.javaSerial.Serialize(this.Names %>;
    var Data =  <%= this.javaSerial.Serialize(this.Data %>;

    var options = {
        chart: {
            renderTo: 'container',
            type: 'bar'
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apple', 'Banana', 'Orange']
        },
        yAxis: {
            title: {
                text: 'fruit eaten'
            }
        },
        series: [{
            name: Names[0],
            data: [Data[0]]
        }]
    };

    for (var i=1; i<Names.Length; i++)
    {
        options.series.push({
            name: Names[i],
            data: [Data[i]]
        });
    }
    var chart = new Highcharts.Chart(options);
}

这是后面的部分代码:

public JavaScriptSerializer javaSerial = new JavaScriptSerializer(); 
public string[] Names = new string[] { "Jane", "John", "Mike" };
public string[] Data = new string[] { "1,2,3", "2,4,6", "3,6,9" }
于 2013-06-07T18:19:39.407 回答
0

On the most simple form, you just need to render them on the correct format. Here is a simple example that maybe need a few tweeks...

Run this on code behind, maybe on PageLoad

StringBuilder sb = new StringBuilder();

for(int i = 0 ; i < 3 ; i++)
 sb.AppendFormat("{3}{{name: '{0}',data: {1} }}", 
      Names[i], Data[i], 
       i>0 ? "," : "" // this is render the comma "," after the first line
  );    

ScriptData.Text = sb.ToString();

And place them on the script on the page using a Literal control

$(function () { 
    $('#container').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
        },
        yAxis: {
            title: {
                text: 'Fruit eaten'
            }
        },
        series: [
        <asp:Literan id="ScriptData" Runat="server" EnableViewState="off" />
        ]
    });
});?
于 2013-06-07T11:50:43.217 回答