为一周中的每一天创建包含单独行的折线图?

时间:2012-01-16 02:38:51

标签: c# winforms visual-studio-2010 charts

我在数据库表中有以下数据, :日期,小时(从1到24)和温度。

我想创建一个折线图,在x轴上显示从1到24的小时数,在Y轴上显示温度。

现在棘手的部分是我希望每个日期都有不同的颜色线。因此,如果日期是2012-01-15,那么我有一条从1到24的红线显示温度的变化。如果日期是2012-01-14,那么蓝色也是1到24。

要明确我希望在一个图表中有多个日期。

以下是我的一些代码

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

谢谢!

3 个答案:

答案 0 :(得分:1)

为了创建多行,你必须创建多个系列,但我不确定你在这里使用什么类型的图表是一个关于图表的非常好的教程

http://www.4guysfromrolla.com/articles/072209-1.aspx

Chart1.Series[0].Points.FindMinByValue().Color = Color.Red; 这应该改变你想要的每个点的颜色。

答案 1 :(得分:1)

我找到的解决方案是重新格式化我的dataSource。我创建了一个DataTable,第一列是Hours,它从1到24.下面的列是我想要表示的每一天。

有两个查询。第一个是知道间隔中有多少天,并创建适当数量的列和系列。第二个查询是引入所有数据的查询。

以下是代码:

        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;

答案 2 :(得分:0)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dtTest.Columns.Add("TimePoint", GetType(Integer))
dtTest.Columns.Add("Speed", GetType(Integer))

dtTest.Rows.Add(0, 0)
dtTest.Rows.Add(1000, 50)
dtTest.Rows.Add(2000, 50)
dtTest.Rows.Add(3000, 0)

With Chart1.ChartAreas(0)
  .AxisX.Minimum = 0
  .AxisX.Maximum = 3000
  .AxisY.Minimum = 0
  .AxisY.Maximum = 60
  .AxisY.Interval = 10
  .AxisX.Title = "Elapsed Time (ms)"
  .AxisY.Title = "Speed (km/hr)"
End With
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    With Chart1.Series(0)
      .Points.DataBind(dtTest.DefaultView, "TimePoint", "Speed", Nothing)
      .ChartType = DataVisualization.Charting.SeriesChartType.Line
      .BorderWidth = 4
    End With
End Sub