我正试图通过剃须刀将数据绑定到图形中。我通过jquery调用提取数据,并计划将结果数据绑定到图中。在jquery成功函数中是否有包含剃刀代码的选项?如何在图表代码中将计数值绑定到x、yValues?
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" },//need from json
yValues: new[] { "40", "285" });//need from json
chartImage.Save(path: PathName);
}
}
Jquery脚本
<script>
$(document).ready(function () {
$.ajax({
type: 'get',
url: '/Home/GetSales',
success: function (data) {
//Bind sales data counts by year result to graph
},
error: function (response) {
alert(response.Message);
},
dataType: 'json'
});
});
</script>
结果Json:
{"Sales":[
{"Year":"2011", "loc":"ca"},
{"Year":"2014", "loc":"wa"},
{"Year":"2011", "loc":"wi"}
]}
编辑:或者我可以在Razor代码中使用jquery结果组?
发布于 2016-11-30 10:18:15
正如@Stephen所说,这种解决方案是不可能的,但是,也许您可以尝试使用一个部分视图在服务器中呈现您的图形,然后在您的网页中插入ajax。
就像这样:
<div>
your page content
<div id="graph">
</div>
</div>
剧本:
$("#graph").load("MyAction", { yourdata }, function() { yourcallback });
在“MyAction”中:
public ActionResult MyAction( ... ) {
...
return PartialView(Model);
};
因此,在本例中,在“MyAction”部分视图中,您可以生成图形。
发布于 2016-11-30 10:41:41
无法从javascript更新C#对象(即chartImage)。
但是,在调用服务器获取Json数据时,您可以做一件事。因此,您可以直接获得图表,而不是获取数据。
以下是代码:-在HomeController中
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult GetSales()
{
var chartImage = new Chart(600, 400);
chartImage.AddTitle("Count");
chartImage.AddSeries(
chartType: "Pie",
name: "Sales",
axisLabel: "Count",
xValue: new[] { "2011", "2014" },// Make change based on your Json Data
yValues: new[] { "40", "285" }// Make change based on your Json Data
).Write(format: "jpg");
return null;
}
}
Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<img src="/home/GetSales" />
发布于 2016-12-06 06:20:33
I have created chart in my application using below code
// On controller side
public void My_Chart(List<my_Class> objClass)
{
var myChart = new Chart(width: 400, height: 300, theme: ChartTheme.Blue)
.AddTitle("My Chart Title")
.AddLegend()
.AddSeries("Default",
xValue: objClass, xField: "FieldName",
yValues: objClass, yFields: "FieldName")
.ToWebImage("png");
// Charts is folder inside my project where image is got saved
myChart.Save("~/Charts/My_image", "jpeg");
}
// And .cshtml side code is as follows
<img src="../Charts/My_image.jpeg" />`enter code here`
Note: before image loads your controller side code must be initialised
https://stackoverflow.com/questions/40821381
复制相似问题