[英]Values in a pie chart
如何使用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");
我是通過使用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")" />
我的解決方案歸功於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");
呈現給此:
我的答案和解決方案(與解釋一起使用):
這適合MVC 4和MVC 3與.NET 4框架並添加對System.Web.DataVisualization.dll的引用而不是.net System.Web.Helpers,DataVisualization.dll可以在http://www.codeproject找到.COM /用品/ 125230 / ASP-NET-MVC-表-控制
ChartApplication->外部參考
可以在那里找到有關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="" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.