2

ASP.NETWebサイトにGoogleChartsを使用しようとしています。addrows関数のデータを読み込んで計算するページにこれを含めたいと思います。

グラフに必要なカウントを計算した後、コードビハインドからページ上のスクリプトにカウントを渡すにはどうすればよいですか。VisualStudio2008でVB.NETを使用しています。

カウントデータをグラフに渡す最良の方法はどのようになっていますか?

  // Load the Visualization API and the piechart package.
  google.load('visualization', '1.0', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Topping');
    data.addColumn('number', 'Slices');
    data.addRows([
      ['Mushrooms', 3],
      ['Onions', 1],
      ['Olives', 1],
      ['Zucchini', 1],
      ['Pepperoni', 2]
    ]);

    // Set chart options
    var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':400,
                   'height':300};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

4

1 に答える 1

1

サーバー側で Google DataTable クラスを作成し、そのインスタンスをデータで初期化し、JSON 文字列に保存してから、その文字列をクライアント側の Google DataTable に割り当てる必要があります。次のようになります。

C#

public string GoogleDataTableJson { get; private set; }

protected void Page_Load(object sender, EventArgs e)
{
  GoogleDataTable table = CreateAndFillGoogleDataTable();
  Serializer ser = new Serializer(typeof(GoogleDataTable);
  this.GoogleDataTableJson = ser.Serialize(table);
}

そしてJavaScriptで:

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

または、.NET DataTable クラスを使用して Google Visualization を埋めることができる .NET ヘルパーを使用できます。

http://code.google.com/p/bortosky-google-visualization/

于 2012-05-27T08:15:38.303 に答える