方法1:圖表數據drawChart formatiing()
使用System.Web.Script。序列化;
1)構建jsonobj
public string GetLocality()
{
string[] locality = new string[] { "Loc_A", "Loc_B", "Loc_C", "Loc_D", };
//long[] frequency = new long[] { 10, 20, 10, 80 };
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string jsonobj = jsSerializer.Serialize(locality);
return jsonobj;
}
2)無論是在HTML數據屬性添加這個或使用AJAX調用拉。
<div id="piechart" data-piechart-localityary='<ServerSide call/var Getlocality()'>
<div>
3)從HTML數據破折號拉數據在你的JavaScript作爲
var locality = $('#piechart').data('piechart-localityary');
方法2:在代碼圖表數據格式化背後
我會建議保持DrawChart()函數簡單。你提到你在後面的代碼中提供了數組。
1)在後面的代碼中構建JavaScript字符串字面值對象。 Format
2)將jSonData作爲數據屬性附加到html頁面中,或者使用ajax將其拉入。如果不是至關重要的話,我會建議保存服務器往返。
3)從HTML數據劃線
4)工作實例JSFIDDLE與源代碼
function drawChart() {
//var jsonData = $('#piechart').data('piechar-jsonobj');
var jsonData =
{
"cols":[
{"label":"Locality","type":"string"},
{"label":"Frequency","type":"number"}
],
"rows":[
{"c":[{"v":"Loc_A","f":null},{"v":10.00,"f":null}]},
{"c":[{"v":"Loc_B","f":null},{"v":20.00,"f":null}]},
{"c":[{"v":"Loc_C","f":null},{"v":10.00,"f":null}]},
{"c":[{"v":"Loc_D","f":null},{"v":80.00,"f":null}]}
]
} ;
var data = new google.visualization.DataTable(jsonData);
var options = {
title: 'MyPieChart',
backgroundColor: { fill: 'transparent' },
is3D: true
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
代碼來構建JavaScript字符串文字對象
public class Graph
{
public List<ColInfo> cols { get; set; }
public List<DataPointSet> rows { get; set; }
}
public class ColInfo
{
public string label { get; set; }
public string type { get; set; }
}
public class DataPointSet
{
public List<DataPoint> c { get; set; }
}
public class DataPoint
{
public string v { get; set; } // value
public string f { get; set; } // format
}
public string DemoPieChart()
{
Graph ChartData = new Graph();
ChartData.cols = new List<ColInfo>();
ChartData.rows = new List<DataPointSet>();
ColInfo Col1 = new ColInfo();
Col1.label = "Locality";
Col1.type = "string";
ColInfo Col2 = new ColInfo();
Col2.label = "Frequency";
Col2.type = "number";
ChartData.cols.Add(Col1);
ChartData.cols.Add(Col2);
//Loop your data entry from both array
string[] locality = new string[] { "Loc_A", "Loc_B", "Loc_C", "Loc_D", };
long[] frequency = new long[] { 10, 20, 10, 80 };
for (int i = 0; i < locality.Length; i++)
{
DataPointSet Row = new DataPointSet();
Row.c = new List<DataPoint>();
DataPoint p1 = new DataPoint();
p1.v = locality[i];
p1.f = null;
DataPoint p2 = new DataPoint();
p2.v = frequency[i].ToString("F") ;
p2.f = null;
Row.c.Add(p1);
Row.c.Add(p2);
ChartData.rows.Add(Row);
}
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
string jsonobj = jsSerializer.Serialize(ChartData);
return jsonobj;
}
希望這獲取此數據到drawChart()可以幫助您構建解決方案。如果您需要澄清或特定的代碼添加評論。我使用剃刀(MVC)構建html來填充數據屬性。
可能的問題:JavaScriptSerializer在數字附加引號。
{"c":[{"v":"Loc_A","f":null},{"v": "10.00" ,"f":null}]}
注意「10.00」而不是10.00。 GoogleCharts不喜歡它。它可能不繪製圖表或繪製不正確的圖表。
不知道我是否關注你但現在可以使用'var options = new列表 {「Test」};'例如,如果你想填充列表你可以實現列表 .Add( )方法..您的var選項將是C#列表的一個很好的示例 –
MethodMan
2014-11-08 17:00:53
只需使用System.Web.Script.Serialization.JavaScriptSerializer'將.NET對象序列化爲JSON即可。並使用'JSON.parse()'而不是'eval'。 – Dmitry 2014-11-08 17:02:54
你想要做的是將你的C#數據轉換爲JSON。我不知道C#,但我認爲有功能可以將各種C#源代碼轉換爲JSON。也許這將有助於:http://msdn.microsoft.com/en-us/library/bb412179(VS.100).aspx – jfriend00 2014-11-08 17:03:47