I am trying to bind data to a graph through a razor. I retrieve the data through a jquery ajax call and plan to bind the received data to the chart. Is it possible to include razor code in the jquery success function? How to associate count values with x, yValues in chart code?
https://www.asp.net/web-pages/overview/data/7-displaying-data-in-a-chart
@{
var CountsPath = Server.MapPath("~/Content/Images/counts.jpg");
if (File.Exists(CountsPath))
{
File.Delete(CountsPath);
}
var PathName = "~/Content/Images/counts.jpg";
if (!File.Exists(Server.MapPath(PathName)))
{
var chartImage = new Chart(600, 400);
chartImage.AddTitle("Count");
chartImage.AddSeries(
chartType: "Pie",
name: "Sales",
axisLabel: "Count",
xValue: new[] { "2011", "2014" },
yValues: new[] { "40", "285" });
chartImage.Save(path: PathName);
}
}
Jquery script
<script>
$(document).ready(function () {
$.ajax({
type: 'get',
url: '/Home/GetSales',
success: function (data) {
},
error: function (response) {
alert(response.Message);
},
dataType: 'json'
});
});
</script>
Json result:
{"Sales":[
{"Year":"2011", "loc":"ca"},
{"Year":"2014", "loc":"wa"},
{"Year":"2011", "loc":"wi"}
]}
Edit: Or Can I use the jquery result group inside Razor code?
source
share