Asp.net 用 OWC 生成拆线图

using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using OWC10;
using cncic.kmp.DataBase;
using CI_Components.Price;

namespace WebAppCI_Portal.UI.Price
{
    /// <summary>
    /// Author: Lance
    /// Notes:  General Chart
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class PPChart : IHttpHandler
    {
        public String Sjzq = string.Empty;

        private void doPost(HttpContext context, HttpRequest Request, HttpResponse Response, HttpServerUtility Server)
        {
            string id = DotNet.Common.Utilities.myUtils.StringUtils1.InputText(Request.QueryString["id"], 20);
            string n = DotNet.Common.Utilities.myUtils.StringUtils1.InputText(Request.QueryString["n"], 20);
            string t1 = DotNet.Common.Utilities.myUtils.StringUtils1.InputText(Request.QueryString["t1"], 20);
            string t2 = DotNet.Common.Utilities.myUtils.StringUtils1.InputText(Request.QueryString["t2"], 20);
            string pids = DotNet.Common.Utilities.myUtils.StringUtils1.InputText(Request.QueryString["pids"], 50);
            string tf = Request.QueryString["tf"];
            Sjzq = tf.Trim();
            string pnames = Request.QueryString["pname"];
            ShowCharX(id, n, t1, t2, pids, context, tf,pnames);
        }
        private string GetTableName(params object[] otmp)
        {
            string sqlcmd = string.Empty;
            string id = otmp[0].ToString();
            string n = otmp[1].ToString();
            string t1 = otmp[2].ToString();
            string t2 = otmp[3].ToString();
            string pids = otmp[4].ToString();
            pids = pids.Substring(0, pids.Length - 1);
            HttpContext context = (HttpContext)otmp[5];
            string tableName = string.Empty;
            switch (int.Parse(n))
            {
                case 0:
                    tableName = "T_Ccjg";
                    break;
                case 1:
                    tableName = "T_dqjg";
                    break;
                case 2:
                    tableName = "t_pfjg";
                    break;
                case 3:
                    tableName = "t_ckjg";
                    break;
                case 4:
                    tableName = "t_jkjg";
                    break;
            }
            return tableName;
        }

        /// <summary>
        /// 基本格式:
        ///     select  * from dbo.T_Ccjg where productId ={0} {1}
        /// Colums:
        ///     bjrq,jg
        /// </summary>
        /// <param name="otmp"></param>
        /// <returns></returns>
        private string GetSql(params object[] otmp)
        {
            string sqlcmd = string.Empty;
            string id = otmp[0].ToString();
            string n = otmp[1].ToString();
            string t1 = otmp[2].ToString();
            string t2 = otmp[3].ToString();
            string pids = otmp[4].ToString();           
            pids = pids.Substring(0, pids.Length - 1);           
            pids = pids.Replace('`', ',');

            HttpContext context = (HttpContext)otmp[5];
            string sqlcmd0 = @" select bjrq,datepart(yy,bjrq) as yy,datepart(mm,bjrq) as mm,datepart(dd,bjrq)dd,sum(jg)/count(1) jg,productId from
({0})f
group by bjrq,productId
order by bjrq";
            sqlcmd = @" select  convert(varchar(11),bjrq,120) bjrq,sum(jg)/count(1) jg,productId from {0}
group by convert(varchar(11),bjrq,120),jg,productId ";
            switch (int.Parse(n))
            {
                case 0:
                    sqlcmd = string.Format(sqlcmd, " dbo.T_Ccjg  where productId in {0} {1} ");
                    break;
                case 1:
                    sqlcmd = string.Format(sqlcmd, " dbo.T_Dqjg  where productId in {0} {1} ");
                    break;
                case 2:
                    sqlcmd = string.Format(sqlcmd, " dbo.T_Pfjg  where productId in {0} {1} ");
                    break;
                case 3:
                    sqlcmd = string.Format(sqlcmd, " dbo.T_Ckjg  where productId in {0} {1} ");
                    break;
                case 4:
                    sqlcmd = string.Format(sqlcmd, " dbo.T_Jkjg  where productId in {0} {1} ");
                    break;
            }
            sqlcmd = string.Format(sqlcmd, "("+pids+")", " and bjrq between '" + t1 + "' and '" + t2 + "' ");
            sqlcmd = string.Format(sqlcmd0, sqlcmd);
            return sqlcmd;
        }
        /// <summary>
        /// 条状
        /// </summary>
        private void ShowCharT(params object[] otmp)
        {
            HttpContext context = (HttpContext)otmp[otmp.Length - 1];
        }
        /// <summary>
        /// 取出产品名称
        /// </summary>
        /// <param name="otmp"></param>
        /// <returns></returns>
        private string GetProductName(params object[] otmp)
        {
            string id = otmp[0].ToString();
            string n = otmp[1].ToString();
            string t1 = otmp[2].ToString();
            string t2 = otmp[3].ToString();
            string pids = otmp[4].ToString();
            pids = pids.Substring(0, pids.Length - 1);           
            HttpContext context = (HttpContext)otmp[5];
            string pnames = otmp[7].ToString();
            pnames = pnames.Substring(0, pnames.Length - 1);
            pnames = pnames.Replace('`', ',');
            return pnames;

            #region 从数据库里取出产品名字,改为了参数传递产品名字
            //string sqlcmd = string.Format("select * from t_product where id  in ({0}) order by id desc ", pids.Replace("`", ","));
            //DataTable dtCpmc = helper.SQL.ExecuteDataset(CONNST_STRING.ConnectionStringPriceDb, CommandType.Text, sqlcmd).Tables[0];
            //string pname = string.Empty;
            //foreach (DataRow dr in dtCpmc.Rows)
            //{
            //    pname += dr["productname"].ToString() + ",";
            //}
            //pname = pname.TrimEnd(',');
            //return pname.Trim();
            #endregion
        }
        private string GetDw(params object[] otmp)
        {
            string id = otmp[0].ToString();
            string n = otmp[1].ToString();
            string t1 = otmp[2].ToString();
            string t2 = otmp[3].ToString();
            string pids = otmp[4].ToString();
            pids = pids.Substring(0, pids.Length - 1);
            HttpContext context = (HttpContext)otmp[5];
            string sqlcmd = "select top 1 dw from " + GetTableName(otmp);
            return helper.SQL.ExecuteScalar(CONNST_STRING.ConnectionStringPriceDb, CommandType.Text, sqlcmd).ToString().Trim();
        }
        public string[] GetProductsId(params object[] otmp)
        {
            string id = otmp[0].ToString();
            string n = otmp[1].ToString();
            string t1 = otmp[2].ToString();
            string t2 = otmp[3].ToString();
            string pids = otmp[4].ToString();
            pids = pids.Substring(0, pids.Length - 1);
            HttpContext context = (HttpContext)otmp[5];
            return pids.Split('`');
        }
        /// <summary>
        /// 线状
        /// </summary>
        private void ShowCharX(params object[] otmp)
        {
            string id = otmp[0].ToString();
            string n = otmp[1].ToString();
            string t1 = otmp[2].ToString();
            string t2 = otmp[3].ToString();
            string pids = otmp[4].ToString();
            pids = pids.Substring(0, pids.Length - 1);
            HttpContext context = (HttpContext)otmp[5];
            string tf = otmp[6].ToString();
            //初始化产品名称
            string cpmc = GetProductName(otmp);
            //初始化单位名称
            string dw = GetDw(otmp);
            #region 设置画布
            //创建ChartSpace对象来放置图表
            ChartSpace mySpace = new ChartSpaceClass();
            //在ChartSpace对象中添加图表,Add方法返回chart对象
            ChChart myChart = mySpace.Charts.Add(0);
            //设置边框
            mySpace.Border.Color = "white";
            //图例
            myChart.HasLegend = true;
            //给定标题
            myChart.HasTitle = true;
            myChart.Title.Font.Name = "宋体";
            myChart.Title.Font.Size = 9;
            myChart.Title.Position = ChartTitlePositionEnum.chTitlePositionTop;
            myChart.Title.Caption = "("+cpmc+")"+UtilsPrice.GetPriceStyle(otmp);

            //设定图例文字
            myChart.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
            myChart.Legend.Font.Name = "宋体";
            myChart.Legend.Font.Size = 9;

            //设置背景色
            myChart.PlotArea.Interior.Color = "white";//lxj
            myChart.PlotArea.Border.Color = "#b5cae1";

            //设置图表的X坐标
            myChart.Axes[0].Font.Name = "宋体";
            myChart.Axes[0].Font.Size = 8;
            myChart.Axes[0].HasTitle = true;
            myChart.Axes[0].Title.Font.Name = "宋体";
            myChart.Axes[0].Title.Font.Size = 9;
            myChart.Axes[0].Title.Caption = "时间(按" + tf + ")";
            myChart.Axes[0].Font.Color = "#282727";//lxj X轴字体颜色
            myChart.Axes[0].Line.Color = "#b6cbe8";//lxj X轴颜色
            myChart.Axes[0].HasMajorGridlines = true;//lxj 显示X轴网格线
            myChart.Axes[0].MajorGridlines.Line.Color = "#b5cae1";//lxj X轴网格线颜色

            //设置图表的Y坐标
            myChart.Axes[1].Font.Name = "Arial";
            myChart.Axes[1].Font.Size = 7;
            myChart.Axes[1].HasTitle = true;
            myChart.Axes[1].Title.Font.Name = "宋体";
            myChart.Axes[1].Title.Font.Size = 9;
            myChart.Axes[1].Title.Caption = "数量(" + dw + ")";
            myChart.Axes[1].Font.Color = "#282727";//lxj Y轴字体颜色
            myChart.Axes[1].Line.Color = "#b6cbe8";//lxj Y轴颜色
            myChart.Axes[1].MajorGridlines.Line.Color = "#b5cae1";//Y轴网格线颜色
            #endregion           
            double intmax1 = 0;
            double intmin1 = 0;
            double tempunit = 1;
            DataTable dtTmp = new DataTable();
            #region
            //取出相应的sql
            string sqlcmd = GetSql(otmp);
            dtTmp = helper.SQL.ExecuteDataset(CONNST_STRING.ConnectionStringPriceDb, CommandType.Text, sqlcmd).Tables[0];
            #endregion
            foreach (DataRow dr in dtTmp.Rows)
            {
                double dtmp =  double.Parse(dr["jg"].ToString());
                if (intmax1 <dtmp)
                {
                    intmax1 = dtmp;
                }
                if (intmin1 > dtmp)
                {
                    intmin1 = dtmp;
                }
            }
           
            tempunit = intmax1-intmin1;
            tempunit = Math.Pow(10, Convert.ToString(Convert.ToInt64(tempunit)).Length - 1);// *2;
            myChart.Axes[1].MajorUnit = tempunit;
            myChart.Axes[1].Scaling.Minimum = (int)(intmin1 / tempunit - 1) * tempunit;
            myChart.Type = ChartChartTypeEnum.chChartTypeLineMarkers;
     
            string[] productIds = pids.Split('`');
            string[] productNames = cpmc.Split(',');
            int itmp = productIds.Length;
            string strValueStrtemp = ValueStrtemp(dtTmp);
          
            //给系列命名
            string Temp = "";
            #region 跟据产品的数量生成相应的线
            for (int i = 0; i < itmp; i++)
            {
                string strValueStr = ValueStr(dtTmp,productIds[i]);
                Temp = productNames[i];
                //添加图表系列
                myChart.SeriesCollection.Add(i);               
                myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimSeriesNames, (int)(ChartSpecialDataSourcesEnum.chDataLiteral), Temp);
                //给系列分组
                myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories, (int)(ChartSpecialDataSourcesEnum.chDataLiteral), strValueStrtemp);
                //给系列各分组赋值     
                myChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimValues, (int)(ChartSpecialDataSourcesEnum.chDataLiteral), strValueStr);
                //指定各系列的属性
                string colortmp = UtilsPrice.GetColor(i);
                myChart.SeriesCollection[i].Line.Color = colortmp;
                myChart.SeriesCollection[i].Interior.Color = colortmp;
                myChart.SeriesCollection[i].Marker.Size = 5;
                myChart.SeriesCollection[i].Marker.Style = ChartMarkerStyleEnum.chMarkerStyleCircle;
            }
            #endregion     
            int width = 550, height = 450;
            context.Response.BinaryWrite((byte[])mySpace.GetPicture("GIF", width, height));
            mySpace.Clear();
            mySpace.ClearUndo();
            mySpace = null;
        }
        protected string ValueStr(DataTable dttmp,string productId)
        {           
            string strtemp = string.Empty;
            DataRow[] drtmp = dttmp.Select(" productId= " + productId);
            try
            {
                if (drtmp.Length == 0)
                {
                    return "0";
                }
                foreach (DataRow dr in drtmp)
                {
                    if (dr["jg"].ToString().Trim() == "0" || dr["jg"].ToString().Trim() == "")
                    {
                        strtemp += ",0";
                    }
                    else
                    {
                        strtemp += "," + dr["jg"].ToString();
                    }
                }
                strtemp = strtemp.TrimStart(',');
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return strtemp;
        }
        /// <summary>
        /// 生成以逗号分隔的日期字符串
        /// </summary>
        /// <returns>逗号分隔的字符串</returns>
        protected string ValueStrtemp(DataTable dttmp)
        {
           
            string strtemp = string.Empty;
            try
            {
                if (dttmp.Rows.Count == 0)
                {
                    return "0";
                }
                foreach (DataRow dr in dttmp.Rows)
                {
                    switch (Sjzq)
                    {
                        case "月":
                            strtemp += DateTime.Parse(dr["bjrq"].ToString()).ToString("yyyy.MM") + ",";
                            break;
                        case "周":
                            strtemp += dr["yy"].ToString() +"."+ (int.Parse(dr["mm"].ToString())*4) + ",";
                            break;
                        case "日":
                            strtemp += DateTime.Parse(dr["bjrq"].ToString()).ToString("yyyy.MM.dd") + ",";
                            break;
                    }
                   
                }
                strtemp = strtemp.Substring(0, strtemp.Length - 1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return strtemp;
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Buffer = false;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "image/jpg";
            doPost(context, context.Request, context.Response, context.Server);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

你可能感兴趣的:(asp.net)