现在的位置: 首页 > 综合 > 正文

刚搞出一个好东西哟,识货的赶紧下走

2013年01月28日 ⁄ 综合 ⁄ 共 11013字 ⁄ 字号 评论关闭

 

View Code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Web.UI.DataVisualization.Charting;
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(2659105);
        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(2659105)
         );
        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)
    {
        DataPoint point = new DataPoint();
        point["LabelStyle"] = "Top";
        point.Label = pointValue;
        point.SetValueXY(xName, pointValue);
        if (pointValue == "0")
        {
            point.Label = "  ";
        }
        point.IsValueShownAsLabel = true;
        point.LabelForeColor = Color.Black;
        point.LabelBackColor = Color.Transparent;
        point.LabelBorderColor = Color.Transparent;
        point.Font = font;
        return point;
    }

    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, 750500,
            Docking.Bottom, true, BorderSkinStyle.None);
        ChartArea cArea = GetChartArea("温馨提示:没有数据"" 用 户 "" 绩 效 情 况"false1001);
        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;
    }
    #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>
    
/// <returns>MemoryStream(报表)</returns>
    public MemoryStream GetColumnImg(
        Dictionary<stringdouble> DataDict,
        string xTitlename, string yTitleName,
        string tableTile, string subTitle,
        int Width,
        int Height,
        int YInterval
        )
    {
        if (subTitle == null)
        {
            subTitle = "";
        }
        if (DataDict == nullreturn null;
        if (xTitlename == null) xTitlename = "";
        if (yTitleName == null) yTitleName = "";
        if (tableTile == null) tableTile = "";
        if (subTitle == null) subTitle = "";
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        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;
        Series seriesPerformance = GetSeries("", cArea, S3d, ChartType, changes);
        foreach (var Month in DataDict.Keys)
        {
            var yearPerformanceSum = DataDict[Month].ToString();
            DataPoint pointPerformance = GetPoint(Month, font, yearPerformanceSum);
            pointPerformance.Label = yearPerformanceSum;
            seriesPerformance.Points.Add(pointPerformance);
        }
        chartControl.Legends[0].Enabled = false;
        SetStyle(cArea, chartControl);
        chartControl.Series.Add(seriesPerformance);
        chartControl.SaveImage(ms);
        return ms;
    }
    #endregion  
}

 

调用需要提供数据哦 用法如下:

 

 

      protected void Page_Load(object sender, EventArgs e)
        {
            MSChartHelpe cxh = new MSChartHelpe();
            MemoryStream ms = new MemoryStream();  
            string taskreport = @"。。。。。。";
            XDocument taskreportxd = XDocument.Load(taskreport);
            if (taskreportxd == null )
            {
                getDefaultImg(cxh);
                return;
            }
。。。。。。。。 
            var DataDict = new Dictionary<stringdouble>(); 
。。。。。。。
 
            ms = cxh.GetColumnImg(DataDict,  "",  "","","",500,400,50);
            Context.Response.Clear();
            Context.Response.ContentType = "image/jpeg";
            Context.Response.AddHeader("Cache-Control""max-age=3600");
            ms.WriteTo(Context.Response.OutputStream);
        }
        private void getDefaultImg(MSChartHelpe cxh)
        {
            MemoryStream ms = cxh.GetDefaultImg();
            Response.Clear();
            Response.ContentType = "image/jpeg";
            Response.AddHeader("Cache-Control""max-age=3600");
            ms.WriteTo(Response.OutputStream);
        }

 

 

 

抱歉!评论已关闭.