我正在尝试将查询绑定到谷歌图表,但我遇到了如下所示的问题。我是 asp.net 核心的新手,所以当我尝试使用谷歌图表时,我遇到了这个问题,下面是我的代码,请提供任何帮助或您建议我使用的任何其他免费图表
大家好,我正在尝试将查询绑定到谷歌图表,但我遇到了如下所示的问题。我是 asp.net 核心的新手,所以当我尝试使用谷歌图表时,我遇到了这个问题,下面是我的代码,请提供任何帮助或您建议我使用的任何其他免费图表
public JsonResult AjaxMethod(IConfiguration config)
{
string query = "select [UserId], count([ServiceOrderNumber]) as ServiceOrders from [dbo].[ServiceOrders] group by [UserId] order by count ([ServiceOrderNumber]) desc";
string constr = this.configuration.GetConnectionString("DefaultConnection");
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"[UserId]", "[ServiceOrders]"
});
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["[UserId]"], sdr["[ServiceOrders]"]
});
}
}
con.Close();
}
}
return Json(chartData);
}
这是我的观点
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
$.ajax({
type: "POST",
url: "/Reports/AjaxMethod",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r);
//Pie
var options = {
title: 'USA City Distribution'
};
var chart = new google.visualization.PieChart($("#chart")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
<div id="chart" style="width: 500px; height: 400px;">
</div>
</body>
</html>