【问题标题】:One column GridView data to multiple chart series一列GridView数据到多个图表系列
【发布时间】:2019-03-18 17:28:14
【问题描述】:

我想从同一个 gridview 列创建一个包含多个系列的图表。

我的 GridView 数据(来自存储过程):

Name  |  Month   |  Volume
A     |  2019-01 |  1400
B     |  2019-01 |  1100
B     |  2019-02 |   400
C     |  2019-01 |  6500
B     |  2019-03 |  6500
A     |  2019-02 |   500
C     |  2019-02 |    35

我想在轴上添加一个带有卷/名称的图表,并为每个月添加一个单独的系列,例如 2019-01 的一个系列,2019-02 的另一个系列。

对于上面的示例,我将有三个系列。

我是 ASP.NET 的新手,我很高兴知道如何实现这一目标。提前感谢您的帮助!

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您首先必须按月份对数据进行分组。我在下面的代码中通过创建一个数据透视表并按名称对数据进行分组来做到这一点。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    
    namespace ConsoleApplication106
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Month", typeof(DateTime));
                dt.Columns.Add("Volume", typeof(int));
    
                dt.Rows.Add(new object[] {"A", DateTime.ParseExact("2019-01", "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), 1400});
                dt.Rows.Add(new object[] { "B", DateTime.ParseExact("2019-01", "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), 1100 });
                dt.Rows.Add(new object[] { "B", DateTime.ParseExact("2019-02", "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), 400 });
                dt.Rows.Add(new object[] { "C", DateTime.ParseExact("2019-01", "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), 6500 });
                dt.Rows.Add(new object[] { "B", DateTime.ParseExact("2019-03", "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), 6500 });
                dt.Rows.Add(new object[] { "A", DateTime.ParseExact("2019-02", "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), 500 });
                dt.Rows.Add(new object[] { "C", DateTime.ParseExact("2019-02", "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), 35 });
    
                DateTime[] dates = dt.AsEnumerable().Select(x => x.Field<DateTime>("Month")).Distinct().OrderBy(x => x).ToArray();
    
                DataTable pivot = new DataTable();
                pivot.Columns.Add("Name", typeof(string));
                foreach (DateTime date in dates)
                {
                    pivot.Columns.Add(date.ToString("yyyy-MM"), typeof(int));
                }
    
    
                var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("Name")).ToList();
    
                foreach(var group in groups)
                {
                    DataRow newRow = pivot.Rows.Add();
                    newRow["Name"] = group.Key;
                    foreach(DataRow row in group)
                    {
                        newRow[row.Field<DateTime>("Month").ToString("yyyy-MM")] = row.Field<int>("Volume");
                    }
    
                }
    
    
            }
    
        }
    
    
    }
    

    【讨论】:

    • 谢谢jdweng!非常清楚。实际上数据在GridView中。如何将 GridView 数据收集到 dt?
    • 您不需要使用数据表。您可以将输出分组到任何类型的对象。请参阅以下有关在 asp.net 中读取 gridview 的方法的帖子:stackoverflow.com/questions/47887018/…
    • 非常感谢@jdwend!
    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多