TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
samo kung
NA
7
2.7k
Google Chart With ASP.NET Core can't get it to work
Feb 19 2018 3:45 AM
public
JsonResult PopulationChart()
{
using
(SqlConnection con =
new
SqlConnection(
"Data Source=.....;Initial Catalog=....;User ID=....;Password=.....;"
))
{
//await con.OpenAsync();
con.Open();
SqlTransaction transaction;
transaction = con.BeginTransaction();
using
(SqlCommand cmd =
new
SqlCommand(
"GenLocationData"
, con, transaction))
{
try
{
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@UN", username);
//cmd.Parameters.AddWithValue("@PW", password);
//await cmd.ExecuteNonQueryAsync();
cmd.ExecuteNonQuery();
transaction.Commit();
var populationList = _smscontext.LocationDatas.OrderBy(xx => xx.Location).ToList();
return
Json(populationList);
//return RedirectToAction("Index", "UserHome");
}
catch
(Exception ex)
{
transaction.Rollback();
ModelState.AddModelError(
""
, ex.Message);
var populationList = _smscontext.LocationDatas.OrderBy(xx => xx.Location).ToList();
return
Json(populationList);
}
}
}
}
<div id=
"chart_div"
></div>
<script type=
"text/javascript"
>
//google.charts.load('current', {
// packages: ['corechart']
//});
google.charts.load(
'current'
, { packages: [
'corechart'
,
'bar'
] });
google.charts.setOnLoadCallback(LoadData);
function
LoadData()
{
$.ajax({
url:
'@Url.Action("PopulationChart","SMS")'
,
dataType:
"json"
,
type:
"GET"
,
error:
function
(xhr, status, error)
{
var
err = eval(
"("
+ xhr.responseText +
")"
);
toastr.error(err.message);
},
success:
function
(data)
{
PopulationChart(data);
return
false
;
}
});
return
false
;
}
function
PopulationChart(data) {
var
tdata =
new
google.visualization.DataTable(data);
tdata.addColumn(
'string'
,
'Location'
);
tdata.addColumn(
'number'
,
'Count'
);
for
(
var
i = 0; i < data.length; i++) {
//tdata.data.addRow([data[i].Location, data[i].Count]);
var
xxx = data[i].Location;
tdata.addRow([data[i].Location, 1]);
}
var
options = {
'title'
:
'How Much Pizza I Ate Last Night'
,
//'width': 500,
//'height': 300
//is3D: true
};
var
chart =
new
google.visualization.BarChart(document.getElementById(
'chart_div'
));
chart.draw(tdata, options);
return
false
;
}
</script>
My problem is google charts can't seem to display the data[i].Location contents. Any help would be very appreciated.
Reply
Answers (
1
)
Post data to web api with entity framework using aspx page
To show notification