我试图在ASP.NET中使用图表js做一个折线图,但是在我尝试使用饼图并且这个工作正常之前,这不起作用,但是使用折线图并没有'工作,我认为响应是不正确的,因为在控制台显示"未捕获的SyntaxError:意外的令牌:"。
这是代码c#:
[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>
答案 0 :(得分:0)
一段时间后我找到了解决方案,问题出在eval方法中,我需要添加&#34;()&#34;
var data = (eval("(" + r.d + ")"));
由于