需求:在ASP.NET中,从数据库中导出数据,规定时间内预测出下一个数据,然后蒋数据绘制折线图。

代码:

    public partial classWare1 : System.Web.UI.Page
{
static string s = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["User"].ToString();
DataClasses1DataContext ds
= newDataClasses1DataContext(s);protected void Page_Load(objectsender, EventArgs e)
{
//页面加载时,就开始显示该仓库中存在的货物信息 DengJi DJ = newDengJi();
WareHouse ware
= newWareHouse();var num = from ware1 in ds.WareHouse where ware1.Wnumber == 1 && ware1.Snumber % 2 == 0 selectware1;if (num.Count() > 0)
{
SqlConnection sd
= newSqlConnection(s);string str2 = "select Id,class,company,realnumber,accept_time,nphone,Snum,quality from Dengji where Wnum =" + 1+ "and Status = '在库'";
SqlDataAdapter sda
= newSqlDataAdapter(str2, sd);
DataSet ds
= newDataSet();
sda.Fill(ds,
"DengJi");
GridView1.DataSource
= ds.Tables[0].DefaultView;
GridView1.DataBind();
GridView1.HeaderRow.Cells[
0].Text = "ID";
GridView1.HeaderRow.Cells[
1].Text = "种类";
GridView1.HeaderRow.Cells[
2].Text = "公司名称";
GridView1.HeaderRow.Cells[
3].Text = "入库数量";
GridView1.HeaderRow.Cells[
4].Text = "入库时间";
GridView1.HeaderRow.Cells[
5].Text = "客户电话";
GridView1.HeaderRow.Cells[
6].Text = "货架号";
GridView1.HeaderRow.Cells[
7].Text = "货损率";

}
else{

}

}
protected void Button1_Click(object sender, EventArgs e)//更改温度 、湿度、风速的值 {
Temple temple
= newTemple();var num1 =ds.Temple.Count();
temple.Id
= num1 + 1;
temple.xname
=System.DateTime.Now;
temple.ytem
= double.Parse(DropDownList1.SelectedItem.Text);
temple.yhumidity
= double.Parse(DropDownList2.SelectedItem.Text);
temple.ywind
= double.Parse(DropDownList3.SelectedItem.Text);
ds.Temple.InsertOnSubmit(temple);
ds.SubmitChanges();
}
public class GrayModel//灰色预测模型 {private doublea0, a1, a2;private int size;//定义数组长度 private doubleerror;publicGrayModel()
{

}
public void build(double[] Str)
{
size
= Str.Length;//获取数组Str长度 double[] Str1 = new double[size];//新建一个长度与以知数组长度一致的数组Str1 Str1[0] = Str[0];//把给定数组的第一个元素赋给数组Str1 for (int i = 1; i < size; i++)
{
Str1[i]
= Str[i] + Str1[i - 1];//获取新的数组值,也就是AGO计算 }double[,] a = new double[size - 1, 2];//生成二维数组a double[,] bt = new double[2, size - 1];//生成二维数组bt double[,] y = new double[size - 1, 1];//生成一维数组y for (int i = 0; i < a.GetLength(0); i++)//GetLength(0)是获取行数 {
a[i,
0] = -(Str1[i] + Str1[i + 1]) / 2;
a[i,
1] = 1;
bt[
0, i] = a[i, 0];
bt[
1, i] = 1;
y[i,
0] = Str[i + 1];
}
double[,] t = new double[2, 2];//新建矩阵t multiply(bt, a, t);//矩阵bt,a相乘,最后返回矩阵t t = inverse(t);//获取矩阵t的转置矩阵 double[,] t1 = new double[2, size - 1];//新建矩阵t1 multiply(t, bt, t1);double[,] t2 = new double[2, 1];//新建矩阵t2 multiply(t1, y, t2);
a0
= t2[0, 0];double u = t2[1, 0];
a2
= u /a0;
a1
= Str[0] -a2;
a0
= -a0;

error
= 0;for (int i = 0; i < Str.Length; i++)
{
double d = (Str[i] -GetStr(i));
error
+= d *d;
}
error
/=Str.Length;
}
public double GetError()//误差 {returnerror;
}
double GetStr1(intk)
{
return a1 * Math.Exp(a0 * k) +a2;
}
double GetStr(int k)//返回a0 * a1 * Math.exp(a0 * k); {if (k == 0)return double.Parse((a1 * Math.Exp(a0 * k) + a2).ToString("0.0"));else return double.Parse((a1 * (Math.Exp(a0 * k) - Math.Exp(a0 * (k - 1)))).ToString("0.0"));
}
public double NextValue(int index)//预测后续的值 {if (index < 0)throw new Exception("超出索引范围");return GetStr(size +index);
}
public double nextValue()//返回预测值 {return NextValue(0);
}
static double[,] inverse(double[,] t)//求矩阵t的逆矩阵 {double[,] a = new double[2, 2];double det = t[0, 0] * t[1, 1] - t[0, 1] * t[1, 0];
a[
0, 0] = t[1, 1] /det;
a[
0, 1] = -t[1, 0] /det;
a[
1, 0] = -t[0, 1] /det;
a[
1, 1] = t[0, 0] /det;returna;
}
static void multiply(double[,] left, double[,] right, double[,] dest)//两个矩阵相乘 {int first1 = left.GetLength(0);//获取第一个数组的第一维长度,也就是行数 int first2 = left.GetLength(1);//获取第一个数组的二维长度,也就是列数 int second1 = right.GetLength(1);//获取第二个数组的列数 for (int k = 0; k < first1; k++)
{
for (int j = 0; j < second1; j++)
{
dest[k, j]
= 0;for (int i = 0; i < first2; i++)
{
dest[k, j]
+= left[k, i] *right[i, j];
}
}
}
}
}
protected void Timer1_Tick(objectsender, EventArgs e)
{
//绘制图表 Chart1.ChartAreas["ChartArea1"].AxisY.Maximum =System.Double.NaN;
Chart1.ChartAreas[
"ChartArea1"].AxisY.Minimum =System.Double.NaN;//Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 5; Series series = new Series("温度");//设置图表类型,绘制温度表 series.ChartType = SeriesChartType.Line;//设置曲线样式 Temple temple = newTemple();
series.BorderWidth
= 1;//线粗 series.ShadowOffset = 1;int i = 0;int j = 0;var temple1 = from r inds.Templewhere r.Id > ds.Temple.Max(p => p.Id) - 10 //orderby r.Id descending selectr.ytem;//从数据库中获取10个数字//var tem = from r in ds.Temple select r.ytem;//查找温度值,在点上表明数字 foreach (var templ1 intemple1)
{
series.Points.AddY(templ1);
series.Points[i].Label
=templ1.ToString();
i
++;
}
//将温度值作为y值,并且在该点标识出温度值 var xna = from r inds.Templewhere r.Id > ds.Temple.Max(p => p.Id) - 10 //orderby r.Id descending select r.xname;//查找时间值 foreach (var xna12 inxna)
{
series.Points[j].AxisLabel
=xna12.ToString(); ;
j
++;
}
//将时间值作为x轴的值 Series series1= new Series("风速");//绘制风速图 series1.ChartType = SeriesChartType.Spline;//曲线样式 series1.BorderWidth = 1;
series1.ShadowOffset
= 1;int a = 0;var wind = from r inds.Templewhere r.Id > ds.Temple.Max(p => p.Id) - 10 //orderby r.Id descending selectr.ywind;//var win = from r in ds.Temple select r.ywind;//y的值 foreach (var wind1 inwind)
{
series1.Points.AddY(wind1);
series1.Points[a].Label
=wind1.ToString();
a
++;
}
//获取y的值,并显示出y值 Series series2= new Series("湿度");//绘制湿度图 series2.ChartType =SeriesChartType.Spline;
series2.BorderWidth
= 1;
series2.ShadowOffset
= 1;int c = 0;var humity1 = from r inds.Templewhere r.Id > ds.Temple.Max(p => p.Id) - 10 //orderby r.Id descending selectr.yhumidity;//var hum1 = from h in ds.Temple select h.yhumidity;//y的值 foreach (var h1 inhumity1)
{
series2.Points.AddY(h1);
series2.Points[c].Label
=h1.ToString();
c
++;
}


series1.YAxisType
=AxisType.Primary;
Chart1.Series.Add(series);
Chart1.Series.Add(series1);
Chart1.Series.Add(series2);
//在图表中显示出曲线//进行预测数据的前期获取 Temple tem1 = newTemple();var temp1 = from r inds.Templewhere r.Id > ds.Temple.Max(p => p.Id) - 20 select new{
ytem1
=r.ytem,
win1
=r.ywind,
hum1
=r.yhumidity
};
//List<double> array = new List<double>(); double[] array = new double[0];//新建数组 foreach (var num1 intemp1)
{
double p =num1.ytem1;
array
= array.Concat(new double[] { p }).ToArray();

}
//将获取的数据保存在array中 double[] array1 = new double[0];foreach (var num2 intemp1)
{
array1
= array1.Concat(new double[] { num2.win1 }).ToArray();

}
double[] array2 = new double[0];foreach(var num3 intemp1)
{
array2
= array2.Concat(new double[] { num3.hum1 }).ToArray();
}
//预测数据的计算 GrayModel gm = new GrayModel();//调用灰色预测模型进行温度数据预测 gm.build(array);
tem1.ytem
=gm.nextValue();
GrayModel gm1
= newGrayModel();
gm1.build(array1);
tem1.ywind
=gm1.nextValue();
GrayModel gm2
= newGrayModel();
gm2.build(array2);
tem1.yhumidity
=gm2.nextValue();var q =ds.Temple.Count();
tem1.Id
= q + 1;
tem1.xname
=System.DateTime.Now;
ds.Temple.InsertOnSubmit(tem1);
//将时间,学列,温度,湿度,风速数据保存在数据库中 ds.SubmitChanges();//this.Label1.Text = System.DateTime.Now.ToString(); }

上面代码的说明:从数据库中获取了指定数量 的数据,然后使用灰色预测模型预测出新的数据,最后将显示出10个数据的折线图。

标签: none

添加新评论