软件学习记录(8)数据统计与报表功能
软件学习记录(8)数据统计与报表功能
(一)数据统计
这个demo就很粗糙。简单说一下。需要首先选择年份与月份,然后选择报表类型(年度统计表,季度统计表,月度统计表),最后点击"报表查询"。
代码部分,过于粗糙,就不进行演示了。
(二)数据报表
重点讲解图的绘制:
思路:使用了第三方控件 zedGraphControl。以下是月度统计信息(先使用sql读取数据库,获取每一天的成功或者失败产量加入Result的list中,然后绘图中读取把每一天数据绘制到表格上)。
public class Result
{
public int OKNumber { get; set; }
public int NGNumber { get; set; }
}
private void GetMonthData()
{
List<Result> result = new List<Result>();
string sql = string.Empty;
int YearAdd = CurrentYear - DateTime.Now.Year;
int MonthAdd = CurrentMonth - DateTime.Now.Month;
int monthday = GetCurrentMonthDays();
for (int i = 1; i <= monthday; i++)
{
sql = "Select OKNumber,NGNumber from Report where ReportDateTime='{0}'";
string format = "yyyy/MM/" + i.ToString("00") + " 00:00:00";
sql = string.Format(sql, DateTime.Now.AddYears(YearAdd).AddMonths(MonthAdd).ToString(format));
MySqlDataReader dr = MySQLHelper.GetReader(sql);
if (dr.HasRows)
{
while (dr.Read())
{
result.Add(new Result()
{
OKNumber = Convert.ToInt32(dr["OKNumber"]),
NGNumber = Convert.ToInt32(dr["NGNumber"])
});
}
}
else
{
result.Add(new Result()
{
OKNumber = 0,
NGNumber = 0
});
}
}
SetMonthDGV(result);
}
private void SetMonthDGV(List<Result> lstValue)
{
int dayCount = GetCurrentMonthDays();
if (lstValue.Count == dayCount)
{
//清空ZedGraph
this.zedGraphControl1.GraphPane.CurveList.Clear();
this.zedGraphControl1.GraphPane.GraphObjList.Clear();
//设置标题
this.zedGraphControl1.GraphPane.Title.Text = CurrentYear + "年第" + CurrentMonth.ToString() + "月度产量统计图";
list1 = new PointPairList();
list2 = new PointPairList();
List<string> XAxisNameList = new List<string>();
for (int i = 0; i < dayCount; i++)
{
double xAxis = i;
double y1Axis = lstValue[i].OKNumber;
double y2Axis = lstValue[i].NGNumber;
list1.Add(xAxis, y1Axis);
list2.Add(xAxis, y2Axis);
XAxisNameList.Add((i+1)+ "日");
}
//显示曲线
//设置示例的字体
this.zedGraphControl1.GraphPane.Legend.FontSpec.Family = "微软雅黑";
this.zedGraphControl1.GraphPane.Legend.FontSpec.Size = 10;
this.zedGraphControl1.GraphPane.AddBar("合格", list1, Color.Blue);
this.zedGraphControl1.GraphPane.AddBar("不合格", list2, Color.Red);
//设置X轴显示名称
this.zedGraphControl1.GraphPane.XAxis.Scale.TextLabels = XAxisNameList.ToArray();
this.zedGraphControl1.GraphPane.XAxis.Type = AxisType.Text;
this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Family = "微软雅黑";
this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 10;
this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Family = "微软雅黑";
this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 10;
this.zedGraphControl1.GraphPane.YAxis.Scale.MaxAuto = true;
this.zedGraphControl1.AxisChange();
this.zedGraphControl1.Refresh();
}
else
{
MessageBox.Show("查询数据数量不满足,请检查!", "曲线查询");
}
}