weixin_42853935
2019-07-31 09:32
采纳率: 100%
浏览 1.3k

C# 自带控件chart多条曲线显示方法(连接sql动态获取)

现在有如下sql-table month代表月份、item代表产品,qty代表出货的重量

图片说明

请教各位大神如何用 c#的chart控件结合sql 将以下数据结合成这样

图片说明

需求如上图就是
按数据库中的每个月份(month) 得出每个 产品(item)每个月出货的重量(QTY)可能部分产品某个月份没有出货数据,那就空着,不用绘制那个月的,其他有的都要绘制,主要需求难点还是在多个产品绘制图形上面,单个的或者写死的都没问题,动态实现这部分主要是小弟的难点。不知可否有大大帮忙贴上c# 代码,感激不尽,sql语句部分为

SELECT [month],[item],[qty] FROM [YFYCRM].[dbo].[2018-2019-ship] GROUP BY [month],[item] order BY [month],[item] asc

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 听雨停了 2019-08-01 10:00
    已采纳
    private void button1_Click(object sender, EventArgs e)
            {
    
                chart1.Series.Clear();
                //模拟你的原始数据
                var list = new List<Test>(){
                    new Test{ month = 1, item = "A001", qty = 1200 },
                    new Test{ month = 2, item = "A001", qty = 1100 },
                    new Test{ month = 3, item = "A001", qty = 1800 },
                    new Test{ month = 4, item = "A001", qty = 600 },
                    new Test{ month = 5, item = "A001", qty = 200 },
                    new Test{ month = 1, item = "B001", qty = 1000 },
                    new Test{ month = 2, item = "B001", qty = 1500 },
                    new Test{ month = 3, item = "B001", qty = 100 },
                    new Test{ month = 4, item = "B001", qty = 900 },
                    new Test{ month = 5, item = "B001", qty = 900 },
                    new Test{ month = 1, item = "C001", qty = 2000 },
                    new Test{ month = 2, item = "C001", qty = 1800 },
                    new Test{ month = 6, item = "C001", qty = 2500 }
                };
                DataTable dt = ToDataTable(list);   //把你的SQL语句结果替换掉ToDataTable(list)             
                DataTable dtConvert = ConvertDataTable(dt);
                var allSeries = dt.AsEnumerable().Select(a => a["item"]).Distinct();
                foreach (var item in allSeries)
                {
                    Series series = new Series(item.ToString());
                    chart1.Series.Add(series);
                }
                for (int i = 0; i < allSeries.Count(); i++)
                {
                    chart1.Series[i].YValueMembers = chart1.Series[i].Name;
                    chart1.Series[i].XValueMember = "month";
                }
                chart1.DataSource = dtConvert;                       
                chart1.DataBind();
    
    
            }
    
    static DataTable ConvertDataTable(DataTable source)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("month");
                var columns = (from x in source.Rows.Cast<DataRow>() select x[1].ToString()).Distinct();
                foreach (var item in columns) dt.Columns.Add(item);
                var data = from x in source.Rows.Cast<DataRow>()
                           group x by x[0] into g
                           select new { Key = g.Key.ToString(), Items = g };
                data.ToList().ForEach(x =>
                {
                    string[] array = new string[dt.Columns.Count];
                    array[0] = x.Key;
                    for (int i = 1; i < dt.Columns.Count; i++)
                        array[i] = (from y in x.Items
                                    where y[1].ToString() == dt.Columns[i].ToString()
                                    select y[2].ToString())
                                   .SingleOrDefault();
                    dt.Rows.Add(array);
                });
                return dt;
            }
    

    图片说明

    已采纳该答案
    评论
    解决 5 无用
    打赏 举报
  • lyn5284767 2019-07-31 09:55

    //画图柱形图的条数决定是由数据源也就Series决定。Series是对象,动态创建即可。
    Series s1 = new Series();
    Series s2 = new Series();
    Series s3 = new Series();
    Random r = new Random();
    for (int i = 1; i < 13; i++)
    {
    //绑定数据,第一个参数i未X轴数据,第二个参数r.Next(20, 30)为Y轴参数。你用数据库查出来的数据对应绑定就行
    s1.Points.AddXY(i, r.Next(20, 30));
    s2.Points.AddXY(i, r.Next(10, 30));
    s3.Points.AddXY(i, r.Next(20, 30));
    }
    //指定柱形条的颜色
    s1.Color = Color.Green;
    s2.Color = Color.Red;
    s3.Color = Color.Black;
    //加入到chart1中
    //X轴上网格
    chart1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;
    //y轴上网格
    //ct.ChartAreas[0].Axes[1].MajorGrid.Enabled = false;

    chart1.Series.Add(s1);
    chart1.Series.Add(s2);
    chart1.Series.Add(s3);

    评论
    解决 无用
    打赏 举报
  • 小达哥的垃圾桶 2019-07-31 12:04

    循环获取,然后放进集合里面。

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题