using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Web.UI.DataVisualization.Charting;
namespace SBS.Common
{
    public class MSChartHelpe
    {
        #region Get    { ChartControl || ChartArea || Series } 5-25

        private System.Web.UI.DataVisualization.Charting.Chart GetChartControl(
           string TableName, string datecreated, System.Web.UI.WebControls.Unit ChartWidth,
           System.Web.UI.WebControls.Unit ChartHeight,
           Docking LegendDock, bool LegendsEnabled, BorderSkinStyle borderSkinStyle)
        {
            System.Web.UI.DataVisualization.Charting.Chart chartControl =
           new System.Web.UI.DataVisualization.Charting.Chart();
            chartControl.RenderType = RenderType.ImageMap;
            chartControl.ImageType = ChartImageType.Png;
            chartControl.ImageStorageMode = ImageStorageMode.UseHttpHandler;
            chartControl.Width = ChartWidth;
            chartControl.Height = ChartHeight;
            chartControl.Palette = ChartColorPalette.BrightPastel;
            chartControl.Legends.Add("Legend1");
            chartControl.Legends[0].Enabled = LegendsEnabled;
            chartControl.Legends[0].IsTextAutoFit = false;
            chartControl.Legends[0].Docking = LegendDock;
            chartControl.Legends[0].LegendStyle = LegendStyle.Table;
            chartControl.Legends[0].Alignment = StringAlignment.Center;
            chartControl.BorderSkin.SkinStyle = borderSkinStyle;
            chartControl.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
            Title t =
               new Title(TableName, Docking.Top, new System.Drawing.Font(
               "微软雅黑", 10,
               System.Drawing.FontStyle.Bold),
               System.Drawing.Color.Black
               );
            Title subTile =
             new Title(datecreated, Docking.Top, new System.Drawing.Font(
             "微软雅黑", 9,
             System.Drawing.FontStyle.Regular),
             System.Drawing.Color.FromArgb(26, 59, 105)
             );
            if (TableName != "")
            {
                chartControl.Titles.Add(t);
                chartControl.ToolTip = TableName;
            }
            if (datecreated != "")
            {
                subTile.Text = " 文件生成时间:" + datecreated;
                chartControl.Titles.Add(subTile);
            }
            return chartControl;
        }

        private ChartArea GetChartArea(string TableName, string XTile,
            String Ytile, bool Is3D, double YInterval, double XYInterval)
        {
            ChartArea cArea = new ChartArea(TableName);
            cArea.AxisX.IsLabelAutoFit = false;
            cArea.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.IncreaseFont;
            cArea.AxisX.LabelAutoFitMaxFontSize = 10;
            cArea.AxisX.MajorGrid.Enabled = true;
            cArea.AxisY.MajorGrid.Enabled = false;
            cArea.AxisX.MajorGrid.LineColor = System.Drawing.ColorTranslator.FromHtml("#e3e3e3");
            cArea.AxisY.MajorGrid.LineColor = System.Drawing.ColorTranslator.FromHtml("#e3e3e3");
            //直角坐标显示,
            cArea.Area3DStyle.IsRightAngleAxes = false;
            //是否群集在一起
            cArea.Area3DStyle.IsClustered = false;
            //外牆寬度
            cArea.Area3DStyle.WallWidth = 0;
            //转动X轴角度
            cArea.Area3DStyle.Inclination = 15;
            //是否启用3D显示
            cArea.Area3DStyle.Enable3D = Is3D;
            //转动Y轴角度
            cArea.Area3DStyle.Rotation = 15;
            cArea.AxisX.Title = XTile;
            cArea.AxisY.Title = Ytile;
            cArea.AxisX.LineColor = System.Drawing.ColorTranslator.FromHtml("#e3e3e3");
            cArea.AxisY.LineColor = System.Drawing.ColorTranslator.FromHtml("#e3e3e3");
            cArea.AxisX.LineWidth = 1;
            cArea.AxisY.LineWidth = 1;
            cArea.AxisX.TitleFont = new System.Drawing.Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);
            cArea.AxisY.TitleFont = new System.Drawing.Font("微软雅黑", 9, System.Drawing.FontStyle.Regular);
            cArea.AxisX.LabelStyle.Font = new System.Drawing.Font("微软雅黑", 9, System.Drawing.FontStyle.Regular);
            cArea.AxisY.LabelStyle.Font = new System.Drawing.Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);
            if (YInterval != 0)
            {
                cArea.AxisY.Interval = YInterval;
            }
            cArea.AxisY.IsInterlaced = false;
            cArea.AxisY.IsLabelAutoFit = false;
            cArea.AxisY.IsMarksNextToAxis = true;
            cArea.AxisX.Interval = XYInterval;
            cArea.IsSameFontSizeForAllAxes = true;
            cArea.AxisX.LabelStyle.IsStaggered = false;   //设置是否交错显示,比如数据多的时候分成两行来显示 

