我正在尝试绘制X(周),Y(燃料量)图,显示每个给定间隔的总燃料消耗量,以周为单位,但目前该图仅显示相对于天的燃料量(Y) (X)。如何最好地绘制出每周消耗的所有燃料的图表。下面是绘制当前图表
的代码 private void Load_BTN_Click(object sender, EventArgs e)
{
try
{
string selectQuery = "Select * from Fuel_Attendend";
cmd = new SqlCommand(selectQuery, connection);
try
{
connection.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
this.chart1.Series["Date"].Points.AddXY(dr.GetString(3), dr.GetInt32(6));
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:1)
如果要显示每周金额,最简单的方法是将sql更改为按周分组。像...
select sum(Fuel_Amount), DATEDIFF(week, '2018-01-01', Date) AS WeekNumber from Fuel_Attended group by DATEDIFF(week, '2018-01-01', Date)
在将点添加到系列1时,您必须将用作第一列和第二列的列更改为
。this.chart1.Series["Date"].Points.AddXY(dr.Int32(0), dr.GetInt32(1));
或者您可以使用
select sum(Fuel_Amount), DATEADD(week, DATEDIFF(week, '1950-01-01', Date), '1950-01-01') AS WeekNumber from Fuel_Attended group by DATEADD(week, DATEDIFF(week, '1950-01-01', Date), '1950-01-01')
这使您可以将星期表示为星期几的开始日期。而不只是数字。