带有 asp.net 的图表 js 线
Chart js line with asp.net
我正在尝试在 ASP.NET 中使用图表 js 制作折线图,但这不起作用,在我尝试使用饼图之前它工作正常,但折线图不起作用,我认为响应不正确,因为在控制台中显示 "uncaught SyntaxError: unexpected token : ".
这是代码 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>
经过一段时间我找到了解决方案,问题出在 eval 方法中,我需要添加 "()"
var data = (eval("(" + r.d + ")"));
谢谢
我正在尝试在 ASP.NET 中使用图表 js 制作折线图,但这不起作用,在我尝试使用饼图之前它工作正常,但折线图不起作用,我认为响应不正确,因为在控制台中显示 "uncaught SyntaxError: unexpected token : ".
这是代码 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>
经过一段时间我找到了解决方案,问题出在 eval 方法中,我需要添加 "()"
var data = (eval("(" + r.d + ")"));
谢谢