在 Core Asp.Net 的 Razor 视图中创建饼图



我一直在互联网上搜索指南,以帮助我从我的Asp Net Core项目中获得在Razor页面视图上运行的图表。问题是,到目前为止,我已经找到了使用 Angular 或以一定价格提供.js的网站。直到我偶然发现了一个教程并整理了一个代码,才发现

找不到类型或命名空间图表

代码如下所示:

public class DashboardController : Controller
{
public IConfiguration Configuration { get; }
public DashboardController(IConfiguration configuration)
{
Configuration = configuration;
}
public ActionResult Index()
{
string query = "SELECT Total_Releases, Completed_Releases FROM ReleaseStats":
string constr = Configuration["ConnectionStrings:DefaultConnection"];
List<ReleaseStatistics> chartData = new List<ReleaseStatistics>();
using (SqlConnection 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 ReleaseStatistics
{
TotalReleases = Convert.ToInt32(sdr["Total_Releases"]),
CompletedReleases = Convert.ToInt32(sdr["Completed_Releases"])
});
}
}
con.Close();
}
}
return View(chartData);
}
}

@model List<Intersection.Models.Statistics.ReleaseStatistics>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@{
var chart = new Chart(width: 500, height: 500, theme: ChartTheme.Yellow)
.AddTitle("Releases")
.AddSeries("Default", chartType: "Pie",
xValue: Model, xField: "Total Releases",
yValues: Model, yFields: "Completed Releases")
.Write();
}
</body>
</html>

因此 - 应该怎么做才能让我的视图能够看到Chart()方法?还有其他方法可以轻松地将图表添加到我的视图中吗?非常欢迎任何指向指南,教程的链接!虽然我觉得我已经把它们都吃掉了。

图表助手支持 asp.net 但不支持 asp.net 核心。

如果要在核心中创建饼图 asp.net。我建议你可以尝试使用Chart.js。

参考:

https://www.chartjs.org/docs/latest/charts/doughnut.html

https://www.c-sharpcorner.com/article/creating-charts-with-asp-net-core/

最新更新