Search code examples
c#winformsdatetimemergemschart

displaying two or more data series (some with empty values) in one chart


I'm having a series with decimal values of energy consumption for every 15 minutes for one full year.This data is from the past.
Now a prediction of the future energy consumption is made. However, here I only have values for every hour for a year.
I want to overlay these two series to see the difference. Because of the missing values in the second series, there is no consistent line. The line just breaks of.

Here is where I add the first series data.

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];

Here is where I add the second series data and merge it.

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]);

Here is a picture how my chart looks now.
The blue line is the first data series and the yellow line is the second.
chart1

It should look something like this.
chart2


Solution

  • in the first series for every 4 values (4, 15min values) make the average (that will give you the average hour value) and now you can draw the chart