0
点赞
收藏
分享

微信扫一扫

关于MsChart的封装与实现


C#开发的WEB开发的简单框架 节省开发信息管理系统时间 提高开发效率
构建一个WEB开发的基础框架(主要包括数据库处理、页面框架及工具类),形成ASP.NET信息管理系统快速开发架构。
一、框架实现了基于XML定制的列表查询及图表展现
二、列表实现了复杂查询条件、合并行列(分组求和的处理)、汇总行及钻取数据和图表的钻取
三、编辑配置支持单表数据的增加和修改,针对开发人员实现的编辑页面可方便扩展主表新增字段
使开发人员专注于系统的业务实现,节省开发时间,提高开发效率

试用登录​​http://121.18.78.216/​​

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.DataVisualization.Charting;
using MyQuery.Utils;
using System.Web.UI;
using System.Data;
using System.Collections;

namespace MyQuery.MyControl
{
    /// <summary>
    /// 封装的处理MsChart的对象类
    /// by 贾世义 2009-2-18
    /// </summary>
    [Serializable]
    public sealed class MyChart
    {
        private int _width = 600;
        /// <summary>
        /// 宽度 默认600
        /// </summary>
        public int Width
        {
            get
            {
                if (_width < 50)
                {
                    return 600;
                }
                else
                {
                    return _width;
                }
            }
            set { _width = value; }
        }
        private int _height = 500;
        /// <summary>
        /// 高度 默认500
        /// </summary>
        public int Height
        {
            get
            {
                if (_height < 50)
                {
                    return 500;
                }
                else
                {
                    return _height;
                }
            }
            set { _height = value; }
        }
        /// <summary>
        /// 外观配置模板
        /// </summary>
        public string Template = null;
        /// <summary>
        /// 标题集合
        /// </summary>
        public List<ChartTitle> Titles = null;
        /// <summary>
        /// 对应图像ChartArea的MyArea集合
        /// </summary>
        public List<MyArea> MyAreas = null;
        /// <summary>
        /// 数据源
        /// </summary>
        public object DataSource = null;
        /// <summary>
        /// 处理的数据列
        /// </summary>
        public MyColumns DataColumns = null;
        /// <summary>
        /// 查询对应的名称
        /// </summary>
        public string Name = null;
        /// <summary>
        /// 当期用户ID
        /// </summary>
        public string UserAccount = null;
        /// <summary>
        /// 计算使用的脚本
        /// </summary>
        public string CalcScript = null;
        /// <summary>
        /// 获得图形对象
        /// </summary>
        /// <param name="page">页面实例</param>
        /// <param name="type">输出类型</param>
        /// <returns></returns>
        public Chart GetChart(Page page, RenderType type)
        {
            Chart result = new Chart();
            //图像属性
            result.RenderType = type;
            if (type == RenderType.ImageTag)
            {
                result.ImageStorageMode = ImageStorageMode.UseImageLocation;
                result.ImageLocation = "~/Img/temp/ChartPic_#SEQ(300,1)";
            }
            result.AlternateText = "分析图形";
            result.Attributes.Add("align", "center");
            result.Width = Width;
            result.Height = Height;
            if (String.IsNullOrEmpty(Template))
            {
                result.Serializer.Content = SerializationContents.Default;
            }
            else
            {
                result.LoadTemplate(Template);
            }
            if (Titles != null)
            {
                //标题
                foreach (ChartTitle obj in Titles)
                {
                    result.Titles.Add(new Title(obj.Text, obj.Docking));
                }
            }
            if (MyAreas == null || MyAreas.Count == 0)
            {
                throw new Exception("图像的区域配置不能为空");
            }
            foreach (MyArea myArea in MyAreas)
            {
                //图像区域
                ChartArea item = new ChartArea(myArea.Name);
                item.Area3DStyle.Enable3D = myArea.IsShow3D;
                item.AxisX.IsLabelAutoFit = true;
                item.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont | LabelAutoFitStyles.IncreaseFont | LabelAutoFitStyles.WordWrap;
                if (!String.IsNullOrEmpty(myArea.XDataFormat))
                {
                    item.AxisX.LabelStyle.Format = myArea.XDataFormat;
                }
                if (!String.IsNullOrEmpty(myArea.YDataFormat))
                {
                    item.AxisY.LabelStyle.Format = myArea.YDataFormat;
                }
                //绑定数据
                //图例
                if (myArea.Lengend != null && !IsExit(result.Legends, myArea.Lengend.Name))
                {
                    Legend legend = new Legend(myArea.Lengend.Name);
                    if (!String.IsNullOrEmpty(myArea.Lengend.Title))
                    {
                        legend.Title = myArea.Lengend.Title;
                    }
                    legend.Docking = myArea.Lengend.Docking;
                    legend.Enabled = true;
                    result.Legends.Add(legend);
                }
                //数据及系列
                if (myArea.IsDataTable && DataSource != null)
                {
                    result.DataBindTable(((DataTable)DataSource).DefaultView, myArea.XField);
                }
                if (myArea.MySeries != null)
                {
                    foreach (MySerie mySerie in myArea.MySeries)
                    {
                        Series serie = new Series(mySerie.Name);
                        serie.ChartArea = myArea.Name;
                        serie.ChartType = mySerie.ChartType;
                        if (myArea.Lengend != null)
                        {
                            serie.Legend = myArea.Lengend.Name;
                            if (!String.IsNullOrEmpty(mySerie.Title))
                            {
                                serie.LegendText = mySerie.Title;
                            }
                        }
                        serie.IsValueShownAsLabel = mySerie.IsValueShownAsLabel;
                        if (mySerie.IsValueShownAsLabel && !String.IsNullOrEmpty(mySerie.DataFormat))
                        {
                            serie.LabelFormat = mySerie.DataFormat;
                        }
                        serie.XValueType = mySerie.XValueType;
                        serie.YValueType = mySerie.YValueType;
                        if (!myArea.IsDataTable && mySerie.ColumnNames != null)
                        {
                            //自身数据优先
                            object ds = null;
                            if (mySerie.DataSource == null)
                            {
                                ds = DataSource;
                            }
                            else
                            {
                                ds = mySerie.DataSource;
                            }
                            if (ds == null && DataColumns != null)
                            {
                                //单条数据
                                foreach (string name in mySerie.ColumnNames)
                                {
                                    MyColumn myColumn = DataColumns[name];
                                    if (myColumn != null)
                                    {
                                        DataPoint dataPoint = new DataPoint();
                                        if (!String.IsNullOrEmpty(myColumn.Title))
                                        {
                                            dataPoint.AxisLabel = myColumn.Title;
                                        }
                                        dataPoint.SetValueY(DataHelper.GetDoubleValue(myColumn.Value, 0));
                                        if (!String.IsNullOrEmpty(mySerie.UrlFormatString))
                                        {
                                            string url = null;
                                            if (!String.IsNullOrEmpty(mySerie.UserFields))
                                            {
                                                url = String.Format(mySerie.UrlFormatString, ReflectionDeal.GetUseFieldsValue(DataColumns, null, DataHelper.GetStrings(mySerie.UserFields)));
                                            }
                                            else
                                            {
                                                url = mySerie.UrlFormatString;
                                            }
                                            dataPoint.Url = WebHelper.GetUrl(url);
                                            dataPoint.MapAreaAttributes = "target=/"_blank/"";
                                        }
                                        if (mySerie.IsToolTip)
                                        {
                                            dataPoint.ToolTip = dataPoint.AxisLabel + ":" + DataHelper.GetString(dataPoint.YValues, mySerie.DataFormat);
                                        }
                                        serie.Points.Add(dataPoint);
                                    }
                                }
                            }
                            else if (ds != null)
                            {
                                IList list = ReflectionDeal.GetDisplayList(ds);
                                List<int> iList = new List<int>();
                                if (myArea.RowIndexs == null)
                                {
                                    for (int i = 0; i < list.Count; i++)
                                    {
                                        iList.Add(i);
                                    }
                                }
                                foreach (int i in iList)
                                {
                                    DataPoint dataPoint = new DataPoint();
                                    object label = ReflectionDeal.GetValue(list[i], myArea.XField);
                                    if (!DataHelper.IsNullOrEmpty(label))
                                    {
                                        dataPoint.AxisLabel = label.ToString();
                                    }
                                    dataPoint.YValues = new double[mySerie.ColumnNames.Length];
                                    for (int j = 0; j < mySerie.ColumnNames.Length; j++)
                                    {
                                        if (DataColumns == null)
                                        {
                                            dataPoint.YValues[j] = DataHelper.GetDoubleValue(ReflectionDeal.GetValue(list[i], mySerie.ColumnNames[j]), 0);
                                        }
                                        else
                                        {
                                            MyColumn myColumn = DataColumns[mySerie.ColumnNames[j]];
                                            if (myColumn != null)
                                            {
                                                DataColumns.SetColumnsValue(Name, list[i], CalcScript, DataSource, UserAccount, 0);
                                                dataPoint.YValues[j] = DataHelper.GetDoubleValue(myColumn.Value, 0);
                                            }
                                        }
                                    }
                                    if (!String.IsNullOrEmpty(mySerie.UrlFormatString))
                                    {
                                        string url = null;
                                        if (!String.IsNullOrEmpty(mySerie.UserFields))
                                        {
                                            url = String.Format(mySerie.UrlFormatString, ReflectionDeal.GetUseFieldsValue(DataColumns, null, DataHelper.GetStrings(mySerie.UserFields)));
                                        }
                                        else
                                        {
                                            url = mySerie.UrlFormatString;
                                        }
                                        dataPoint.Url = WebHelper.GetUrl(url);
                                        dataPoint.MapAreaAttributes = "target=/"_blank/"";
                                    }
                                    if (mySerie.IsToolTip)
                                    {
                                        dataPoint.ToolTip = dataPoint.AxisLabel + ":" + DataHelper.GetString(dataPoint.YValues, mySerie.DataFormat);
                                    }
                                    serie.Points.Add(dataPoint);
                                }
                            }
                        }
                        result.Series.Add(serie);
                    }
                }
                result.ChartAreas.Add(item);
            }
            result.Page = page;
            return result;
        }
        /// <summary>
        /// 判断是否已经存在
        /// </summary>
        /// <param name="legends"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private bool IsExit(LegendCollection legends, string name)
        {
            if (legends == null || legends.Count == 0)
            {
                return false;
            }
            foreach (Legend legend in legends)
            {
                if (legend.Name.Equals(name))
                {
                    return true;
                }
            }
            return false;
        }
    }

