2011-05-17 57 views
4

有什麼方法可以使用圖表助手在圖表上顯示圖例嗎?ASP.NET MVC 3.0圖表助手顯示圖例

它默認不顯示出來,沒有物業,我可以說顯示,如果我在XML中指定的傳說:

<Legends> 
    <Legend _Template_=""All"" BackColor=""Black"" Docking=""Bottom"" Font=""Trebuchet MS, 8.25pt, style=Bold"" LegendStyle=""Row"" > 
    </Legend> 
    </Legends> 

它也不能正常工作。

圖助手public Chart AddLegend(string title = null, string name = null)中有方法;但是當我打電話時,我看不到圖表上的圖例,圖表也不顯示。

Chart chart = new System.Web.Helpers.Chart(width: 120, height: 300, theme: chartStyle: ChartTheme.Green.ToString()) 
       .AddLegend("title", "name") // not existed legends, that's why error. 
       .AddSeries(
        chartType: "Column" 
        legend: "Rainfall" 
        xValue: new[] { "jan", "feb", "mar", "apr", "may" }, 
        yValues: new[] { "20", "20", "40", "10", "10" }); 

.Getting錯誤:Series 'Series1' uses non-existing legend name 'Rainfall'. Set Legend property value to Default

如果我改變legend: "Rainfall"legend: "Default"得到同樣的錯誤。

我怎麼能讓這個傳說名稱存在?例如,在使用直接的System.Web.UI.DataVisualization.Charting情況下,它會尋找這樣的:

 chart.Legends.Add("Legend1"); 

     // show legend based on check box value 
     chart.Legends["Legend1"].Enabled = true; 

但如何實現與助手同樣的事情?

回答

10

您需要爲每個系列命名,然後調用不帶參數的AddLegend()來添加圖例。

var chart = new Chart(width: 600, height: 250) 
     .AddSeries(
      chartType: "column", 
      xValue: new [] { 2010, 2011, 2012 }, 
      yValues: new[] { 100, 125, 150 }, 
      name: "Forecasts") 
     .AddSeries(
      chartType: "column", 
      xValue: new[] { 2010, 2011, 2012 }, 
      yValues: new[] { 200, 225, 250 }, 
      name: "Actuals") 
     .AddLegend();