0

I have a website in which I use Visual Studio's built in charts to visualize data.

I use c# methods to provide the chart with a datasource. However, google charts require the data on javascript methods. I have done some research on how to provide the data to a javascript method but I came up empty.

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
           google.load("visualization", "1", { packages: ["corechart"] });
           google.setOnLoadCallback(drawChart);
           function drawChart() {
               var data = new google.visualization.DataTable('<%=t%>');

               var options = {
                   title: 'Company Performance'
               };

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

In this example I tried to create a data table called "t" on the c# side. I debugged and saw that data table is filled correctly but I keep getting "Table has no columns" error.

So, Can anyone provide a walkthrough on how to supply my MSSQL data into the javascript method used by google charts?

Thanks in advance

Can Canbek
  • 334
  • 3
  • 16

1 Answers1

0

See this Google Line Charts

First Change

var data = new google.visualization.DataTable('<%=t%>');

to

var data = new google.visualization.arrayToDataTable('<%=t%>');

Now as discussed here Google Line Charts you should use Array of Array not the C# DataTable object. Convert you DataTable to Array of Array and then pass it to arrayToDataTable() function.

amrinder007
  • 1,455
  • 1
  • 12
  • 13
  • I use a List to retrieve the data from database then I turned it into a DataTable, per your suggestion I tried turning it into an array but it doesn't allow me to create a multidimensional array. – Can Canbek Jun 03 '13 at 10:10
  • @CanCanbek please provide the C# code through which you are trying to create multidimensional array. – amrinder007 Jun 03 '13 at 10:56
  • new google.visualization.DataTable('<%=table.Cast().ToArray()%>') Tried this but didn't work. – Can Canbek Jun 03 '13 at 11:27
  • @CanCanbek this will not work. You need to create multidimensional array as given in documentation from server side. Iterate through List and create array of array. See this http://stackoverflow.com/questions/549399/c-sharp-creating-an-array-of-arrays – amrinder007 Jun 03 '13 at 11:55