C#Charts从datatable添加多个系列 [英] C# Charts add multiple series from datatable

查看:90
本文介绍了C#Charts从datatable添加多个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的数据库检索几个数据,大小不同。这2个只是一个例子。

I retrieve several datatables from my DB, which vary in size. This one of 2 is just an example.

在这里查看结构!



我设法创建了2不同的系列,并让他们出现在传奇。

See the structure here!

I managed to create the 2 different series and have them show up on the legend.

我的问题是如何将数据绑定到相应的系列。
系列名称是从doman_namn列创建的,系列数量是从包含唯一URL数量的antal列创建的。

My question is on how to bind that data to the respective series. The series name are created from column doman_namn and the amount of series are created from the "antal" column which holds the number of unique URLS.

QUESTION
如何将ADDY和ADDX加入现在失败的图表。

QUESTION HOW TO BIND ADDY and ADDX to the chart it fails now.

这是我的代码到目前为止...

This is my code so far...

 Chart1.DataSource = dt;

        int amountofrows = Convert.ToInt32(dt.Rows[0]["antal"].ToString());

        for (int i = 0; i < amountofrows; i++)
        {
            string serieName = dt.Rows[i]["doman_namn"].ToString();

            Chart1.Series.Add(serieName);
            Chart1.Series[i].ChartType = SeriesChartType.Line;

            foreach(DataRow dr in dt.Rows)
            {
                try
                {
                    if (String.Equals(serieName,dr["doman_namn"].ToString(), StringComparison.Ordinal))     
                    {
                    Chart1.Series[serieName].Points.AddY(Convert.ToDouble(dr["ranking_position"]));
                    Chart1.Series[serieName].Points.AddY(Convert.ToDouble(dr["ranking_date"]));
                    }
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Failed when adding points");
                }
            }
        }


        Chart1.DataBind();
        Chart1.Visible = true;

来自GREGOR的帮助

CODE AFTER HELP FROM GREGOR

        for (int i = 0; i < amountofrows; i++)
        {
            string serieName = dt.Rows[i]["doman_namn"].ToString();

            Chart1.Series.Add(serieName);
            Chart1.Series[i].ChartType = SeriesChartType.Line;

            Chart1.Series[serieName].XValueMember = "ranking_date";
            Chart1.Series[serieName].YValueMembers = "ranking_position";

    }
     Chart1.DataBind();


推荐答案

Okay I managed to do it myself! But you Gregor Primar pushed me in the right direction!!

重要的是设置X轴和Y轴的值类型。因为十进制类型不是一个选项,我使用auto作为类型。

What was important was that you set the valuetype for the X and Y-axis. As decimal type was not an option I used auto as type.

        Chart1.DataSource = dt;

        int amountofrows = Convert.ToInt32(dt.Rows[0]["antal"].ToString());

        for (int i = 0; i < amountofrows; i++)
        {
            List<string> xvals = new List<string>();
            List<decimal> yvals = new List<decimal>();
            string serieName = dt.Rows[i]["doman_namn"].ToString();
            Chart1.Series.Add(serieName);
            Chart1.Series[i].ChartType = SeriesChartType.Line;

            foreach(DataRow dr in dt.Rows)
            {
                try
                {


                if (String.Equals(serieName,dr["doman_namn"].ToString(), StringComparison.Ordinal))     
                {                    
                    xvals.Add(dr["ranking_date"].ToString());
                    yvals.Add(Convert.ToDecimal(dr["ranking_position"].ToString()));              
                }

                }
                catch (Exception)
                {

                    throw new InvalidOperationException("Diagrammet kunde inte ritas upp");
                }
            }
            try
            {
                Chart1.Series[serieName].XValueType = ChartValueType.String;
                Chart1.Series[serieName].YValueType = ChartValueType.Auto;
                Chart1.Series[serieName].Points.DataBindXY(xvals.ToArray(), yvals.ToArray());
            }
            catch (Exception)
            {

                throw new InvalidOperationException("Kunde inte bind punkterna till Diagrammet");
            }    
        }

        Chart1.DataBind();
        Chart1.Visible = true;

这篇关于C#Charts从datatable添加多个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