Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:05:20+00:00 2026-05-28T06:05:20+00:00

I have the following data in a database table, Columns : Date, Hour(from 1

  • 0

I have the following data in a database table,
Columns: Date, Hour(from 1 to 24) and Temperature.

I want to create a line chart that will show in the x-axis the hours from 1 to 24 and in the Y-axis the Temperatures.

Now the tricky part is that I want to have different color lines for each date. So if the date is 2012-01-15 then I have a red line from 1 to 24 showing the variations in temperature. if the date is 2012-01-14 then a blue one also from 1 to 24.

To be clear I want to have multiple dates in one chart.

Here is some of code I have

        SqlConnection conn2 = new SqlConnection(@"connectionString");
        conn2.Open();

        SqlCommand cmd2 = new SqlCommand();
        cmd2.Connection = conn2;
        cmd2.CommandText = "SELECT * FROM observation WHERE day BETWEEN @fromDate AND @toDate ORDER BY day ASC, hour ASC ";
        cmd2.Parameters.AddWithValue("fromDate", dateTimePicker2.Value.Date);
        cmd2.Parameters.AddWithValue("toDate", dateTimePicker3.Value.Date);



        SqlDataReader reader = cmd2.ExecuteReader();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("dateRange");
        dt.Columns.Add("day");
        dt.Columns.Add("hour");
        dt.Columns.Add("temp");

        DataRow dr = dt.NewRow();
        int counter = 0;

        while (reader.Read())
        {
            if (reader[1].ToString() != dt.TableName)
            {
                if (dt.Rows.Count != 0 && dt.TableName != "dateRange")
                {
                    dt.AcceptChanges();
                    ds.Tables.Add(dt);
                    counter++;
                }
                dt = new DataTable(reader[1].ToString());
                dt.Columns.Add("day"):                    
                dt.Columns.Add("hour");
                dt.Columns.Add("temp");

            }
            dr = dt.NewRow();
            dr[0] = reader[2];
            dr[1] = reader[3];
            dt.Rows.Add(dr);
        }
        if (dt.Rows.Count != 0)
        {
            dt.AcceptChanges();
            ds.Tables.Add(dt);
        }
        else
        {
            lblError.Visible = false;
        }

        for (int i = 0; i < counter; i++)
        {
            chart1.Series[i].Name = i.ToString();
            chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series[i].BorderWidth = 5;
            if (i % 2 == 0)
            {
                chart1.Series[i].Color = Color.Red;
            }
            else
            {
                chart1.Series[i].Color = Color.Green;
            }

            chart1.DataSource = ds.Tables[i];
            chart1.Series[i].XValueMember = "hour";
            chart1.Series[i].YValueMembers = "temp";
        }

        chart1.Legends[0].Alignment = StringAlignment.Center;
        chart1.Legends[0].Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top;

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

        cmd2.Connection.Close();

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:05:21+00:00Added an answer on May 28, 2026 at 6:05 am

    A solution I found was to re format my dataSource. I created a DataTable with the first column being Hours and it goes from 1 to 24. the following columns are one for each day that I want to represent.

    There are two Queries. the first one is to know how many days are in the interval and to create the appropriate number of Columns and Series. The second Query is the one that brings in all the Data.

    Here is the Code:

            SqlConnection conn = new SqlConnection(connectionString);
    
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT day FROM observation WHERE day BETWEEN @from AND @to GROUP BY day";
            cmd.Parameters.AddWithValue("from", Convert.ToDateTime(dateFrom));
            cmd.Parameters.AddWithValue("to", Convert.ToDateTime(dateTo));
            cmd.Connection.Open();
    
            SqlDataReader rdr = cmd.ExecuteReader();
    
            DataTable dt = new DataTable("DATA");
            dt.Columns.Add("Hour");
    
            int days = 0;
            chart1.DataSource = dt;
            while (rdr.Read())
            {
                dt.Columns.Add(((DateTime)rdr[0]).ToString("yyyy-MM-dd"));
    
                if (days == 0)
                {
                    chart1.Series[days].Name = ((DateTime)rdr[0]).ToString("yyyy-MM-dd");
                }
                else
                {
                    chart1.Series.Add(((DateTime)rdr[0]).ToString("yyyy-MM-dd"));
                }
                chart1.Series[((DateTime)rdr[0]).ToString("yyyy-MM-dd")].XValueMember = "Hour";
                chart1.Series[((DateTime)rdr[0]).ToString("yyyy-MM-dd")].YValueMembers = ((DateTime)rdr[0]).ToString("yyyy-MM-dd");
                chart1.Series[((DateTime)rdr[0]).ToString("yyyy-MM-dd")].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                days++;
            }
            rdr.Close();
    
            DataRow dr = dt.NewRow();
            for (int i = 1; i < 25; i++)
            {
                dr = dt.NewRow();
                dr["Hour"] = i;
                dt.Rows.Add(dr);
            }
    
            cmd.CommandText = "SELECT * FROM observation WHERE day BETWEEN @from2 AND @to2 ORDER BY day ASC, hour ASC ";
            cmd.Parameters.AddWithValue("from2", Convert.ToDateTime(fromDate));
            cmd.Parameters.AddWithValue("to2", Convert.ToDateTime(toDate));
    
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                dt.Rows[((int)(rdr[2]) - 1)][(((DateTime)rdr[1]).ToString("yyyy-MM-dd"))] = rdr[3];   
            }
    
            cmd.Connection.Close();
            rdr.Close();
    
            dataGridView1.DataSource = dt;
    
            chart1.DataBind();
            chart1.Visible = true;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function that is pulling data from a database. The ajax
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have a MySQL table with the following columns: id(int), date (timestamp), starttime(varchar), endtime(varchar),
I have made the following code to retrive data from SQLite database. public Cursor
I have the following code that creates my table. I insert data into it
I have the following database structure: CREATE TABLE LookupTable ( PK UNIQUEIDENTIFIER PRIMARY KEY,
I am trying to extract numeric data from a database where the columns have
I have following data to save in database using php my data is :
I have the following data in my database (comma separated strings): word, test, hello
I have the following scenario: I have various user's data stored in my database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.