2011-05-17 56 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();