在一个图表

时间:2015-05-05 07:27:46

标签: c# winforms datetime merge mschart

我有一系列的能量消耗十进制值,每15分钟一整年。这个数据来自过去。
现在预测未来的能源消耗。但是,这里我只有一年中每小时的价值 我想重叠这两个系列来看看差异。由于第二个系列中缺少值,因此没有一致的线。这条线刚刚破裂。

这是我添加第一个系列数据的地方。

dtlist.Add(new DataTable(tableIST));                            
dtlist[0].Columns.Add("Date");            
dtlist[0].Columns.Add("Volume1");
dtlist[0].PrimaryKey = new DataColumn[] { dtlist[0].Columns[0] };
DataRow dr;
Series default2 = new Series("Default");

using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
{                
    string query = "SELECT TIMESTAMP, LAST FROM " + tableIST;
    connection.Open();
    int i = 0;
    decimal sum = 0;
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {                            
                    dr = dtlist[0].NewRow();
                    dr["Date"] = reader.GetDateTime(0);
                    dr["Volume1"] = reader.GetDecimal(1).ToString().Replace(',', '.');                                
                    dtlist[0].Rows.Add(dr);
                    default2.Points.AddY(reader.GetDecimal(1).ToString().Replace(',', '.'));                                
                }                            
            }
        }
    }
    connection.Close();
}
chart1.DataSource = dttemp = dtlist[0];

这是我添加第二个系列数据并将其合并的地方。

using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
{
    string query = "SELECT TIMESTAMP, LAST FROM " + tableName;                        
    connection.Open();
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                dr = dtlist[count].NewRow();
                dr["Date"] = reader.GetDateTime(0).Subtract(TimeSpan.FromDays(365));
                dr["Volume2"] = reader.GetDecimal(1).ToString().Replace(',', '.');
                dtlist[count].Rows.Add(dr);
            }
        }
    }
    connection.Close();
}

//merge data in one datatable for displaying

dttemp.Merge(dtlist[1]);

这是我的图表现在看起来的图片。
蓝线是第一个数据系列,黄线是第二个。
chart1

看起来应该是这样的。
chart2

2 个答案:

答案 0 :(得分:1)

在第一个系列中,每4个值(4,15分钟值)得到平均值(这将给出平均小时值),现在你可以绘制图表

答案 1 :(得分:0)

这是我惯用的代码。

                DateTime helpDate;
                Decimal helpNumber = 0, helpNumber2, difference;
                DataTable dthelp = new DataTable();
                dthelp.Columns.Add("Date");
                dthelp.Columns.Add("Volume" + CountPlusOne);
                dthelp.PrimaryKey = new DataColumn[] { dthelp.Columns[0] };
                int i = 0;
                foreach (DataRow row in dtlist[count].Rows)
                {
                    if (i > 0)
                    {
                        helpNumber2 = Decimal.Parse(row["Volume" + CountPlusOne].ToString().Replace('.', ','));
                        difference = (helpNumber - helpNumber2) / 4;
                        helpDate = DateTime.Parse(row["Date"].ToString());

                        dr = dthelp.NewRow();
                        dr["Date"] = helpDate.Subtract(TimeSpan.FromMinutes(45));
                        dr["Volume" + CountPlusOne] = (helpNumber - difference).ToString().Replace(',', '.');                            
                        dthelp.Rows.Add(dr);

                        dr = dthelp.NewRow();
                        dr["Date"] = helpDate.Subtract(TimeSpan.FromMinutes(30));
                        dr["Volume" + CountPlusOne] = (helpNumber - difference * 2).ToString().Replace(',', '.');                            
                        dthelp.Rows.Add(dr);

                        dr = dthelp.NewRow();
                        dr["Date"] = helpDate.Subtract(TimeSpan.FromMinutes(15));
                        dr["Volume" + CountPlusOne] = (helpNumber - difference * 3).ToString().Replace(',', '.');
                        dthelp.Rows.Add(dr);
                    }
                    i++;
                    helpNumber = Decimal.Parse(row["Volume" + CountPlusOne].ToString().Replace('.', ','));
                }