我试图显示一个带有一些示例数据的示例图表,只是按照 satckoverflow 中的一个链接,在我的应用程序中尝试了该代码,但由于“名称数据确实存在于当前上下文中”而出现错误。
public ActionResult Chart()
{
Chart chart = new Chart();
chart.ChartAreas.Add(new ChartArea());
chart.Series.Add(new Series("Data"));
chart.Series["Data"].ChartType = SeriesChartType.Pie;
chart.Series["Data"]["PieLabelStyle"] = "Outside";
chart.Series["Data"]["PieLineColor"] = "Black";
chart.Series["Data"].Points.DataBindXY(
data.Select(data => data.Name.ToString()).ToArray(),
data.Select(data => data.Count).ToArray());
//Other chart formatting and data source omitted.
MemoryStream ms = new MemoryStream();
chart.SaveImage(ms, ChartImageFormat.Png);
return File(ms.ToArray(), "image/png");
}
The view is
<img alt="" src="@Url.Action("Chart")" />