【问题标题】:shield ui chart: generate series dynamically?屏蔽 ui 图表:动态生成系列?
【发布时间】:2014-04-12 14:20:15
【问题描述】:

有没有办法在不提前知道有多少系列的情况下将系列添加到图表中?例如,我想在 X 轴上按月绘制不同年份的图表。每一年都是一个系列。

我曾考虑在按年份分组时绑定到 IEnumerable,并为每个分组项创建一个系列,但我无法真正想象代码。

【问题讨论】:

    标签: c# asp.net-mvc-4 shieldui


    【解决方案1】:

    我让它与自定义助手(HtmlHelper 扩展方法)一起使用。我改编了来自http://demos.shieldui.com/aspnet/aspnet-chart/programmatic-chart-creation 的示例,但该示例适用于 ASP.NET Web 表单。我正在做MVC。我的解决方案涉及在帮助程序中使用 ChartBuilder 对象。

    public static class ChartExtensions
    {
        public static ChartBuilder<PropertyPivotAverageAmountPerMonth_Result> AnnualComparison(this HtmlHelper html, int id)
        {
            MyDatabaseEntities db = new MyDatabaseEntities();
            var results = db.PropertyPivotAverageAmountPerMonth(id);
            ChartBuilder<PropertyPivotAverageAmountPerMonth_Result> chart = new ChartBuilder<PropertyPivotAverageAmountPerMonth_Result>(html, results);
    
            chart.AxisX(axisX => axisX.CategoricalValues("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"));
            chart.PrimaryHeader(header => header.Text("Annual Cost Comparisons"));
    
    
            foreach (var year in results)
            {
                chart.DataSeries(s => s.Line()
                    .CollectionAlias(year.Year.ToString())
                    .Data(new object[]
                        {
                            new { x=0, y=year.C1 },
                            new { x=1, y=year.C2 },
                            new { x=2, y=year.C3 },
                            new { x=3, y=year.C4 },
                            new { x=4, y=year.C5 },
                            new { x=5, y=year.C6 },
                            new { x=6, y=year.C7 },
                            new { x=7, y=year.C8 },
                            new { x=8, y=year.C9 },
                            new { x=9, y=year.C10 },
                            new { x=10, y=year.C11 },
                            new { x=11, y=year.C12 }
                        }));
            }
            return chart;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      相关资源
      最近更新 更多