            return cArea;
        }

        private Series GetSeries(
            string sName, ChartArea cArea, string S3d,
            System.Web.UI.DataVisualization.Charting.SeriesChartType type, bool Change)
        {
            Series s = new Series(sName);
            s.ChartType = type;
            s.ChartArea = cArea.Name;
            //柱状条是否3D 
            if (S3d != "")
            {
                s["DrawingStyle"] = S3d;//Cylinder
            }
            s["PointWidth"] = "0.3";
            //是否显示数值
            s.IsValueShownAsLabel = true;
            s.LegendText = sName;
            s.Font = new System.Drawing.Font("宋体", 9, System.Drawing.FontStyle.Regular);
            s.BorderWidth = 2;
            s.ShadowColor = System.Drawing.ColorTranslator.FromHtml("180, 26, 59, 105");
            if (Change)
            {
                s.BackGradientStyle = GradientStyle.DiagonalRight;
            }
            s.ShadowOffset = 1;
            return s;
        }

        private void SetStyle(ChartArea cArea, System.Web.UI.DataVisualization.Charting.Chart chartControl)
        {
            cArea.BorderDashStyle = ChartDashStyle.Solid;
            cArea.BackSecondaryColor = Color.White;

            cArea.BorderColor = System.Drawing.ColorTranslator.FromHtml("64, 64, 64, 64");
            cArea.BackColor = System.Drawing.ColorTranslator.FromHtml("#ECF4FF");
            chartControl.BackColor = System.Drawing.ColorTranslator.FromHtml("#EBF3FF");
            chartControl.BorderlineColor = System.Drawing.ColorTranslator.FromHtml("26, 59, 105");
            cArea.ShadowColor = Color.Transparent;

            chartControl.BorderlineWidth = 0;
            chartControl.BorderlineDashStyle = ChartDashStyle.Solid;
            cArea.BackGradientStyle = GradientStyle.TopBottom;
            chartControl.BackGradientStyle = GradientStyle.TopBottom;
            chartControl.EnableViewState = true;

        }

        private static DataPoint GetPoint(string xName, System.Drawing.Font font, string pointValue, string LegendText)
        {
            DataPoint point = new DataPoint();
            point["LabelStyle"] = "Top";
            point.Label = pointValue;
            point.SetValueXY(xName, pointValue);
            if (pointValue == "0")
            {
                point.Label = "  ";
            }
            point.IsValueShownAsLabel = false;
            point.LabelForeColor = Color.Black;
            point.LabelBackColor = Color.Transparent;
            point.LabelBorderColor = Color.Transparent;
            point["PieLabelStyle"] = "Outside";
            if (LegendText != null && LegendText != "")
            {
                point.LegendText = LegendText;
                point.Label = pointValue +"%";
                point["CollectedThresholdUsePercent"] = "true";
                point.IsValueShownAsLabel = true;
            }
            point.Font = font;
            return point;
        }
        private static void SetPIE(Series s, string style, string width)
        {
           s["PieLabelStyle"] = "Outside";
           s["PieLabelStyle"] = style; //样式
           s["DoughnutRadius"] = width; //圆环粗细
            s["CollectedThresholdUsePercent"] = "true";

        }
        public MemoryStream GetDefaultImg()
        {
            if (System.Web.HttpRuntime.Cache["defaultImg"] != null)
            {
                return (System.IO.MemoryStream)System.Web.HttpRuntime.Cache["defaultImg"];
            }
            string CreationTime = DateTime.Now.ToShortDateString();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Chart chartControl = GetChartControl("温馨提示:数据不完全", CreationTime, 700, 350,
                Docking.Bottom, true, BorderSkinStyle.None);
            ChartArea cArea = GetChartArea("温馨提示:数据不完全", "", "", false, 100, 1);
            chartControl.ChartAreas.Add(cArea);
            cArea.AxisX.IsMarginVisible = true;
            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular);
            SetStyle(cArea, chartControl);
            chartControl.SaveImage(ms);
            System.Web.HttpRuntime.Cache.Remove("defaultImg");
            System.Web.HttpRuntime.Cache.Insert("defaultImg", ms, null,
                System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.Zero);
            return ms;
        }
        public MemoryStream GetDefaultImg2(string content)
        {
            string CreationTime = DateTime.Now.ToShortDateString();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Chart chartControl = GetChartControl(content, CreationTime, 700, 350,
                Docking.Bottom, true, BorderSkinStyle.None);
            ChartArea cArea = GetChartArea(content, "", "", false, 100, 1);
            chartControl.ChartAreas.Add(cArea);
            cArea.AxisX.IsMarginVisible = true;
            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular);
            SetStyle(cArea, chartControl);
            chartControl.SaveImage(ms);
            return ms;
        }
        #endregion


        #region 饼,柱 --默认获取
        #region 柱状图
        /// <summary>
        /// 简单快捷获取柱状图
        /// </summary>
        /// <param name="DataDict">数据集(X,Y轴值的集合)</param>
        /// <param name="xTitlename">x轴的TITLE</param>
        /// <param name="yTitleName">y轴的TITLE</param>
        /// <param name="tableTile">报表标题</param>
        /// <param name="subTitle">报表副标题</param>
        /// <param name="Width">报表宽</param>
        /// <param name="Height">报表高</param>
        /// <param name="YInterval">Y轴的间隔</param>
        /// <param name="imgType">图表类型(要柱状图请传0)</param>
        /// <returns>MemoryStream(报表)</returns>
        public MemoryStream GetColumnImg(
            Dictionary<string, double> DataDict,
            string xTitlename, string yTitleName,
            string tableTile, string subTitle,
            int Width,
            int Height,
            int YInterval,
            System.Web.UI.DataVisualization.Charting.SeriesChartType imgType,
             string SeriesName
            )
        {
            if (subTitle == null)
            {
                subTitle = "";
            }
            if (xTitlename == null) xTitlename = "";
            if (yTitleName == null) yTitleName = "";
            if (tableTile == null) tableTile = "";
            if (subTitle == null) subTitle = "";
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (DataDict == null) { ms = GetDefaultImg(); return ms; }
            Chart chartControl =
                GetChartControl(tableTile, subTitle, Width, Height,
                Docking.Bottom, true, BorderSkinStyle.None);

            ChartArea cArea =
                GetChartArea(tableTile, xTitlename, yTitleName, false, YInterval, 1);

            chartControl.ChartAreas.Add(cArea);
            cArea.AxisX.IsMarginVisible = true;
            System.Drawing.Font font =
                new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular);

            bool changes = true;//渐变
            string S3d = "Cylinder";//Cylinder

            var ChartType =
                System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
            if (imgType != 0)
                ChartType = imgType;

            Series seriesPerformance = GetSeries(SeriesName, cArea, S3d, ChartType, changes);

            foreach (var XValue in DataDict.Keys)
            {
                var YValue = DataDict[XValue].ToString();

                DataPoint pointPerformance =
                    GetPoint(XValue, font, YValue, null);

                pointPerformance.Label = YValue;
                seriesPerformance.Points.Add(pointPerformance);
            }
            chartControl.Legends[0].Enabled = false;
            SetStyle(cArea, chartControl);

            chartControl.Series.Add(seriesPerformance);
            chartControl.SaveImage(ms);
            return ms;
        }
        #endregion
        #region 饼图
        /// <summary>
        /// 简单快捷获取饼图
        /// </summary>
        /// <param name="DataDict">数据集(X,Y轴值的集合)</param>
        /// <param name="xTitlename">x轴的TITLE</param>
        /// <param name="yTitleName">y轴的TITLE</param>
        /// <param name="tableTile">报表标题</param>
        /// <param name="subTitle">报表副标题</param>
        /// <param name="Width">报表宽</param>
        /// <param name="Height">报表高</param>
        /// <param name="YInterval">Y轴的间隔</param>
        /// <param name="imgType">图表类型(要柱状图请传0)</param>
        /// <returns>MemoryStream(报表)</returns>
        public MemoryStream GetPIEImg(
            Dictionary<string, double> DataDict,
            string xTitlename, string yTitleName,
            string tableTile, string subTitle,
            int Width,
            int Height,
            int YInterval,
            System.Web.UI.DataVisualization.Charting.SeriesChartType imgType,
             string SeriesName
            )
        {
            if (subTitle == null)
            {
                subTitle = "";
            }
            if (xTitlename == null) xTitlename = "";
            if (yTitleName == null) yTitleName = "";
            if (tableTile == null) tableTile = "";
            if (subTitle == null) subTitle = "";
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            if (DataDict == null) { ms = GetDefaultImg(); return ms; }
            Chart chartControl =
                GetChartControl(tableTile, subTitle, Width, Height,
                Docking.Bottom, true, BorderSkinStyle.None);

            ChartArea cArea =
                GetChartArea(tableTile, xTitlename, yTitleName, false, YInterval, 1);

            chartControl.ChartAreas.Add(cArea);
            cArea.AxisX.IsMarginVisible = true;
            System.Drawing.Font font =
                new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular);

            bool changes = true;//渐变
            string S3d = "Cylinder";//Cylinder

            var ChartType =
                System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
            if (imgType != 0)
                ChartType = imgType;
            string 圆环样式 = "Outside";
            string 圆环粗细 = "50";
            Series seriesPerformance = GetSeries("Default", cArea, S3d, ChartType, changes);
            foreach (var XValue in DataDict.Keys)
            {

                var YValue = DataDict[XValue].ToString();
                DataPoint pointPerformance =
                    GetPoint(XValue, font, YValue, XValue);
                pointPerformance.Label = YValue;
                seriesPerformance.Points.Add(pointPerformance);
            }
            chartControl.Series.Add(seriesPerformance);
            SetPIE(seriesPerformance, 圆环样式, 圆环粗细);
            chartControl.Legends[0].Enabled = true;
            SetStyle(cArea, chartControl);
            chartControl.ChartAreas[0].Area3DStyle.Enable3D = true;
            chartControl.SaveImage(ms);
            return ms;
        }


        #endregion
        #endregion


        #region 自定义获取
        public MemoryStream GetWeekResult(
    Dictionary<string, monthUser> UserSummary,
    string TableName, bool isMarginVisible, string CreationTime)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bool changes = true;//渐变
            string S3d = "";//Cylinder
            var ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.StackedArea;
            System.Web.UI.DataVisualization.Charting.Chart chartControl = GetChartControl(TableName,
                CreationTime, 750, 500, Docking.Bottom, true, BorderSkinStyle.None);
            ChartArea cArea = GetChartArea(TableName, " 用 户 ", " 总 结 情 况", false, 2, 1);
            chartControl.ChartAreas.Add(cArea);
            //本月总结完成量
            Series seriesSummaryCount = GetSeries("本月总结完成量", cArea, S3d, ChartType, changes);
            //本月计划完成量
            Series seriesPlanCount = GetSeries("本月计划完成量", cArea, S3d, ChartType, changes);
            //本月计划缺少量
            Series seriesPlanVac = GetSeries("本月应该完成总量", cArea, S3d, System.Web.UI.DataVisualization.Charting.SeriesChartType.Spline, false);
            cArea.AxisX.IsMarginVisible = isMarginVisible;
            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 6, System.Drawing.FontStyle.Regular);
            string AllfinishCount = "";
            foreach (var UserID in UserSummary.Keys)
            {
                var UserInfo = UserSummary[UserID];
                var x = UserInfo.UserName;
                AllfinishCount = UserInfo.getmonthFinishFull();
                DataPoint SummaryCountPoints = GetPoint(x, font, UserInfo.monthSummaryCount, null);
                seriesSummaryCount.Points.Add(SummaryCountPoints);
                DataPoint PlanCountPoints = GetPoint(x, font, UserInfo.monthPlanCount, null);
                seriesPlanCount.Points.Add(PlanCountPoints);
                DataPoint PointPlanVac = GetPoint(x, font, AllfinishCount, null);
                PointPlanVac.Label = "   ";
                seriesPlanVac.Points.Add(PointPlanVac);
            }
            seriesPlanVac.Name = "本月应该完成总量(" + AllfinishCount + ")";
            SetStyle(cArea, chartControl);
            seriesSummaryCount["StackedGroupName"] = "Group1";
            seriesPlanCount["StackedGroupName"] = "Group1";
            chartControl.Series.Add(seriesSummaryCount);
            chartControl.Series.Add(seriesPlanCount);
            chartControl.Series.Add(seriesPlanVac);
            chartControl.SaveImage(ms);
            return ms;
        }

        public MemoryStream GetPerformance(
            Dictionary<string, monthUser> UserSummary,
            string TableName, bool isMarginVisible, string CreationTime)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Chart chartControl = GetChartControl(TableName, CreationTime, 750, 500,
                Docking.Bottom, true, BorderSkinStyle.None);
            ChartArea cArea = GetChartArea(TableName, " 用 户 ", " 绩 效 情 况", false, 100, 1);
            chartControl.ChartAreas.Add(cArea);
            cArea.AxisX.IsMarginVisible = isMarginVisible;
            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular);

            bool changes = true;//渐变
            string S3d = "Cylinder";//Cylinder
            var ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;

            //绩效情况
            Series seriesPerformance = GetSeries("绩效", cArea, S3d, ChartType, changes);

            foreach (var UserID in UserSummary.Keys)
            {
                var UserInfo = UserSummary[UserID];
                var x = UserSummary[UserID].UserName.Replace("(", "").Replace(")", "");
                DataPoint pointPerformance = GetPoint(x, font, UserInfo.performance, "");
                pointPerformance.Label = "-" + UserInfo.performance;
                seriesPerformance.Points.Add(pointPerformance);
            }
            SetStyle(cArea, chartControl);
            chartControl.Series.Add(seriesPerformance);
            chartControl.SaveImage(ms);
            return ms;
        }

        public MemoryStream GetPerformance(
            Dictionary<string, string> MonthPerformanceDict,
            string TableName, bool isMarginVisible)
        {
            string CreationTime = DateTime.Now.ToShortDateString();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            Chart chartControl = GetChartControl(TableName, CreationTime, 650, 350,
                Docking.Bottom, true, BorderSkinStyle.None);
            ChartArea cArea = GetChartArea(TableName, " 月 份 ", "月 绩 效 统 计", false, 5000, 1);
            chartControl.ChartAreas.Add(cArea);
            cArea.AxisX.IsMarginVisible = isMarginVisible;
            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular);
            bool changes = true;//渐变
            string S3d = "Cylinder";//Cylinder
            var ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
            //绩效情况
            Series seriesPerformance = GetSeries("月绩效", cArea, S3d, ChartType, changes);
            foreach (var Month in MonthPerformanceDict.Keys)
            {
                var yearPerformanceSum = MonthPerformanceDict[Month];
                DataPoint pointPerformance = GetPoint(Month + "月", font, yearPerformanceSum, "");
                pointPerformance.Label = "-" + yearPerformanceSum;
                seriesPerformance.Points.Add(pointPerformance);
            }
            SetStyle(cArea, chartControl);
            chartControl.Series.Add(seriesPerformance);
            chartControl.SaveImage(ms);
            return ms;
        }

        public MemoryStream GetMonthSumer(
            Dictionary<string, int> UserSummary,
            string TableName, double tasktotal, string tasktotalTitle,
            string sumerTile, string CreationTime)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            double[] tasktotalValues = { };
            double[] sumeryValues = { };
            System.Web.UI.DataVisualization.Charting.Chart chartControl = GetChartControl(TableName, CreationTime, 750, 500,
                Docking.Bottom, true, BorderSkinStyle.None);
            ChartArea cArea = GetChartArea(TableName, " 用 户 ", " 完 成 情 况 ", false, 2, 1);
            chartControl.ChartAreas.Add(cArea);
            bool changes = true;//渐变
            string S3d = "Cylinder";//Cylinder
            Series s = GetSeries(sumerTile, cArea, S3d, System.Web.UI.DataVisualization.Charting.SeriesChartType.StackedArea, changes);
            Series tasktotals = GetSeries(tasktotalTitle, cArea, S3d, System.Web.UI.DataVisualization.Charting.SeriesChartType.Line, changes);
            System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 7, System.Drawing.FontStyle.Regular);
            foreach (var UserName in UserSummary.Keys)
            {
                var UserSummaryNum = UserSummary[UserName].ToString();
                DataPoint Points = GetPoint(UserName, font, UserSummaryNum, "");
                Points.Label = UserSummaryNum;
                s.Points.Add(Points);
                DataPoint PointsFull = GetPoint(UserName, font, tasktotal.ToString(), "");
                PointsFull.Label = "   ";
                tasktotals.Points.Add(PointsFull);
            }
            SetStyle(cArea, chartControl);
            chartControl.Series.Add(s);
            chartControl.Series.Add(tasktotals);
            chartControl.SaveImage(ms);
            return ms;
        }
        #endregion

    }

    #region 对象
    /// <summary>
    /// 本月单用户周/月/年/完成量(实体)
    /// </summary>
    public class monthUser
    {
        /// <summary>
        /// 用户姓名
        /// </summary>
        public string UserName;
        /// <summary>
        /// 本月计划完成量
        /// </summary>
        public string monthPlanCount;
        /// <summary>
        /// 本月总结完成量
        /// </summary>
        public string monthSummaryCount;
        /// <summary>
        /// 本月计划缺少量
        /// </summary>
        public string monthPlanVac;
        /// <summary>
        /// 本月总结缺少量
        /// </summary>
        public string monthSummaryVac;
        /// <summary>
        /// 本月完成总量
        /// </summary>
        public string monthCount;
        /// <summary>
        /// 本月缺少总量
        /// </summary>
        public string monthVac;
        /// <summary>
        /// 扣多少钱
        /// </summary>
        public string performance;
        /// <summary>
        /// 本月应该完成总量(缺少量+完成量)
        /// </summary>
        /// <returns></returns>
        public string getmonthFinishFull()
        {
            int monthFinishFull = System.Convert.ToInt32(monthVac) + System.Convert.ToInt32(monthCount);
            return monthFinishFull.ToString();
        }
    }
    #endregion
}

posted on 2012-06-13 15:13  wsenmin  阅读(572)  评论(0编辑  收藏  举报