0
我使用下面的片段來填充Google Chart -將數據傳遞給JavaScript函數
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
這工作一種享受,並根據需要創建一個折線圖。
不過,我希望將數據從我的代碼傳遞給這個函數的背後 -
protected void Page_Load(object sender, EventArgs e)
{
string JsonString = "{'res':[{'category':'A','count':17167},{'category':'B','count':18183},{'category':'C','count':17972},{'category':'D','count':18539}]}";
Jobj = JObject.Parse(JsonString);
// extract values from Json
foreach (var response in Jobj["res"])
{
string category= (string)response["category"];
int count = (int)response["count"];
// put values into format that can be passed to the javascript function
}
}
然後用像 -
function drawChart() {
var data = google.visualization.arrayToDataTable([<%=Data Values%>]);
凡count
和category
是X軸和Y軸值和17167,18183 etc
是圖上的點。 但是,顯然上述語法不正確,我如何修改函數來接受我的Json數據?
找到[此處](https://developers.google.com/chart/interactive/docs/reference#DataTable)文檔有幫助嗎?它似乎描述你需要什麼...... – Floris