    /// <summary>
    /// 封装的处理MsChart的Title对象类
    /// by 贾世义 2009-2-18
    /// </summary>
    [Serializable]
    public sealed class ChartTitle
    {
        /// <summary>
        /// 标题文本
        /// </summary>
        public string Text = null;
        /// <summary>
        /// 位置
        /// </summary>
        public Docking Docking = Docking.Top;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="text"></param>
        /// <param name="docking"></param>
        public ChartTitle(string text, string docking)
        {
            Text = text;
            Docking = ChartHelper.GetDocking(docking);
        }
    }

    /// <summary>
    /// 封装的处理MsChart的ChartArea对象类
    /// by 贾世义 2009-2-18
    /// </summary>
    [Serializable]
    public sealed class MyArea
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name = null;
        /// <summary>
        /// 是否显示为3D
        /// </summary>
        public bool IsShow3D = false;
        /// <summary>
        /// XValue的展示格式
        /// </summary>
        public string XDataFormat = null;
        /// <summary>
        /// YValue的展示格式
        /// </summary>
        public string YDataFormat = null;
        /// <summary>
        /// 数据来源是否为DataTable
        ///     当true时 仅支持DataTable中的数据 如不指定serie节 则将xfield列作为横坐标 其余列各作为一个serie,否则按照指定的列生成serie
        ///     当false时 如指定了rows属性则只按照指定的行索引,否则取全部数据生成图 支持IList和计算列 其它同datatable
        /// </summary>
        public bool IsDataTable = true;
        /// <summary>
        /// 指定的数据行 搭配isdatatable=false使用 指定行的索引号的数组
        /// </summary>
        public List<int> RowIndexs = null;
        /// <summary>
        /// 横坐标对应字段名称
        /// </summary>
        public string XField = null;
        /// <summary>
        /// 图例
        /// </summary>
        public MyLengend Lengend = null;
        /// <summary>
        /// 对应数据系列Serie的集合
        /// </summary>
        public List<MySerie> MySeries = null;
    }

    /// <summary>
    /// 封装的处理MsChart的Lengend对象类
    /// by 贾世义 2009-2-18
    /// </summary>
    [Serializable]
    public sealed class MyLengend
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name = null;
        /// <summary>
        /// 标题文本
        /// </summary>
        public string Title = null;
        /// <summary>
        /// 位置
        /// </summary>
        public Docking Docking = Docking.Right;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="docking"></param>
        /// <param name="title"></param>
        public MyLengend(string name, string docking, string title)
        {
            Name = name;
            if (String.IsNullOrEmpty(docking))
            {
                Docking = Docking.Right;
            }
            else
            {
                Docking = ChartHelper.GetDocking(docking);
            }
            Title = title;
        }
    }

    /// <summary>
    /// 封装的处理MsChart的ChartSerie对象类
    /// by 贾世义 2009-2-18
    /// </summary>
    [Serializable]
    public sealed class MySerie
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name = null;
        /// <summary>
        /// 序列的标题作为Legend的文本
        /// </summary>
        public string Title = null;
        /// <summary>
        /// 图像类型
        /// </summary>
        public SeriesChartType ChartType = SeriesChartType.Column;
        /// <summary>
        /// 是否显示值
        /// </summary>
        public bool IsValueShownAsLabel = false;
        /// <summary>
        /// label的显示格式
        /// </summary>
        public string DataFormat = null;
        /// <summary>
        /// 链接URL格式
        /// </summary>
        public string UrlFormatString = null;
        /// <summary>
        /// 链接URL对应的参数字段
        /// </summary>
        public string UserFields = null;
        /// <summary>
        /// 是否显示ToolTip
        /// </summary>
        public bool IsToolTip = true;
        /// <summary>
        /// X轴值展示数据类型
        /// </summary>
        public ChartValueType XValueType = ChartValueType.Auto;
        /// <summary>
        /// Y轴值展示数据类型
        /// </summary>
        public ChartValueType YValueType = ChartValueType.Auto;
        /// <summary>
        /// 值表达式必须设置为对应select节的column的name,当多个YValue时name可以用,分割
        /// </summary>
        public string[] ColumnNames = null;
        /// <summary>
        /// 系列自己独立的数据源
        /// </summary>
        public object DataSource = null;
    }
}

举报

相关推荐

0 条评论