0
我想在圖表js在ASP.NET中做一個折線圖,但這不工作,之前我嘗試與餅圖,這工作正常,但與折線圖不不起作用,我認爲響應不正確,因爲在控制檯中顯示「uncaught SyntaxError:unexpected token:」。
這是代碼C#:圖js線與asp.net
[WebMethod]
public static string GetChart(string country)
{
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.Append("labels:[\"January\",\"February\",\"March\",\"April\",\"May\",\"June\"],");
sb.Append("datasets:[");
System.Threading.Thread.Sleep(50);
string color = "rgba(220,220,220,0.2)";
//
sb.Append("{");
sb.Append(string.Format("fillColor:\"{0}\", strokeColor:\"{1}\", pointColor:\"{2}\", pointStrokeColor:\"{3}\", data:{4}", color, "#ACC26D", "#fff", "#9DB86D", "[203,156,99,251,305,247]"));
//
sb.Append("}");
sb.Append("]");
sb.Append("};");
return sb.ToString();
}
這是JavaScript:
<script type="text/javascript">
function LoadChart() {
var chartType = parseInt($("[id*=rblChartType] input:checked").val());
$.ajax({
type: "POST",
url: "inicioCliente.aspx/GetChart",
data: "{country: '" + $("[id*=ddlCountries]").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
$("#dvChart").html("");
$("#dvLegend").html("");
var data = eval((r.d));
var el = document.createElement('canvas');
$("#dvChart")[0].appendChild(el);
var ctx = el.getContext('2d');
var userStrengthsChart;
switch (chartType) {
case 1:
userStrengthsChart = new Chart(ctx).Line(data);
break;
case 2:
userStrengthsChart = new Chart(ctx).Doughnut(data);
break;
}
for (var i = 0; i < data.length; i++) {
var div = $("<div />");
div.css("margin-bottom", "10px");
div.html("<span style = 'display:inline-block;height:10px;width:10px;background-color:" + data[i].color + "'></span> " + data[i].text);
$("#dvLegend").append(div);
}
},
failure: function (response) {
alert('There was an error.');
}
});
}
$(function() {
LoadChart();
$("[id*=ddlCountries]").bind("change", function() {
LoadChart();
});
$("[id*=rblChartType] input").bind("click", function() {
LoadChart();
});
});
</script>
你能張貼在您收到錯誤的詳細信息?什麼行說你有語法錯誤,並指向我們該行的代碼。 – JNYRanger 2015-03-02 16:34:53
未捕獲SyntaxError:意外標記:VM098響應:{labels:[「January」,「February」,「March」,「April」,「May」,「June」],datasets:[{fillColor:「rgba(220,220,220 ,0.2)「,strokeColor:」#ACC26D「,pointColor:」#ffff「,pointStrokeColor:」#9DB86D「,data:[203,156,99,251,305,247]}]}; – 2015-03-02 16:42:04
我想你應該閱讀更多關於JSON。所有的字段名都應該用雙引號:'fillColor' - >'「fillColor」'。從這行刪除不必要的分號:'sb.Append(「};」);' - >'sb.Append(「}」);'。如果這是有效的JSON,我建議從'getChart'測試你的數據。有幾個在線驗證器,比如[this one](http://json.parser.online.fr)。 – 2015-03-03 11:20:16