2

我有一个 json 数组,我将, how do I access it in the javascript. I am using javascriptamcharts and want to passvb.net` 数组序列化为 javascript 图表。代码如下:

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    DataSetToJSON()
End Sub
Public Function SelectChartData(ByVal Name As String) As DataSet
    Dim sqlCmd As New SqlCommand
    Dim myData As New DataSet

    sqlCmd.CommandTimeout = 1200

    sqlCmd.CommandType = CommandType.StoredProcedure
    sqlCmd.CommandText = "SelectAmChartdata"

    sqlCmd.Parameters.Add("@Name", SqlDbType.VarChar)
    sqlCmd.Parameters("@Name").Value = Name

    Dim da As New dsSV1TableAdapters.tblGeneralTableAdapter

    da.Adapter.SelectCommand = sqlCmd

    sqlCmd.Connection = da.Connection
    sqlCmd.Connection.Open()
    da.Adapter.Fill(myData)

    sqlCmd.Dispose()
    sqlCmd = Nothing

    da.Connection.Close()
    da.Connection.Dispose()
    da.Connection = Nothing

    Return myData

End Function

Public Function DataSetToJSON()
    Dim dt As DataTable
    ds = SelectChartData("Weather")


    Dim dict As New Dictionary(Of String, Object)

    For Each dt In ds.Tables
        Dim arr(dt.Rows.Count) As Object

        For i As Integer = 0 To dt.Rows.Count - 1
            arr(i) = dt.Rows(i).ItemArray
        Next

        dict.Add(dt.TableName, arr)
    Next

    Dim json As New JavaScriptSerializer
    Return json.Serialize(dict)

End Function

ASPX

<script type="text/javascript">

//var chartData =  **Access Serverside Array**

    window.onload = function () {
    var chart = new AmCharts.AmSerialChart();
    chart.dataProvider = chartData;
    chart.categoryField = "country";
    chart.marginTop = 15;
    chart.marginLeft = 55;
    chart.marginRight = 15;
    chart.marginBottom = 80;
    chart.angle = 30;
    chart.depth3D = 15;

    var catAxis = chart.categoryAxis;
    catAxis.gridCount = chartData.length;
    catAxis.labelRotation = 90;

    var graph = new AmCharts.AmGraph();
    graph.balloonText = "[[category]]: [[value]]";
    graph.valueField = "visits"
    graph.type = "column";
    graph.lineAlpha = 0;
    graph.fillAlphas = 0.8;
    chart.addGraph(graph);

    chart.write('chartContainer');
}
</script
4

1 回答 1

0

您可以将序列化数据直接转储到 ASPX 页面中:

<script type="text/javascript">

    var my_data = <%= DataSetToJSON() %>;

</script>

或者您可以在 ASPX 页面中使用:

<input type="hidden" id="my_data" clientidmode="static" runat="server" value="" />

在代码隐藏中:

my_data.Value = DataSetToJSON();

然后,您可以使用document.getElementById('my_id').value提取数据,使用类似的东西对其进行解码并将其JSON.parse传递到您的图表。

于 2012-11-19T09:55:28.640 回答