ASP.NET MVC 3.0图表帮助程序显示图例。

5

有没有办法使用图表助手在图表上显示图例?

默认情况下它不会显示,我也无法指定属性来显示。如果我在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" });

出现错误: Series 'Series1' 使用不存在的图例名称 'Rainfall'。将 Legend 属性值设置为 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;

但是如何使用helper实现相同的功能呢?
1个回答

11

您需要为每个序列指定一个名称,然后调用 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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接