我的代码中有两个方法,一个返回一个int数组,另一个返回一个字符串数组。我想使用这两个数组来填充谷歌图表数据表。
我对Json或AJAX没有任何经验,但是我发现了使用string.Join和eval将字符串转换为数组的相对简单的解决方案。
我设法用int数组做到了这一点,但是我发现字符串数组有问题。我知道问题是我需要在数组中的每个元素上添加“”。
function drawChart() {
var locality = ['Loc_A', 'Loc_B', 'Loc_C', 'Loc_D', 'Loc_E'];
//var locality = eval('[<% =string.Join(", ", locations)%>]');
var frequency = eval('[<% =string.Join(", ", numbers ) %>]');
var data = new google.visualization.DataTable();
data.addColumn('string', 'locality');
data.addColumn('number', 'frequency');
for (i = 0; i < locality.length; i++)
data.addRow([locality[i], frequency[i]]);
var options = {
title: 'Test'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
我需要通过在每个单词上添加”来调整注释行,以便随后将字符串转换为javascript数组。
关于如何将c#数组从代码行为转换为Java脚本数组的任何想法或任何其他提示。代码示例将非常有帮助。
方法1:在drawChart()中进行图表数据格式化
使用System.Web.Script.Serialization;
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 data-dash中将JavaScript中的数据提取为
var locality = $('#piechart').data('piechart-localityary');
方法2:在代码后面的图表数据格式
我建议保持DrawChart()函数简单。您提到您在后面的代码中有可用的数组。
1)在后面的代码中构建JavaScript字符串文字对象。格式
2)将jSonData作为数据属性附加到html页面中,或使用ajax将其拉出。如果不是很关键,我建议您保存服务器往返行程。
3)从html-data-dash将数据获取到drawChart()中
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;
}
希望这可以帮助您构建解决方案。如果您需要澄清或特定的代码,请添加注释。我使用剃刀(MVC)构建html来填充数据属性。
可能的问题:JavaScriptSerializer在数字前后添加引号。
{"c":[{"v":"Loc_A","f":null},{"v": "10.00" ,"f":null}]}
注意“ 10.00”,而不是10.00。GoogleCharts不喜欢它。它可能不会绘制图表或绘制不正确的图表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句