为一周中的每一天创建一个包含各条线的折线图
本文关键字:一个 包含各 折线图 一周 每一天 创建 | 更新日期: 2023-09-27 18:20:18
我在数据库表中有以下数据,列:日期、小时(从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();
谢谢!
为了创建多行,你必须创建多个系列,但我不确定你使用的是什么类型的图表。这里是一个关于图表的非常好的教程
https://web.archive.org/web/20211020203246/https://www.4guysfromrolla.com/articles/072209-1.aspx
Chart1.Series[0].Points.FindMinByValue().Color = Color.Red;
这应该会改变你想要的每个点的颜色。
我找到的一个解决方案是重新格式化我的dataSource。我创建了一个DataTable,第一列是Hours,它从1到24。以下列是我想要表示的每一天的一列。
有两个查询。第一个是知道间隔中有多少天,并创建适当数量的Columns和Series。第二个查询是引入所有数据的查询。
这是代码:
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;
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