2011-12-06 49 views
3

如何使用ChartHelper顯示餅圖中每個餅圖的值?我正在使用MVC3/Razor語法。餅圖中的值

試圖做這樣的事情:

Pie

的影像從this tutorial爲ChartHelper在MVC:

我的代碼:

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

你也可以通過結合客戶端和服務器端來實現那個鏡像。拖放圖表控件,使用html和css進行定位。設置Series CustomProperties =「PieLineColor = Black,PieLabelStyle = Outside」,根據需要自定義Points,然後通過調用「Chart.Series [0] .Points [0] .SetValueY(value )'來設置每個餅圖片的值和'Chart1.Series [0] .Points [0] .Label = value'來設置外部標籤文本。不知道你如何分開餡餅。也許玩xAxis – Jcorretjer

回答

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")" /> 
+0

我的圖表來自System.Web.Helpers命名空間。它看起來像你的圖表來自System.Web.UI.DataVisualization.Charting。如何將System.Web.UI.DataVisualization.Charting.Chart返回到MVC視圖? – Anish

+1

@Anish你說得很對,對不起。我已更新我的示例以包含完整的控制器方法和視圖。 – DaveShaw

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"); 

呈現這樣的:

Labels blackened for privacy

3

我的答案和解決方案(與解釋工作):

這適合MVC 4和MVC 3 .NET 4框架,並添加引用System.Web.DataVisualization.dll而不是.net System.Web.Helpers,DataVisualization.dll可以在 http://www.codeproject.com/Articles/125230/ASP-NET-MVC-Chart-Control

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"); 
    } 

或(對於MVC 4和MVC 3在.NET 4和System.Web.Helpers):

 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="" />