您的位置:首页 > 编程语言 > ASP

ASP.NET----owc绘制图表

2013-06-20 18:21 375 查看
protected void pic() {

string datedata = null;

string sql = "select "+str2+"  from cellardata  where cellars='" + cellar + "' and  sendtime   between'" + date1 + "'  and '" + date2 + "'";
DataSet ds = CC.GetDataSet(sql, "wendu");
DataRow[] row = ds.Tables["wendu"].Select();
int ii = 0,x=5;//x是温度刻度值,这里初始化5为一个刻度
Random r1 = new Random();

string[] table_label = str1.Split(','); //str1中记录了checkbox的选值,也就是有哪些探头

int len = table_label.Length; //len为所选的探头个数
int len_wendu = row.Length; //温度数据的个数
int beishu = len_wendu / 200;  //计算温度数据个数是不是200的倍数
if (beishu == 0)
{

x = 5;  //如果温度个数不超过200个,则刻度仍为5

}
else {

x = 5 * beishu;  //温度个数超过了200个,则刻度的值为倍数*5

}

string [] arr;
arr = new string[len]; //定义一个数组用来存放温度数据

foreach (DataRow rs in row)
{
ii++;
if (ii % x == 0)
{
datedata += rs["sendtime"].ToString() + "\t";//为x轴指定时间数据,是x(刻度)的倍数的时间才显示

}
else
{
//datedata += rs["sendtime"].ToString().Substring(5) + "\t";//为x轴指定时间数据
datedata += "" + "\t";
}
//tempdata += int.Parse(rs["wendu_value"].ToString()) + (-5 + r1.NextDouble() * 10) + "\t";//为y轴指定wendu数据
//tempdata1 += int.Parse(rs["wendu_value"].ToString()) + ( r1.NextDouble() * 5) + "\t";//为y轴指定wendu数据1
for (int i = 0; i < len; i++)
{

string  a=table_label[i].ToString();
arr[i] += Convert.ToString(double.Parse(rs[a].ToString().Trim()) / 100) + "\t"; //把温度数据除以100后放进温度数组中

}

}

//计算出该时间段内数据的最大值和最小值
for (int i = 0; i < len; i++)
{

string a = table_label[i].ToString();

string sql1 = "SELECT MAX(" + a + ") AS 最大值, MIN(" + a + ") AS 最小值 FROM cellardata where cellars='" + cellar + "' and  sendtime   between'" + date1 + "'  and '" + date2 + "'";
DataSet da = CC.GetDataSet(sql1, "zuizhi");

if (da.Tables[0].Rows[0][1] == null || da.Tables[0].Rows[0][0] == null || da.Tables[0].Rows[0][1].ToString() == ""|| da.Tables[0].Rows[0][0].ToString()=="")
{
Response.Write(CC.MessageBox("选择的时间段和探头无数据,请重新选择", "inquire.aspx"));

}

else
{

double min1 = double.Parse(da.Tables[0].Rows[0][1].ToString());
double max1 = double.Parse(da.Tables[0].Rows[0][0].ToString());

//   this.Label1.Text += "," + min1;
if (min1 < min)
{
min = min1;

}

if (max1 > max)
{
max = max1;

}

}
//   this.Label1.Text += "," + min;

//arr[i] += int.Parse(rs[a].ToString().Trim())+"\t";
// arr[i] += Convert.ToString(double.Parse(rs[a].ToString().Trim()) / 100) + "\t";

}

min = (int)min/ 100 - 5;
max = (int)max/ 100 + 5;
min = min - min % 5;
max = max +(5- max % 5);
// this.Label1.Text += "," + min;
//创建ChartSpace对象来放置图表
ChartSpace thisChart = new ChartSpaceClass();
//在ChartSpace对象中添加图表,Add方法返回chart对象
ChChart myChart = thisChart.Charts.Add(0);
//指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到
myChart.Type = ChartChartTypeEnum.chChartTypeLine;

//指定图表是否需要图例
myChart.HasLegend = true;
myChart.Legend.Font.Size = 8;
myChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionTop;

//给定标题
myChart.HasTitle = true;
myChart.Title.Caption = "温度曲线图";

//给定x,y轴的图示说明
myChart.Axes[0].HasTitle = true;
myChart.Axes[0].Title.Caption = "X : 时间";
myChart.Axes[1].HasTitle = true;
myChart.Axes[1].Title.Caption = "Y : 温度";

//myChart.Axes[0].Font.Size = 8;
myChart.Axes[0].HasMajorGridlines = true;//X轴需不需要网格
//myChart.Axes[1].Font.Size = 8;
myChart.Axes[1].HasMajorGridlines = true;//y轴需不需要网格

//y轴的最小值和最大值
myChart.Axes[1].Scaling.Maximum = max;
myChart.Axes[1].Scaling.Minimum = min;

ChScaling axisScale = thisChart.Charts[0].Axes[1].Scaling;
thisChart.Charts[0].Axes.Add(axisScale);
myChart.Axes[2].Position = ChartAxisPositionEnum.chAxisPositionRight;

for (int i = 0; i < len; i++)
{

//增加第i条曲线(第个图例)
myChart.SeriesCollection.Add(i);
string strseriesName = table_label[i].ToString().Replace("sendtime", "时间").Replace("d1", "探头1").Replace("d2", "探头2").Replace("d3", "探头3").Replace("d4", "探头4").Replace("d5", "探头5").Replace("d6", "探头6").Replace("d7", "探头7").Replace("d8", "探头8").Replace("d9", "探头9").Replace("d10", "探头10").Replace("d11", "探头11").Replace("d12", "探头12").Replace("e_temp", "环境温度");

myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strseriesName);
myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, datedata);
myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, arr[i]);

}

////增加一条曲线(一个图例)
//myChart.SeriesCollection.Add(0);
//string strseriesName = "温度1";

//myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strseriesName);
//myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, datedata);
//myChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, tempdata);

////增加第二条曲线(第二个图例)
//myChart.SeriesCollection.Add(1);
//string strseriesName2 = "温度2";

//myChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strseriesName2);
//myChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimCategories, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, datedata);
//myChart.SeriesCollection[1].SetData(ChartDimensionsEnum.chDimValues, (int)ChartSpecialDataSourcesEnum.chDataLiteral, tempdata1);

string name = DateTime.Now.ToString("yyyyMMddhhmmss");
string strAbsolutePath = (Server.MapPath(".")) + "\\savepic\\" + name + ".jpg";
thisChart.ExportPicture(strAbsolutePath, "JPG", 800, 600);
this.Image1.ImageUrl = "savepic\\" + name + ".jpg";
this.Image1.Visible = true;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: