MS Chart 折线图——去除时间中的时、分、秒,按天统计
private void Form2_Load(object sender, EventArgs e)
{
string sql1 = "select 关键词,时间,相同条数 from 条目表";
OleDbCommand cmd1 = new OleDbCommand(sql1, conn);
conn.Open();
string sql0 = "delete from 统计表"; //将表中原有数据删除,即初始化总词频表
OleDbCommand cmd0 = new OleDbCommand(sql0, conn); //定义SqlCommand变量
cmd0.ExecuteNonQuery();
OleDbDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
if (dr1["关键词"].ToString() != "" && !comboBox1.Items.Contains(dr1["关键词"].ToString()))
comboBox1.Items.Add(dr1["关键词"].ToString());
string sql2 = "";
if (dr1["时间"].ToString() != "")
{
if (dr1["相同条数"].ToString() != "")
sql2 = "insert into 统计表(关键词,时间,相同条数) values('" + dr1["关键词"].ToString() + "' ,'" + Convert.ToDateTime(dr1["时间"].ToString()).ToShortDateString() + "'," + Convert.ToInt64(dr1["相同条数"]) + ")";
else
sql2 = "insert into 统计表(关键词,时间,相同条数) values('" + dr1["关键词"].ToString() + "' ,'" + Convert.ToDateTime(dr1["时间"].ToString()).ToShortDateString() + "',0)";
OleDbCommand cmd2 = new OleDbCommand(sql2, conn);
cmd2.ExecuteNonQuery();
}
}
dr1.Close();
conn.Close();
this.comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
chart1.Series["不带转载发文量"].Points.Clear();
chart1.Series["带转载发文量"].Points.Clear();
string d_before = DateTime.Now.ToShortDateString();
string w_before = DateTime.Now.AddDays(-30).ToShortDateString();
string sql = "select 时间,count(*) as 条数 from 统计表 where 关键词='" + comboBox1.SelectedItem.ToString() + "' and 时间 between #" + w_before + "# and #" + d_before + "# group by 时间 order by 时间";
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader rd = cmd.ExecuteReader();
time.Clear();
while (rd.Read())
{
if (rd["时间"].ToString() != "")
{
chart1.Series["不带转载发文量"].Points.AddXY(Convert.ToDateTime(rd["时间"]).ToShortDateString(), rd["条数"]);
time.Add(Convert.ToDateTime(rd["时间"]).ToShortDateString());
}
}
rd.Close();
conn.Close();
for (int i = 0; i < time.Count; i++)
{
string sql2 = "select 相同条数 from 统计表 where 关键词='" + comboBox1.SelectedItem.ToString() + "' and 时间 like '%" + time[i] + "%'";
OleDbCommand cmd2 = new OleDbCommand(sql2, conn);
conn.Open();
OleDbDataReader dr = cmd2.ExecuteReader();
int total = 0;
while (dr.Read())
{
int s2 = 0;
if (dr["相同条数"].ToString() == "")
{
s2 = 0;
}
else
s2 = Convert.ToInt32(dr["相同条数"]);
total += s2 + 1;
}
dr.Close();
conn.Close();
chart1.Series["带转载发文量"].Points.AddXY((time[i]), total);
}
}