如何使用ChartHelper在饼图中显示每个饼图的值?我正在使用MVC3 / Razor语法。
尝试做这样的事情:
图片来自this tutorial,适用于MVC中的ChartHelper:
我的代码:
var bytes = new Chart(600, 300).AddSeries(
chartType: "pie",
legend: "Sales in Store per Payment Collected",
xValue: viewModel.SalesInStorePerPaymentCollected.XValues,
yValues: viewModel.SalesInStorePerPaymentCollected.YValues
)
.GetBytes("png");
return File(bytes, "image/png");
答案 0 :(得分:13)
我是通过使用System.Web.UI.DataVisualization.Charting.Chart
类来完成的。
以下是我的控制器中的代码:
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");
}
观点:
<img alt="alternateText" src="@Url.Action("Chart")" />
答案 1 :(得分:4)
我的解决方案归功于DaveShaw。需要更多的调整,但给了我大部分我需要的东西。
Chart chart = new Chart();
chart.ChartAreas.Add(new ChartArea());
chart.Series.Add(new Series("Data"));
chart.Legends.Add(new Legend("Stores"));
chart.Series["Data"].ChartType = SeriesChartType.Pie;
chart.Series["Data"]["PieLabelStyle"] = "Outside";
chart.Series["Data"]["PieLineColor"] = "Black";
for (int x = 0; x < viewModel.SalesInStorePerPaymentCollected.XValues.Length; x++)
{
int ptIdx = chart.Series["Data"].Points.AddXY(
viewModel.SalesInStorePerPaymentCollected.XValues[x],
viewModel.SalesInStorePerPaymentCollected.YValues[x]);
DataPoint pt = chart.Series["Data"].Points[ptIdx];
pt.LegendText = "#VALX: #VALY";
pt.LegendUrl = "/Contact/Details/Hey";
}
chart.Series["Data"].Label = "#PERCENT{P0}";
chart.Series["Data"].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
chart.Series["Data"].ChartType = SeriesChartType.Pie;
chart.Series["Data"]["PieLabelStyle"] = "Outside";
chart.Series["Data"].Legend = "Stores";
chart.Legends["Stores"].Docking = Docking.Bottom;
var returnStream = new MemoryStream();
chart.ImageType = ChartImageType.Png;
chart.SaveImage(returnStream);
returnStream.Position = 0;
return new FileStreamResult(returnStream, "image/png");
渲染到:
答案 2 :(得分:3)
我的答案和解决方案(与解释一起使用):
这适合使用.NET 4框架的MVC 4和MVC 3并添加对System.Web.DataVisualization.dll的引用而不是.net System.Web.Helpers,可以在以下位置找到DataVisualization.dll http://www.codeproject.com/Articles/125230/ASP-NET-MVC-Chart-Control
ChartApplication-&gt;外部参考
可以在那里找到有关DataVisualization的图表的更多信息。
没关系,它可以替换为:
public ActionResult Chart()
{
Chart chart = new Chart();
chart.ChartAreas.Add(new ChartArea());
chart.Series.Add(new Series("Data"));
chart.Legends.Add(new Legend("Stores"));
chart.Series["Data"].ChartType = SeriesChartType.Spline;
chart.Series["Data"].Points.AddXY(1.0, 5.0);
chart.Series["Data"].Points.AddXY(2.0, 9.0);
/*
chart.Series["Data"]["PieLabelStyle"] = "Outside";
chart.Series["Data"]["PieLineColor"] = "Black";
*/
/*
int ptIdx = chart.Series["Data"].Points.AddXY(1.0, 5.0);
DataPoint pt = chart.Series["Data"].Points[ptIdx];
pt.LegendText = "#VALX: #VALY";
pt.LegendUrl = "/Contact/Details/Hey";
*/
/*
chart.Series["Data"].Label = "#PERCENT{P0}";
chart.Series["Data"].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
chart.Series["Data"].ChartType = SeriesChartType.Pie;
chart.Series["Data"]["PieLabelStyle"] = "Outside";
chart.Series["Data"].Legend = "Stores";
chart.Legends["Stores"].Docking = Docking.Bottom;
*/
var returnStream = new MemoryStream();
chart.ImageType = ChartImageType.Png;
chart.SaveImage(returnStream);
returnStream.Position = 0;
return new FileStreamResult(returnStream, "image/png");
}
或(对于带有.net 4和System.Web.Helpers的mvc 4和mvc 3):
public ActionResult Chart()
{
var bytes = new Chart(600, 300).AddSeries(
chartType: "pie",
legend: "Sales in Store per Payment Collected",
xValue: new[] { "Jan", "Feb", "Mar", "Apr", "May" },
yValues: new[] { "20", "20", "40", "10", "10" }
)
.Write("png");
return null;
}
当然,您需要在.cshtml中添加以下内容:
<img src="/Home/Chart" alt="" />