使用MVC、.net c#显示图表
本文关键字:显示图 net MVC 使用 | 更新日期: 2023-09-27 18:24:42
我试图在我的mvc应用程序上显示一些图表,但我遇到了一些错误。我在本地主机上开发。我有一个名为ReportChart 的cshtml文件
@{
var myChart = new Chart(width: 600, height: 400)
.AddTitle("Chart Title")
.AddSeries(
name: "Employee",
xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
yValues: new[] { "2", "6", "4", "5", "3" })
.Write();
}
以及另一个使用该图表的文件:
<body>
<h1>Chart Example</h1>
<p>The following chart is generated by the <em>ReportChart.cshtml</em> file:</p>
<p><img src="ReportChart.cshtml" alt="Cricketers" /> </p>
唯一的问题是网页没有显示任何图像:/
不,它在MVC中不起作用。您应该在控制器操作方法中创建图表:
//I CUT THE CODE WHERE I construct string[] t1 and int[] t2 these are just arrays
public ActionResult EfficiencyChart(string pid) {
var myChart = new Chart(width: 1000, height: 600)
.AddTitle("Employee's Efficiency")
.AddSeries(
name: "Employee",
xValue: t2,
yValues: t1)
.Write();
myChart.Save("~/Content/chart" + user.Id, "jpeg");
// Return the contents of the Stream to the client
return base.File("~/Content/chart" + user.Id, "jpeg");
}
然后在Razor视图中:
<img src="@Url.Action("EfficiencyChart", "NAME_OF_THE_CONTROLLER", new { pid = @Model.Id })" />
试试吧,
<p><img src="@Url.Action("ReportChart")" alt="Cricketers" /> </p>
public ActionResult ReportChart()
{
return PartialView();
}