如何使用 google 图表(饼图)在 asp.net mvc 中将 jsonresult 数据显示为图表视图而不是数组
How to display jsonresult data as a chart view and not an array in asp.net mvc using google charts (pie)
我正在尝试根据特定查询显示 google 饼图,但我得到的结果是:
[["Status","TotalProgress"],["Completed",1],["In-Progress",2],["To-Do",1]]
应该是:
expected pie chart
在我的代码中,场景如下:
主管想要查看选定学生的进度:
控制器方法(我使用了本教程,但它不包括 "id" 因素):
link to tutorial i used
控制器方法:
public JsonResult AjaxMilestoneStatus(string id)
{
string query = $"SELECT Status, COUNT(Progress) AS TotalProgress from Milestone
Where StudentNumber = '{id}' GROUP BY Status";
// string query = "SELECT Status, COUNT(Progress) AS TotalProgress from
Milestone GROUP BY Status ";
string constr =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Status", "TotalProgress"
});
using (var 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["Status"], sdr["TotalProgress"]
});
}
}
con.Close();
}
}
return Json(chartData, JsonRequestBehavior.AllowGet);
}
然后视图如下所示:
enter code here
<!DOCTYPE HTML>
<html>
<head>
<script src="~/Scripts/jquery-3.3.1.js"></script>
</head>
<body>
<h1></h1>
@*<div id="chartContainer" style="height: 370px; width: 100%;"></div>*@
<script type="text/javascript"
src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script type="text/javascript"
src="http://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: "Get",
url: "/CHART/AjaxMilestoneStatus",
data: '{id: id}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r);
//Pie
var options = {
title: 'My Progress'
};
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>
`
我在某处读到我应该调用 jsonresult 方法到我的控制器中的视图,如果这是我应该这样做的方式请帮助我,我真的很挣扎,请帮助!
谢谢。
查看
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<script type="text/javascript" src="http://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: "GET",
//my url is different than yours
//url: "/CHART/AjaxMilestoneStatus",
url: "/Home/AjaxMilestoneStatus",
//took off parens in my data
data: { id: $("#aHidden").val() },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r);
//Pie
var options = {
title: 'My Progress'
};
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>
<input type="hidden" id="aHidden" value="2" />
<div id="chart" style="width: 500px; height: 400px;">
</div>
</body>
</html>
控制器
public JsonResult AjaxMilestoneStatus(string id)
{
//swapped {id} with {0}
string query = String.Format(@"SELECT Status, COUNT(Progress) AS TotalProgress from Milestone Where StudentNumber = '{0}' GROUP BY Status",
id);
string constr =
ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Status", "TotalProgress"
});
using (var 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["Status"], sdr["TotalProgress"]
});
}
}
con.Close();
}
}
return Json(chartData, JsonRequestBehavior.AllowGet);
}
我正在尝试根据特定查询显示 google 饼图,但我得到的结果是: [["Status","TotalProgress"],["Completed",1],["In-Progress",2],["To-Do",1]] 应该是:
expected pie chart 在我的代码中,场景如下: 主管想要查看选定学生的进度: 控制器方法(我使用了本教程,但它不包括 "id" 因素): link to tutorial i used
控制器方法:
public JsonResult AjaxMilestoneStatus(string id)
{
string query = $"SELECT Status, COUNT(Progress) AS TotalProgress from Milestone
Where StudentNumber = '{id}' GROUP BY Status";
// string query = "SELECT Status, COUNT(Progress) AS TotalProgress from
Milestone GROUP BY Status ";
string constr =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Status", "TotalProgress"
});
using (var 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["Status"], sdr["TotalProgress"]
});
}
}
con.Close();
}
}
return Json(chartData, JsonRequestBehavior.AllowGet);
}
然后视图如下所示:
enter code here
<!DOCTYPE HTML>
<html>
<head>
<script src="~/Scripts/jquery-3.3.1.js"></script>
</head>
<body>
<h1></h1>
@*<div id="chartContainer" style="height: 370px; width: 100%;"></div>*@
<script type="text/javascript"
src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<script type="text/javascript"
src="http://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: "Get",
url: "/CHART/AjaxMilestoneStatus",
data: '{id: id}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r);
//Pie
var options = {
title: 'My Progress'
};
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>
`
我在某处读到我应该调用 jsonresult 方法到我的控制器中的视图,如果这是我应该这样做的方式请帮助我,我真的很挣扎,请帮助! 谢谢。
查看
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<script type="text/javascript" src="http://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: "GET",
//my url is different than yours
//url: "/CHART/AjaxMilestoneStatus",
url: "/Home/AjaxMilestoneStatus",
//took off parens in my data
data: { id: $("#aHidden").val() },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r);
//Pie
var options = {
title: 'My Progress'
};
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>
<input type="hidden" id="aHidden" value="2" />
<div id="chart" style="width: 500px; height: 400px;">
</div>
</body>
</html>
控制器
public JsonResult AjaxMilestoneStatus(string id)
{
//swapped {id} with {0}
string query = String.Format(@"SELECT Status, COUNT(Progress) AS TotalProgress from Milestone Where StudentNumber = '{0}' GROUP BY Status",
id);
string constr =
ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Status", "TotalProgress"
});
using (var 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["Status"], sdr["TotalProgress"]
});
}
}
con.Close();
}
}
return Json(chartData, JsonRequestBehavior.AllowGet);
}