Ani*_*ish 3 c# asp.net-mvc mschart asp.net-mvc-3
如何使用ChartHelper在饼图中显示每个饼图的值?我正在使用MVC3/Razor语法.
试着这样做:

该图片来自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");
Run Code Online (Sandbox Code Playgroud)
Dav*_*haw 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");
}
Run Code Online (Sandbox Code Playgroud)
观点:
<img alt="alternateText" src="@Url.Action("Chart")" />
Run Code Online (Sandbox Code Playgroud)
我的解决方案感谢 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");
Run Code Online (Sandbox Code Playgroud)
呈现为:

| 归档时间: |
|
| 查看次数: |
43339 次 |
| 最近记录: |