嗨专家我知道它自己的标题已经在论坛中被问过很多次但是在我提出这个问题之前我已经在互联网上搜索答案以试图自己解决但无济于事这里是我的方法试过
<WebMethod()> _
Public Function GetStatisticsForChart() As String
'some repository that returns data....
Dim abc As New incidentsdataDataContext
'It simply returns a list of objects with Year and Count properties.
Dim query1 = (From a In abc.election_incidents _
Select a).ToList()
'let's instantiate the DataTable.
Dim dt = New Google.DataTable.Net.Wrapper.DataTable()
dt.AddColumn(New Column(ColumnType.String, "INCIDENT_TYPE", "INCIDENT_TYPE"))
dt.AddColumn(New Column(ColumnType.String, "MPS", "MPS"))
For Each result In query1
Dim r As Row = dt.NewRow()
r.AddCellRange(New Cell() {New Cell(result.INCIDENT_TYPE), New Cell(result.MPS)})
dt.AddRow(r)
Next
Return dt.GetJson()
End Function
和另一个
Public Shared Function GetJSONString(Dt As System.Data.DataTable) As String
Dim StrDc As String() = New String(Dt.Columns.Count - 1) {}
Dim HeadStr As String = String.Empty
For i As Integer = 0 To Dt.Columns.Count - 1
StrDc(i) = Dt.Columns(i).Caption
HeadStr += """" & StrDc(i) & """ : """ & StrDc(i) & i.ToString() & "%" & ""","
Next
HeadStr = HeadStr.Substring(0, HeadStr.Length - 1)
Dim Sb As New StringBuilder()
Sb.Append("{""" & Convert.ToString(Dt.TableName) & """ : [")
For i As Integer = 0 To Dt.Rows.Count - 1
Dim TempStr As String = HeadStr
Sb.Append("{")
For j As Integer = 0 To Dt.Columns.Count - 1
TempStr = TempStr.Replace(Dt.Columns(j).ToString & "¾", Dt.Rows(i)(j).ToString())
Next
Sb.Append(TempStr & "},")
Next
Sb = New StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1))
Sb.Append("]}")
System.Diagnostics.Debug.Write(Sb)
Return Sb.ToString()
End Function
由于与其他功能相似,我只发布了两个
这是我访问网络服务的代码
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', { 'packages': ['corechart'] });
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
type: "POST",
data:{},
url: "services/recorder.asmx/GetStatisticsForChart",
dataType: "json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData, 0.6);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, { width: 1000, height: 500 });
}
</script>
现在谷歌图表抛出错误见下文
Uncaught Error: Invalid JSON string: <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"cols": [{"type": "string" ,"id": "INCIDENT_TYPE" ,"label": "INCIDENT_TYPE" }, {"type": "string" ,"id": "MPS" ,"label": "MPS" }], "rows" : [{"c" : [{"v": "1"}, {"v": "020901000"}]}, {"c" : [{"v": "2"}, {"v": "020901000"}]}, {"c" : [{"v": "3"}, {"v": "020901000"}]}, {"c" : [{"v": "4"}, {"v": "020901000"}]}, {"c" : [{"v": "5"}, {"v": "020901000"}]}, {"c" : [{"v": "6"}, {"v": "020901000"}]}, {"c" : [{"v": "7"}, {"v": "020901000"}]}, {"c" : [{"v": "8"}, {"v": "020901000"}]}, {"c" : [{"v": "9"}, {"v": "020901000"}]}, {"c" : [{"v": "10"}, {"v": "020901000"}]}, {"c" : [{"v": "11"}, {"v": "020901000"}]}, {"c" : [{"v": "12"}, {"v": "020901000"}]}, {"c" : [{"v": "13"}, {"v": "020901000"}]}, {"c" : [{"v": "14"}, {"v": "020901000"}]}, {"c" : [{"v": "15"}, {"v": "020901000"}]}, {"c" : [{"v": "16"}, {"v": "020901000"}]}, {"c" : [{"v": "17"}, {"v": "020901000"}]}, {"c" : [{"v": "18"}, {"v": "020901000"}]}, {"c" : [{"v": "3"}, {"v": "031402000"}]}, {"c" : [{"v": "5"}, {"v": "031402000"}]}, {"c" : [{"v": "5"}, {"v": "031402000"}]}, {"c" : [{"v": "2"}, {"v": "023114000"}]}, {"c" : [{"v": "1"}, {"v": "023114000"}]}, {"c" : [{"v": "2"}, {"v": "023114000"}]}, {"c" : [{"v": "2"}, {"v": "023114000"}]}, {"c" : [{"v": "3"}, {"v": "023114000"}]}, {"c" : [{"v": "3"}, {"v": "023114000"}]}, {"c" : [{"v": "18"}, {"v": "023114000"}]}, {"c" : [{"v": "18"}, {"v": "023114000"}]}, {"c" : [{"v": "18"}, {"v": "021529000"}]}, {"c" : [{"v": "3"}, {"v": "021529000"}]}, {"c" : [{"v": "3"}, {"v": "021529000"}]}, {"c" : [{"v": "3"}, {"v": "021529000"}]}, {"c" : [{"v": "3"}, {"v": "021529000"}]}, {"c" : [{"v": "3"}, {"v": "021529000"}]}, {"c" : [{"v": "3"}, {"v": "021529000"}]}, {"c" : [{"v": "5"}, {"v": "021529000"}]}, {"c" : [{"v": "6"}, {"v": "023101000"}]}, {"c" : [{"v": "1"}, {"v": "021502000"}]}, {"c" : [{"v": "11"}, {"v": "021502000"}]}, {"c" : [{"v": "14"}, {"v": "021502000"}]}, {"c" : [{"v": "8"}, {"v": "021502000"}]}, {"c" : [{"v": "7"}, {"v": "021504000"}]}, {"c" : [{"v": "7"}, {"v": "021507000"}]}, {"c" : [{"v": "16"}, {"v": "021507000"}]}, {"c" : [{"v": "11"}, {"v": "023116000"}]}, {"c" : [{"v": "3"}, {"v": "025801000"}]}, {"c" : [{"v": "3"}, {"v": "025801000"}]}, {"c" : [{"v": "10"}, {"v": "023108000"}]}, {"c" : [{"v": "10"}, {"v": "023108000"}]}]}</string>
你能帮我吗?