0

所以我试图在我的asp.net网站上使用这个javascript https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart

我在代码隐藏中有一个数组,其中包含我的数据,我将其转换为这样的多维数据表。

<in codebehind vb>
Public Property datat As DataTable

For outerIndex As Integer = 0 To 2
    Dim newRow As DataRow = table.NewRow()
    For innerIndex As Integer = 0 To 2
            newRow(innerIndex) = Array(outerIndex, innerIndex)
    Next
    table.Rows.Add(newRow)
Next

datat = table

<in asp>

function drawChart() {
    var data = <%=datat%>

我总是得到一个未定义的错误数据表。

我的新作品

<in asp>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawcharts);
    function drawcharts() {
        var data = google.visualization.arrayToDataTable([
            ['X', 'MW'],
      [<%=mwtimepublic%>, <%=mwarraypublic%>]
        ]);

        var options = {
            title: 'MW Trend'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    });
</script>

公共函数 Dtable() As DataTable 将 startdate 作为 String 将 enddate 作为 String

    If FirstRun = False Then
        datat.Clear()
    End If

    If FirstRun = True Then
        FirstRun = False
        enddate = Date.Today
        startdate = DateAdd(DateInterval.Day, -20, Date.Today)
    Else
        enddate = TextBox2.Text
        startdate = TextBox1.Text
    End If

'<confidential code></confidentialcode>

    Dim mwaray(pdata.Count)
    Dim mwtime(pdata.Count)
    Dim table As New DataTable
    table.Columns.Add("X")
    table.Columns.Add("MW")
    Dim i As Integer
    Dim x As Integer
    For i = 1 To pdata.Count
        mwaray(i) = pdata(i).Value
        If mwaray(i) > upperlimit Then
            upperlimit = mwaray(i) + 50
        End If
    Next

    For i = 1 To pdata.Count
        mwtime(i) = "'" & pdata(i).TimeStamp.LocalDate.Month.ToString() & "/" & pdata(i).TimeStamp.LocalDate.Day.ToString() & "/" & pdata(i).TimeStamp.LocalDate.Year.ToString() & " " & pdata(i).TimeStamp.LocalDate.TimeOfDay.Hours.ToString() & ":" & pdata(i).TimeStamp.LocalDate.TimeOfDay.Minutes.ToString() & ":" & pdata(i).TimeStamp.LocalDate.TimeOfDay.Seconds.ToString() & "'"
    Next

    For i = 1 To pdata.Count
        mwarraypublic.Add(mwaray(i))
    Next

    For i = 1 To pdata.Count
        mwtimepublic.Add(mwtime(i))
    Next

End Function

我仍然没有绘制图表

4

2 回答 2

0

我制作了一个视频,介绍如何在从代码隐藏获取数组到 javascript 时创建 jquery ui 自动完成功能

跳到 6:00 看看我是如何制作数组的 http://www.youtube.com/watch?v=ULHSqoDHP-U&list=UUgtKyNvllU9E6c2Mi8OtzCQ&index=6&feature=plcp

PageMethods.YourWebMethod(function(results){
  var data = resluts;
  //the rest of your code goes here...
});

更新:


Google 实际上将数组数组转换为数据表。换句话说,您必须返回一个行数组,它们自己的行是一个列值数组。

因此,在您背后的代码中,首先执行以下导入 webservices

Imports System.Web.Services

然后使用返回数组列表的函数创建您的网络方法

<WebMethod()>
Public Shared Function Dtable() As ArrayList
dim table As New ArrayList
Dim YourDataSetTable as new Dataset.YorDataTable
'fill you table here

'first get column names as the first row/array
dim colNames as New ArrayList

For Each col as DataColumn in YourDataSetTable.Columns
   colNames.Add(col.ColumnName)
Next
table.Add(colNames)

'then on to the data
For Each r as DataRow in YourDataSetTable .Rows
  dim colVals as New ArrayList
  For Each col as DataColumn in YourDataSetTable.Columns
    colVals.Add(r.Item(col.ColumnName))
  Next
  table.Add(colVals)
Next
Return table
End Function

然后在javascript中

PageMethods.Dtable(function(results){
  var data = resluts;
  //the rest of your code goes here...
});

确保页面上有一个启用了 pagemethods 的脚本管理器。

于 2012-11-11T17:50:01.060 回答
0

您发布的代码示例不应编译。在 Visual Basic 中,有一些自动实现的属性,就像您在代码的第一行中看到的那样,然后是一个与数据表无关的块。

尝试将代码放入您的属性 getter:

Public ReadOnly Property datat As DataTable
   Get
      For outerIndex As Integer = 0 To 2
          Dim newRow As DataRow = table.NewRow()

          For innerIndex As Integer = 0 To 2
              newRow(innerIndex) = Array(outerIndex, innerIndex)
          Next

          table.Rows.Add(newRow)
      Next

      Return table
   End Get
End Property
于 2012-11-11T19:34:01.097 回答