方法一: 前台代码: <% ... @ Page Language="C#" AutoEventWireup="true" CodeFile="ViewDeptDriver.aspx.cs" Inherits="web_CarsManage_ViewDeptDriver" %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html xmlns ="http://www.w3.org/1999/xhtml" > < head runat ="server" > < title > 无标题页 </ title > < script language ="javascript" > ... <!-- function Ini() ...{ // document.form1.all["tbFrom"].readOnly = true; document.form1.all["tbTo"].readOnly = true; var chart = document.form1.Chart;//new ActiveXObject("OWC11.ChartSpace.11"); var c = chart.Constants; if(chart == null) ...{ alert("您没有安装Office!"); } chart.Clear(); var m_Chart = chart.Charts.Add(0); //m_Chart.PlotArea.Interior.SetOneColorGradient(c.chGradientVariantStart,c.chGradientVariantStart,0.15, "Silver") m_Chart.HasLegend = true //m_Chart.HasToolBar = true m_Chart.Toolbar = true m_Chart.HasTitle = true m_Chart.Title.Caption = "IP访问按区域汇总统计"; m_Chart.Axes(0).HasTitle = true; m_Chart.Axes(0).Title.Caption = "国家/地区"; m_Chart.Axes(1).HasTitle = true; m_Chart.Axes(1).Title.Caption = "访问数"; m_Chart.type=0; var aCats = <% =DriverName.ToString()%>;//["中国","美国","澳大利亚"]; var aVals = <% =DeptName.ToString()%>;//[100, 120, 128]; var oSer = m_Chart.SeriesCollection.Add(0); oSer.SetData(c.chDimSeriesNames,-1, "图例"); oSer.SetData(c.chDimCategories,-1,aCats); oSer.SetData(c.chDimValues,-1,aVals); chart.AllowPropertyToolbox = true; chart.DisplayToolbar=true; } --> </ script > </ head > < body style ="text-align: center" onload ="javascript:Ini();" > < form id ="form1" runat ="server" > < div style ="text-align: center" > < table width ="100%" height ="100%" > < tr > < td width ="100%" height ="100%" > 开始日期: < asp:TextBox ID ="tbFrom" runat ="server" MaxLength ="10" Height ="15px" Width ="91px" ></ asp:TextBox > 截止日期: < asp:TextBox ID ="tbTo" runat ="server" MaxLength ="10" Height ="15px" Width ="85px" ></ asp:TextBox > < input type ="submit" id ="btnSearch" runat ="server" value =" 查看 " /> < input type ="button" id ="btnRtn" runat ="server" value =" 返回 " onclick ="javascript:window.history.back();" /> </ td > </ tr > < tr > < td > < object id ="Chart" name ="Chart" codebase ="http://www.smnpc.cn/bin/OWC11.DLL#version=11,0,6555,0" classid ="clsid:0002E55D-0000-0000-C000-000000000046" progid ="OWC11.ChartSpace.11" viewastext /> </ td > </ tr > < tr > < td align ="center" > </ tr > </ table > </ div > </ form > </ body > </ html > 后台代码: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; public partial class web_CarsManage_ViewDeptDriver : System.Web.UI.Page ... { protected StringBuilder DriverName = new StringBuilder("["); protected StringBuilder DeptName = new StringBuilder("["); protected void Page_Load(object sender, EventArgs e) ...{ GetCountries(); } private void GetCountries() ...{ Yesidea.DAO.DbHelperSQL dbhelper = new Yesidea.DAO.DbHelperSQL(new Yesidea.DAO.BaseDAO()); DataSet ds = dbhelper.Query("SELECT COUNT(ID),Mileage FROM ViewDicpatchDriver WHERE 1=1 GROUP BY Mileage"); foreach (DataRow dr in ds.Tables[0].Rows) ...{ DriverName.Append(""" + dr[0].ToString() + "","); DeptName.Append(dr[0].ToString() + ","); } DriverName.Replace(",", "", DriverName.Length - 1, 1); if (DriverName.Length == 1) DriverName.Append(""无数据""); DriverName.Append("]"); DeptName.Replace(",", "", DeptName.Length - 1, 1); if (DeptName.Length == 1) DriverName.Append("0"); DeptName.Append("]"); }} 方法二: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Text; using Yesidea.DAO; public partial class web_CarsManage_Controls_ViewImage : System.Web.UI.UserControl ... { //声明数据库操作对象 DbHelperSQL dbhelper = new DbHelperSQL(new BaseDAO()); protected StringBuilder DriverName = new StringBuilder("["); protected StringBuilder OilTotal = new StringBuilder("["); protected void Page_Load(object sender, EventArgs e) ...{ BindControl(); } protected void BindControl() ...{ if (!IsPostBack) ...{ ArrayList addyear = new ArrayList(); for (int i = DateTime.Now.Year - 20; i < DateTime.Now.Year + 10; i++) ...{ addyear.Add(i); this.ddlYear.Items.Add(i.ToString()); } this.ddlYear.Items[20].Selected = true; //填充数据集 string cmdtxt1 = "Select Distinct Cnumber from Car_info"; DataSet ds1 = dbhelper.Query(cmdtxt1); //绑定DropDownList显示车号 for (int i = 0; i < ds1.Tables[0].Rows.Count; i++) ...{ this.ddlCnumber.Items.Add(ds1.Tables[0].Rows[i]["Cnumber"].ToString()); } string cmdtxt2 = "Select Distinct Cdriver from Car_info"; DataSet ds2 = dbhelper.Query(cmdtxt2); //绑定DropDownList显示司机姓名 for (int i = 0; i < ds2.Tables[0].Rows.Count; i++) ...{ this.lstName.Items.Add(ds2.Tables[0].Rows[i]["Cdriver"].ToString()); } this.lstName.SelectedIndex = 0; this.Image1.ImageUrl = (Server.MapPath(".")) + "/noinfo.gif"; } } /**//// <summary> /// 创建图表 /// </summary> protected void CreateImage() ...{ //创建ChartSpace对象来放置图表 Microsoft.Office.Interop.Owc11.ChartSpace objCSpace = new Microsoft.Office.Interop.Owc11.ChartSpaceClass(); //在ChartSpace对象中添加图表,Add方法返回chart对象 Microsoft.Office.Interop.Owc11.ChChart objChart = objCSpace.Charts.Add(0); //指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到 objChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeLine; //指定图表是否需要图例 objChart.HasLegend = true; //给定标题 objChart.HasTitle = true; objChart.Title.Font.Bold = true; objChart.Title.Font.Color = "Green"; objChart.Title.Font.Size = 16; objChart.Title.Caption = this.ddlYear.SelectedItem.Text + "年油耗分析图"; //给定x,y轴的图示说明 objChart.Axes[0].HasTitle = true; objChart.Axes[0].Title.Caption = "X : 月份"; objChart.Axes[1].HasTitle = true; objChart.Axes[1].Title.Caption = "Y : 百公里油耗"; //显示横坐标数据(月份) string strCategory = "1" + " " + "2" + ' ' + "3" + ' ' + "4" + ' ' + "5" + ' ' + "6" + ' '; strCategory += "7" + ' ' + "8" + ' ' + "9" + ' ' + "10" + ' ' + "11" + ' ' + "12" + ' '; 为标准油耗量数组赋值#region 为标准油耗量数组赋值 //计算数据 /**//*categories 和 values 可以用tab分割的字符串来表示*/ string strSeriesName = "标准百公里耗油量"; //获取车号 DataSet ds2 = CreateOilDataSet(this.ddlCnumber.SelectedItem.Text); int iCount = ds2.Tables[0].Rows.Count; // 存储月份和相对应的耗油量 string[,] dataSend = new string[2, iCount]; for (int i = 0; i < iCount; i++) ...{ dataSend[0, i] = ds2.Tables[0].Rows[i][1].ToString(); // Month dataSend[1, i] = ds2.Tables[0].Rows[i][2].ToString(); // Oil } /**//// 存储1-12个月份每个月份的耗油量。 /// 索引 - 月份 /// 0 1 /// . . /// . . /// 11 12 string[] dataRecive = new string[12]; int iIndex = int.MinValue; int iOil = int.MinValue; for (int i = 0; i < dataSend.GetLength(1); i++) ...{ iIndex = int.Parse(dataSend[0, i]) - 1; dataRecive[iIndex] = dataSend[1, i]; } //定义空字符串存放数据 string strValue = String.Empty; for (int i = 0; i < dataRecive.Length; i++) ...{ if (dataRecive[i] == null || dataRecive[i] == "") ...{ OilTotal.Append("0"); } strValue += dataRecive[i] + ' '; //OilTotal.Append(dataRecive[i] +","); } //OilTotal.Append("]"); //添加一个series objChart.SeriesCollection.Add(0); //给定series的名字 objChart.SeriesCollection[0].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames, +(int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName); //给定分类 objChart.SeriesCollection[0].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories, +(int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strCategory); //给定值 objChart.SeriesCollection[0].SetData (Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues, (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strValue); #endregion 为司机百公里油耗数组赋值#region 为司机百公里油耗数组赋值 //将选择的司机添加到数组中 ArrayList arylst = new ArrayList(); for (int i = 0; i < this.lstSelectName.Items.Count; i++) ...{ arylst.Add(this.lstSelectName.Items[i].Text); } string strAbsolutePath = String.Empty; for (int m = 0; m < arylst.Count; m++) ...{ string strSeriesName1 = arylst[m].ToString() + "百公里耗油量"; DataSet ds1 = CreateDriverOilDataSet(this.ddlYear.Items[this.ddlYear.SelectedIndex].Value, this.ddlCnumber.SelectedItem.Text, arylst[m].ToString()); int iCount1 = ds1.Tables[0].Rows.Count; // 存储月份和相对应的耗油量 string[,] dataSend1 = new string[2, iCount1]; for (int i = 0; i < iCount1; i++) ...{ dataSend1[0, i] = ds1.Tables[0].Rows[i][5].ToString(); // Month dataSend1[1, i] = ds1.Tables[0].Rows[i][3].ToString(); // Oil } /**//// 存储1-12个月份每个月份的耗油量。 /// 索引 - 月份 /// 0 1 /// . . /// . . /// 11 12 string[] dataRecive1 = new string[12]; int iIndex1 = int.MinValue; int iOil1 = int.MinValue; for (int i = 0; i < dataSend1.GetLength(1); i++) ...{ iIndex1 = int.Parse(dataSend1[0, i]) - 1; dataRecive1[iIndex1] = dataSend1[1, i]; } //定义空字符串存放数据 string strValue1 = String.Empty; for (int i = 0; i < dataRecive1.Length; i++) ...{ if (dataRecive1[i] == null || dataRecive1[i] == "") ...{ strValue1 += "0" + ' '; //DriverName.Append(""无数据""); } strValue1 += dataRecive1[i] + ' '; //DriverName.Append(""" + dataRecive1[i] + "","); } //DriverName.Append("]"); //添加一个series objChart.SeriesCollection.Add(m); /**/////给定series的名字 objChart.SeriesCollection[m].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames, +(int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName1); //给定分类 objChart.SeriesCollection[m].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories, +(int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strCategory); //给定值 objChart.SeriesCollection[m].SetData (Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues, (int)Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, strValue1); //输出成GIF文件. strAbsolutePath = (Server.MapPath(".")) + "/test.gif"; //string strAbsolutePath = "/_layouts/images/images" + "/test.gif"; objCSpace.ExportPicture(strAbsolutePath, "GIF", 750, 580); } #endregion //绑定生成的图片 this.Image1.ImageUrl = strAbsolutePath; } /**//// <summary> /// 创建司机百公里油耗数据集 /// </summary> /// <param name="cnumber">车号</param> /// <param name="cdriver">司机姓名</param> /// <returns></returns> protected DataSet CreateDriverOilDataSet(string year, string cnumber, string cdriver) ...{ string cmdtxt = "select Cdriver,Ctype,Cnumber,sum(futel) as futel, [Year],[Month] from "; cmdtxt += "(select Cdriver,Ctype,Cnumber,(sum(cfutel)/kilometers)*100 as futel,"; cmdtxt += " datename(year, Stimte) as [Year], datename(month, stimte) as [Month] from ViewDriverUOil "; cmdtxt += "group by datename(year, Stimte),datename(month, stimte),Ctype,Cdriver,Cnumber,kilometers) ChildTable"; cmdtxt += " where [Year]='" + year + "' and Cnumber='" + cnumber + "' and Cdriver='" + cdriver + "' group by Cdriver,Ctype,Cnumber, [Year],[Month]"; cmdtxt += " order by [Year] asc,[Month] asc"; return dbhelper.Query(cmdtxt); } /**//// <summary> /// 创建标准百公里油耗数据集 /// </summary> /// <param name="ctype">车型</param> /// <returns></returns> protected DataSet CreateOilDataSet(string cnumber) ...{ string cmdtxt = "Select Ctype,CMonth,COilUse from car_oil_info where Ctype='" + cnumber + "' order by CMonth asc"; return dbhelper.Query(cmdtxt); } /**//// <summary> /// 根据车号查找车型 /// </summary> /// <param name="cnumber">车号</param> /// <returns></returns> protected string GetCarType(string cnumber) ...{ string cmdtxt = "select Cdriver,Ctype,Cnumber,sum(futel) as futel, [Year],[Month] from "; cmdtxt += "(select Cdriver,Ctype,Cnumber,(sum(cfutel)/kilometers)*100 as futel,"; cmdtxt += " datename(year, Stimte) as [Year], datename(month, stimte) as [Month] from ViewDriverUOil "; cmdtxt += "group by datename(year, Stimte),datename(month, stimte),Ctype,Cdriver,Cnumber,kilometers) ChildTable"; cmdtxt += " where Cnumber='" + cnumber + "' group by Cdriver,Ctype,Cnumber, [Year],[Month]"; cmdtxt += " order by [Year] asc,[Month] asc"; DataSet ds = dbhelper.Query(cmdtxt); //填充数据集 string Type = String.Empty; if (ds.Tables[0].Rows.Count != 0) ...{ Type = ds.Tables[0].Rows[0][1].ToString(); } return Type; } protected void btnOK_Click(object sender, EventArgs e) ...{ string alert = String.Empty; if (this.ddlCnumber.SelectedIndex == 0) ...{ alert += "1、请选择车号!" + "/n"; } if (this.lstSelectName.Items.Count == 0 ) ...{ alert += "2、至少选择一名司机进行统计!" + "/n"; } if (alert != "") ...{ Response.Write("<script>alert('" + alert + "');history.go(-1)</script>"); return; } CreateImage(); } protected void btnSelectAll_Click(object sender, EventArgs e) ...{ this.lstName.Items.Clear(); this.lstSelectName.Items.Clear(); string cmdtxt2 = "Select Distinct Cdriver from Car_info"; DataSet ds2 = dbhelper.Query(cmdtxt2); //绑定DropDownList显示司机姓名 for (int i = 0; i < ds2.Tables[0].Rows.Count; i++) ...{ this.lstSelectName.Items.Add(ds2.Tables[0].Rows[i]["Cdriver"].ToString()); } } protected void btnSelectSingle_Click(object sender, EventArgs e) ...{ this.lstName.SelectedIndex = 0; ArrayList arylst = new ArrayList(); arylst.Add(this.lstName.SelectedItem.Text); this.lstName.Items.RemoveAt(this.lstName.SelectedIndex); for (int i = 0; i < arylst.Count; i++) ...{ this.lstSelectName.Items.Add(arylst[i].ToString()); } } protected void btnRemoveSingle_Click(object sender, EventArgs e) ...{ this.lstSelectName.SelectedIndex = 0; ArrayList arylst = new ArrayList(); arylst.Add(this.lstSelectName.SelectedItem.Text); this.lstSelectName.Items.RemoveAt(this.lstSelectName.SelectedIndex); for (int i = 0; i < arylst.Count; i++) ...{ this.lstName.Items.Add(arylst[i].ToString()); } } protected void btnRemoveAll_Click(object sender, EventArgs e) ...{ this.lstName.Items.Clear(); this.lstSelectName.Items.Clear(); string cmdtxt2 = "Select Distinct Cdriver from Car_info"; DataSet ds2 = dbhelper.Query(cmdtxt2); //绑定DropDownList显示司机姓名 for (int i = 0; i < ds2.Tables[0].Rows.Count; i++) ...{ this.lstName.Items.Add(ds2.Tables[0].Rows[i]["Cdriver"].ToString()); } }}