示例#1
0
        public override Dictionary <string, object> Render(string parameters = "", bool IsMobile = false)
        {
            this.FillDataSource(parameters);
            var result = this.BlockDef;

            result.SetValue("Title", this.BlockDef.GetValue("MainTitle"));
            result.SetValue("ShowTitle", this.BlockDef.GetValue("ShowTitle"));
            if (String.IsNullOrEmpty(result.GetValue("chartHeight")))
            {
                result.SetValue("chartHeight", "170px");
            }
            this.setStyle(result);

            #region 设置图表JSON
            var       chartData      = new Dictionary <string, object>();
            var       dataSourceName = HttpContext.Current.Request["DataSourceName"];
            DataTable dataSource     = new DataTable();
            if (String.IsNullOrEmpty(dataSourceName))
            {
                dataSource = this.DataSource.FirstOrDefault().Value;
            }
            else
            {
                dataSource = this.DataSource.GetValue <DataTable>(dataSourceName);
            }
            if (dataSource == null)
            {
                throw new Exception("没有找到指定的数据源");
            }
            if (!dataSource.Columns.Contains("name") || !dataSource.Columns.Contains("value"))
            {
                throw new Exception("数据源必须有name 和 value 列");
            }
            var tooltip = new Dictionary <string, object>();
            tooltip.SetValue("headerFormat", "{point.name}<br>");
            tooltip.SetValue("pointFormat", "{point.name}: <b>{point." + this.BlockDef.GetValue("tooltipValue") + ":.1f}" + this.BlockDef.GetValue("tooltipValueUnit") + "</b>");
            chartData.SetValue("tooltip", tooltip);

            var plotOptions = new Dictionary <string, object>();
            var pieOpt      = new Dictionary <string, object>();
            var dataLabels  = new Dictionary <string, object>();
            dataLabels.SetValue("format", "<b>{point.name}</b>: {point." + this.BlockDef.GetValue("plotValue") + ":.1f} " + this.BlockDef.GetValue("plotValueUnit") + "");
            if (this.BlockDef.GetValue("showInLegend").ToLower() == true.ToString().ToLower())
            {
                dataLabels.SetValue("enabled", false);
                pieOpt.SetValue("showInLegend", true);
                var legend = new Dictionary <string, object>();
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("layout")))
                {
                    legend.SetValue("layout", this.BlockDef.GetValue("layout"));
                }
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("align")))
                {
                    legend.SetValue("align", this.BlockDef.GetValue("align"));
                }
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("verticalAlign")))
                {
                    legend.SetValue("verticalAlign", this.BlockDef.GetValue("verticalAlign"));
                }
                chartData.SetValue("legend", legend);
            }
            pieOpt.SetValue("dataLabels", dataLabels);
            plotOptions.SetValue("pie", pieOpt);
            chartData.SetValue("plotOptions", plotOptions);
            var dataList = new List <Dictionary <string, object> >();
            foreach (DataRow row in dataSource.Rows)
            {
                var dic = Formula.FormulaHelper.DataRowToDic(row);
                dic.SetValue("y", row["value"]);
                dataList.Add(dic);
            }

            #region 设置过滤
            var filters = JsonHelper.ToList(this.BlockDef.GetValue("FilterInfo"));
            foreach (var filter in filters)
            {
                var enumData = filter.GetValue("EnumData");
                if (!String.IsNullOrEmpty(enumData))
                {
                    if (enumData.IndexOf("{") >= 0)
                    {
                        var enumList = JsonHelper.ToList(enumData);
                        foreach (var item in enumList)
                        {
                            if (String.IsNullOrEmpty(item.GetValue("IsDefault")))
                            {
                                item.SetValue("IsDefault", "false");
                            }
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                    else
                    {
                        IEnumService enumService = FormulaHelper.GetService <IEnumService>();
                        var          enumList    = JsonHelper.ToList(JsonHelper.ToJson(enumService.GetEnumDataSource(enumData.ToString())));
                        foreach (var item in enumList)
                        {
                            item.SetValue("IsDefault", "false");
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                }
            }
            result.SetValue("FilterInfo", filters);
            #endregion

            var seriesList = new List <Dictionary <string, object> >();
            var series     = new Dictionary <string, object>();
            series.SetValue("data", dataList);
            seriesList.Add(series);
            result.SetValue("ID", this.ID);
            chartData.SetValue("series", seriesList);
            result.SetValue("chartData", chartData);
            result.SetValue("chartId", this.ID + "_PieChart");
            #endregion
            return(result);
        }
示例#2
0
        public ActionResult PageView()
        {
            string tmpCode = this.GetQueryString("TmplCode");

            var config = this.entities.Set <S_UI_BIConfig>().FirstOrDefault(d => d.Code == tmpCode);

            if (config == null)
            {
                throw new Exception("未能找到编号为【" + tmpCode + "】的BI定义对象");
            }
            var layout = JsonHelper.ToList(config.Layout);
            var blocks = JsonHelper.ToList(config.Blocks);

            foreach (var item in blocks)
            {
                //去除DataSource属性不在页面数据岛上显示,以避免防止SQL注入攻击
                var settings = JsonHelper.ToObject(item.GetValue("Settings"));
                settings.Remove("dataSource");
                if (JsonHelper.ToJson(settings).IndexOf("FilterInfo") >= 0)
                {
                    var FilterInfo = JsonHelper.ToList(settings["FilterInfo"].ToString());
                    for (int i = 0; i < FilterInfo.Count(); i++)
                    {
                        if (JsonHelper.ToJson(FilterInfo[i]).IndexOf("EnumData") >= 0 && !string.IsNullOrEmpty(FilterInfo[i]["EnumData"].ToString()) && FilterInfo[i]["EnumData"].ToString().IndexOf("{") < 0)
                        {
                            IEnumService enumService = FormulaHelper.GetService <IEnumService>();
                            FilterInfo[i]["EnumData"] = JsonHelper.ToJson(enumService.GetEnumDataSource(FilterInfo[i]["EnumData"].ToString()));
                        }
                    }
                    settings["FilterInfo"] = FilterInfo;
                }
                item.SetValue("Settings", JsonHelper.ToJson(settings));
            }
            ViewBag.LayOutInfo   = layout;
            ViewBag.Blocks       = blocks;
            ViewBag.PageTitle    = "";
            ViewBag.LayOutJson   = config.Layout;
            ViewBag.BlocksJson   = JsonHelper.ToJson(blocks);
            ViewBag.RowsCount    = layout.Count;
            ViewBag.ColumnsCount = layout.Count == 0 ? 0 : layout.Max(c => Convert.ToInt32(c["ColumnNumber"]));
            var templateInfo = new Dictionary <string, object>();

            foreach (var item in blocks)
            {
                var    BlockType = item.GetValue("BlockType");
                string filePath  = HttpContext.Server.MapPath(String.Format("/MvcConfig/Scripts/BI/template/div/{0}.html", BlockType));
                if (System.IO.File.Exists(filePath))
                {
                    FileStream   fs      = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    StreamReader reader  = new StreamReader(fs);
                    var          divHtml = reader.ReadToEnd();
                    reader.Close();
                    fs.Close();
                    item.SetValue("divHtml", MvcHtmlString.Create(divHtml));
                }
                if (!templateInfo.ContainsKey(BlockType))
                {
                    string templatefilePath = HttpContext.Server.MapPath(String.Format("/MvcConfig/Scripts/BI/template/templ/{0}.html", BlockType));
                    if (System.IO.File.Exists(templatefilePath))
                    {
                        FileStream   tmplfs     = new FileStream(templatefilePath, FileMode.Open, FileAccess.Read);
                        StreamReader tmplreader = new StreamReader(tmplfs);
                        var          tmplHtml   = tmplreader.ReadToEnd();
                        tmplreader.Close();
                        tmplfs.Close();
                        templateInfo.SetValue(BlockType, MvcHtmlString.Create(tmplHtml));
                    }
                }
            }
            ViewBag.Script        = HttpContext.Server.HtmlDecode(config.ScriptText);
            ViewBag.BlockTemplate = templateInfo;
            ViewBag.FullScreen    = false;
            if (this.GetQueryString("FullScreen").Trim().ToLower() == true.ToString().ToLower())
            {
                ViewBag.FullScreen = true;
            }
            return(View());
        }
示例#3
0
        public ActionResult MobileView()
        {
            string tmpCode = this.GetQueryString("TmplCode");
            string title   = this.GetQueryString("Title");

            if (!string.IsNullOrEmpty(title))
            {
                ViewBag.Title = title;
            }
            else
            {
                ViewBag.Title = "图表";
            }
            string tokenKey = !String.IsNullOrEmpty(ConfigurationManager.AppSettings["TokenKey"]) ? ConfigurationManager.AppSettings["TokenKey"] : "GWToken";
            var    token    = this.GetQueryString(tokenKey);

            if (!String.IsNullOrEmpty(token))
            {
                //根据密钥解析token,获取用户信息
                var secretKey = String.IsNullOrEmpty(ConfigurationManager.AppSettings["SecretKey"]) ? String.Empty : ConfigurationManager.AppSettings["SecretKey"];
                if (String.IsNullOrEmpty(secretKey))
                {
                    throw new Formula.Exceptions.BusinessException("系统未设置密钥,进行验证操作");
                }
                JWT.IJsonSerializer   serializer = new JWT.Serializers.JsonNetSerializer();
                JWT.IDateTimeProvider provider   = new JWT.UtcDateTimeProvider();
                JWT.IJwtValidator     validator  = new JWT.JwtValidator(serializer, provider);
                JWT.IBase64UrlEncoder urlEncoder = new JWT.JwtBase64UrlEncoder();
                JWT.IJwtDecoder       decoder    = new JWT.JwtDecoder(serializer, validator, urlEncoder);
                //var key = Convert.ToBase64String(Encoding.UTF8.GetBytes(secretKey));
                var json    = decoder.Decode(token, secretKey, verify: true);
                var dic     = Formula.Helper.JsonHelper.ToObject(json);
                var sysName = dic.GetValue("systemName");

                //判断是否过期
                bool isExpire = false;
                if (!String.IsNullOrEmpty(dic.GetValue("ExpiredTime")))
                {
                    //兼容以前用自定义字段判断过期时间的写法,目前使用JWT固有的判断超时的字段“exp”,JWT校验的时候会失效,不用写代码判断是否失效
                    var expirationTime = String.IsNullOrEmpty(dic.GetValue("ExpiredTime")) ? DateTime.Now : Convert.ToDateTime(dic.GetValue("ExpiredTime"));
                    isExpire = DateTime.Now > expirationTime;
                }
                FormulaHelper.SetAuthCookie(sysName);
            }
            else
            {
                var sysName = this.GetQueryString("Account");
                FormulaHelper.SetAuthCookie(sysName);
                if (!Formula.AuthCodeHelper.CheckTokenRole() && !string.IsNullOrEmpty(sysName))
                {
                    return(Redirect(HttpContext.Request.Url.ToString()));
                }
            }


            var config = this.entities.Set <S_UI_BIConfig>().FirstOrDefault(d => d.Code == tmpCode);

            if (config == null)
            {
                throw new Exception("未能找到编号为【" + tmpCode + "】的BI定义对象");
            }
            var layout = JsonHelper.ToList(config.Layout);
            var blocks = JsonHelper.ToList(config.Blocks);

            foreach (var item in blocks)
            {
                //去除DataSource属性不在页面数据岛上显示,以避免防止SQL注入攻击
                var settings = JsonHelper.ToObject(item.GetValue("Settings"));
                settings.Remove("dataSource");
                if (JsonHelper.ToJson(settings).IndexOf("FilterInfo") >= 0)
                {
                    var FilterInfo = JsonHelper.ToList(settings["FilterInfo"].ToString());
                    for (int i = 0; i < FilterInfo.Count(); i++)
                    {
                        if (JsonHelper.ToJson(FilterInfo[i]).IndexOf("EnumData") >= 0 && !string.IsNullOrEmpty(FilterInfo[i]["EnumData"].ToString()) && FilterInfo[i]["EnumData"].ToString().IndexOf("{") < 0)
                        {
                            IEnumService enumService = FormulaHelper.GetService <IEnumService>();
                            FilterInfo[i]["EnumData"] = JsonHelper.ToJson(enumService.GetEnumDataSource(FilterInfo[i]["EnumData"].ToString()));
                        }
                    }
                    settings["FilterInfo"] = FilterInfo;
                }
                item.SetValue("Settings", JsonHelper.ToJson(settings));
            }
            ViewBag.LayOutInfo   = layout;
            ViewBag.Blocks       = blocks;
            ViewBag.PageTitle    = "";
            ViewBag.LayOutJson   = config.Layout;
            ViewBag.BlocksJson   = JsonHelper.ToJson(blocks);
            ViewBag.RowsCount    = layout.Count;
            ViewBag.ColumnsCount = layout.Count == 0 ? 0 : layout.Max(c => Convert.ToInt32(c["ColumnNumber"]));
            var templateInfo = new Dictionary <string, object>();

            foreach (var item in blocks)
            {
                var    BlockType = item.GetValue("BlockType");
                string filePath  = HttpContext.Server.MapPath(String.Format("/MvcConfig/Scripts/BI/template/div/{0}.html", BlockType));
                if (System.IO.File.Exists(filePath))
                {
                    FileStream   fs      = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    StreamReader reader  = new StreamReader(fs);
                    var          divHtml = reader.ReadToEnd();
                    reader.Close();
                    fs.Close();
                    item.SetValue("divHtml", MvcHtmlString.Create(divHtml));
                }
                if (!templateInfo.ContainsKey(BlockType))
                {
                    string templatefilePath = HttpContext.Server.MapPath(String.Format("/MvcConfig/Scripts/BI/template/templ/{0}.html", BlockType));
                    if (System.IO.File.Exists(templatefilePath))
                    {
                        FileStream   tmplfs     = new FileStream(templatefilePath, FileMode.Open, FileAccess.Read);
                        StreamReader tmplreader = new StreamReader(tmplfs);
                        var          tmplHtml   = tmplreader.ReadToEnd();
                        tmplreader.Close();
                        tmplfs.Close();
                        templateInfo.SetValue(BlockType, MvcHtmlString.Create(tmplHtml));
                    }
                }
            }
            ViewBag.Script        = HttpContext.Server.HtmlDecode(config.ScriptText);
            ViewBag.BlockTemplate = templateInfo;
            ViewBag.FullScreen    = false;
            if (this.GetQueryString("FullScreen").Trim().ToLower() == true.ToString().ToLower())
            {
                ViewBag.FullScreen = true;
            }
            return(View());
        }
示例#4
0
        public override Dictionary <string, object> Render(string parameters = "", bool IsMobile = false)
        {
            this.FillDataSource(parameters);
            var result = this.BlockDef;

            result.SetValue("Title", this.BlockDef.GetValue("MainTitle"));
            result.SetValue("ShowTitle", this.BlockDef.GetValue("ShowTitle"));
            if (String.IsNullOrEmpty(result.GetValue("chartHeight")))
            {
                result.SetValue("chartHeight", "120px");
            }
            var dataSourceName = this.BlockDef.GetValue("InUserDataSource");
            var targetSourceName = this.BlockDef.GetValue("TargetDataSource");
            var dataSource = this.DataSource.GetValue <DataTable>(dataSourceName);
            var targetDataSource = this.DataSource.GetValue <DataTable>(targetSourceName);
            var fieldName = this.BlockDef.GetValue("FieldName").Trim();
            var targetFieldName = this.BlockDef.GetValue("TargetFieldName").Trim();
            var chartUnit = this.BlockDef.GetValue("chartUnit");
            var value = 0m; var targetValue = 0m;

            if (!dataSource.Columns.Contains(fieldName))
            {
                throw new Exception("数据源中没有【" + fieldName + "】字段");
            }
            if (!targetDataSource.Columns.Contains(targetFieldName))
            {
                throw new Exception("目标数据源中没有【" + targetFieldName + "】字段");
            }
            if (dataSource.Rows.Count > 0)
            {
                value = dataSource.Rows[0][fieldName] == null || dataSource.Rows[0][fieldName] == DBNull.Value ? 0m :
                        Convert.ToDecimal(dataSource.Rows[0][fieldName]);
            }
            if (targetDataSource != null && targetDataSource.Columns.Contains(targetFieldName) && targetDataSource.Rows.Count > 0)
            {
                targetValue = targetDataSource.Rows[0][targetFieldName] == null || targetDataSource.Rows[0][targetFieldName] == DBNull.Value ? 0m :
                              Convert.ToDecimal(targetDataSource.Rows[0][targetFieldName]);
            }
            if (targetValue <= 0)
            {
                targetValue = value + 100;
            }
            result.SetValue("ActualValue", value);
            result.SetValue("ID", this.ID);
            result.SetValue("TargetValue", targetValue);
            var Rate = targetValue == 0 ? 0 : Math.Round(value / targetValue * 100);

            result.SetValue("CompletionAmount", Rate);
            var colorClass = "color4";

            if (Rate > 0 && Rate < 25)
            {
                colorClass = "color1";
            }
            else if (Rate >= 25 && Rate < 50)
            {
                colorClass = "color2";
            }
            else if (Rate >= 50 && Rate < 75)
            {
                colorClass = "color3";
            }

            result.SetValue("ProgressColor", colorClass);

            #region 设置过滤
            var filters = JsonHelper.ToList(this.BlockDef.GetValue("FilterInfo"));
            foreach (var filter in filters)
            {
                var enumData = filter.GetValue("EnumData");
                if (!String.IsNullOrEmpty(enumData))
                {
                    if (enumData.IndexOf("{") >= 0)
                    {
                        var enumList = JsonHelper.ToList(enumData);
                        foreach (var item in enumList)
                        {
                            if (String.IsNullOrEmpty(item.GetValue("IsDefault")))
                            {
                                item.SetValue("IsDefault", "false");
                            }
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                    else
                    {
                        IEnumService enumService = FormulaHelper.GetService <IEnumService>();
                        var          enumList    = JsonHelper.ToList(JsonHelper.ToJson(enumService.GetEnumDataSource(enumData.ToString())));
                        foreach (var item in enumList)
                        {
                            item.SetValue("IsDefault", "false");
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                }
            }
            result.SetValue("FilterInfo", filters);
            #endregion

            var   content = this.BlockDef.GetValue("Content");
            var   rowItem = new Dictionary <string, object>();
            Regex reg     = new Regex(@"(?<=\{)[^}]*(?=\})");
            foreach (Match m in reg.Matches(content))
            {
                string key = m.Value.Trim('{', '}');
                if (dataSource.Rows.Count > 0 && dataSource.Columns.Contains(key))
                {
                    rowItem.Add(key, dataSource.Rows[0][key]);
                }
            }
            result.SetValue("Content", this.ReplaceRegString(content, rowItem, null));

            this.setStyle(result);

            #region 设置图表JSON
            var chartData  = new Dictionary <string, object>();
            var seriesList = new List <Dictionary <string, object> >();
            var series     = new Dictionary <string, object>();
            var yAxis      = new Dictionary <string, object>();
            yAxis.SetValue("min", 0);
            yAxis.SetValue("max", targetValue);
            var yAxisTitle = new Dictionary <string, object>();
            yAxisTitle.SetValue("y", 15);
            yAxisTitle.SetValue("text", result.GetValue("Content"));
            yAxis.SetValue("title", yAxisTitle);
            chartData.SetValue("yAxis", yAxis);

            var dataArray = new ArrayList();
            dataArray.Add(value);
            //var titleDic = new Dictionary<string, object>();
            //titleDic.SetValue("text", Rate + "%");
            series.SetValue("data", dataArray);
            var dataLabels = new Dictionary <string, object>();
            dataLabels.SetValue("inside", "true");
            dataLabels.SetValue("format", "<div style=\"text-align:top\"><span style=\"font-family:Microsoft YaHei;font-size:25px;color:black\">{y}</span><span style=\"font-size:12px;color:silver\">" + chartUnit + "</span></div>");
            series.SetValue("dataLabels", dataLabels);
            seriesList.Add(series);
            chartData.SetValue("series", seriesList);
            result.SetValue("chartData", chartData);
            result.SetValue("chartId", this.ID + "_PieChart");
            #endregion

            var tagDef = this.BlockDef.GetValue("tagDefine");
            if (!String.IsNullOrEmpty(tagDef))
            {
                var TagList    = new List <Dictionary <string, object> >();
                var tagDefList = JsonHelper.ToList(tagDef);
                int index      = 1;
                foreach (var tagDefItem in tagDefList)
                {
                    var tagValueDef = tagDefItem.GetValue("Value").Trim();
                    var tag         = new Dictionary <string, object>();
                    tag.SetValue("TargetID", "target_" + index);
                    tag.SetValue("Title", tagDefItem.GetValue("Title").Trim());
                    tag.SetValue("Value", fo.GetDefaultValue(String.Empty, tagValueDef, this.DataSource));
                    tag.SetValue("Unit", tagDefItem.GetValue("Unit").Trim());
                    tag.SetValue("Prefix", tagDefItem.GetValue("Prefix").Trim());
                    tag.SetValue("Image", tagDefItem.GetValue("Image").Trim());
                    tag.SetValue("HasLinkUrl", "0");
                    if (!String.IsNullOrEmpty(tagDefItem.GetValue("LinkUrl").Trim()) || (IsMobile && !String.IsNullOrEmpty(tagDefItem.GetValue("MobileLinkUrl").Trim())))
                    {
                        tag.SetValue("LinkUrl", fo.GetDefaultValue(String.Empty, IsMobile ? tagDefItem.GetValue("MobileLinkUrl").Trim() : tagDefItem.GetValue("LinkUrl").Trim(), this.DataSource));
                        tag.SetValue("HasLinkUrl", "1");
                        tag.SetValue("width", tagDefItem.GetValue("width").Trim());
                        tag.SetValue("height", tagDefItem.GetValue("height").Trim());
                    }
                    TagList.Add(tag);
                    index++;
                }
                result.SetValue("Tag", TagList);
            }
            return(result);
        }
示例#5
0
        public override Dictionary <string, object> Render(string parameters = "", bool IsMobile = false)
        {
            var enumSev = Formula.FormulaHelper.GetService <IEnumService>();

            this.FillDataSource(parameters);
            var result = this.BlockDef;

            result.SetValue("ID", this.ID);
            result.SetValue("Title", this.BlockDef.GetValue("MainTitle"));
            result.SetValue("ShowTitle", this.BlockDef.GetValue("ShowTitle"));
            result.SetValue("showSummaryRow", this.BlockDef.GetValue("showSummaryRow"));
            if (String.IsNullOrEmpty(result.GetValue("chartHeight")))
            {
                result.SetValue("chartHeight", "170px");
            }
            this.setStyle(result);

            #region 设置过滤
            var filters = JsonHelper.ToList(this.BlockDef.GetValue("FilterInfo"));
            foreach (var filter in filters)
            {
                var enumData = filter.GetValue("EnumData");
                if (!String.IsNullOrEmpty(enumData))
                {
                    if (enumData.IndexOf("{") >= 0)
                    {
                        var enumList = JsonHelper.ToList(enumData);
                        foreach (var item in enumList)
                        {
                            if (String.IsNullOrEmpty(item.GetValue("IsDefault")))
                            {
                                item.SetValue("IsDefault", "false");
                            }
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                    else
                    {
                        IEnumService enumService = FormulaHelper.GetService <IEnumService>();
                        var          enumList    = JsonHelper.ToList(JsonHelper.ToJson(enumService.GetEnumDataSource(enumData.ToString())));
                        foreach (var item in enumList)
                        {
                            item.SetValue("IsDefault", "false");
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                }
            }
            result.SetValue("FilterInfo", filters);
            #endregion

            var dataSourceDefList = JsonHelper.ToList(this.BlockDef.GetValue("dataSource"));

            #region 设置图表JSON
            var chartData      = new Dictionary <string, object>();
            var dataSourceName = String.IsNullOrEmpty(HttpContext.Current.Request["DataSourceName"]) ? this.BlockDef.GetValue("DefaultDataSource") : HttpContext.Current.Request["DataSourceName"];
            if (!string.IsNullOrEmpty(parameters))
            {
                var paras = JsonHelper.ToObject <Dictionary <string, string> >(parameters);
                var ds    = paras.GetValue("DataSourceName");
                if (!string.IsNullOrEmpty(ds))
                {
                    dataSourceName = ds;
                }
            }
            DataTable dataSource = new DataTable();
            dataSource = this.DataSource.GetValue <DataTable>(dataSourceName.Trim());
            if (dataSource == null)
            {
                throw new Exception("没有找到指定的数据源");
            }
            var titleDic = new Dictionary <string, object>();
            titleDic.SetValue("Text", "");
            chartData.SetValue("title", titleDic);

            if (this.BlockDef.GetValue("AllowChangeDataSource").ToLower().Trim() == true.ToString().ToLower())
            {
                result.SetValue("defaultDataSource", dataSourceName);
                var enumData       = new List <Dictionary <string, object> >();
                var dataSourceList = JsonHelper.ToList(this.BlockDef.GetValue("dataSource"));
                foreach (var item in dataSourceList)
                {
                    var dic = new Dictionary <string, object>();
                    dic.SetValue("text", item.GetValue("Name"));
                    dic.SetValue("value", item.GetValue("Code"));
                    enumData.Add(dic);
                }
                result.SetValue("defaultDataSource", dataSourceName);
                result.SetValue("dataSourceEnum", enumData);
            }

            #region 设置X轴
            var categoryField = this.BlockDef.GetValue("categoryField");
            var xAxisList     = new List <Dictionary <string, object> >();
            var xAxis         = new Dictionary <string, object>();
            var xAxisData     = new ArrayList();
            //判定是否是固定的自定义X轴
            if (this.BlockDef.GetValue("diyCategory").Trim() == true.ToString().ToLower())
            {
                //根据枚举来固定X轴
                if (this.BlockDef.GetValue("CategorySource") == "CategoryEnum")
                {
                    var categoryEnumKey = this.BlockDef.GetValue("categoryEnumKey");
                    if (!String.IsNullOrEmpty(categoryEnumKey))
                    {
                        var enumDef = EnumBaseHelper.GetEnumDef(categoryEnumKey);
                        if (enumDef == null)
                        {
                            throw new Formula.Exceptions.BusinessValidationException("没有找到指定的枚举【" + categoryEnumKey + "】");
                        }
                        foreach (var enumItem in enumDef.EnumItem)
                        {
                            if (String.IsNullOrEmpty(enumItem.Name))
                            {
                                continue;
                            }
                            xAxisData.Add(enumItem.Name);
                        }
                    }
                }
                else if (this.BlockDef.GetValue("CategorySource") == "Categories") //根据自定义的数组绘制X轴
                {
                    var Categories = this.BlockDef.GetValue("Categories").Replace(",", ",").Split(',');
                    foreach (var category in Categories)
                    {
                        if (String.IsNullOrEmpty(category))
                        {
                            continue;
                        }
                        xAxisData.Add(category);
                    }
                }
                else
                {
                    //否则根据数据源定义的列来绘制X轴
                    var dsDefine = dataSourceDefList.FirstOrDefault(c => c.GetValue("Code") == dataSourceName);
                    if (dsDefine == null)
                    {
                        throw new Formula.Exceptions.BusinessValidationException("没有找到编号为【" + dataSourceName + "】的数据源,无法根据数据源列定义绘制列表");
                    }
                    var colDefines = JsonHelper.ToList(dsDefine.GetValue("columnDefine"));
                    foreach (var item in colDefines)
                    {
                        if (item.GetValue("ShowInX") == true.ToString().ToLower())
                        {
                            xAxisData.Add(item.GetValue("Title"));
                        }
                    }
                }
                xAxis.SetValue("categories", xAxisData);
                xAxis.SetValue("crosshair", true);
            }
            else
            {
                if (!dataSource.Columns.Contains(categoryField))
                {
                    throw new Formula.Exceptions.BusinessValidationException("没有找到categoryField 字段【" + categoryField + "】");
                }
                var categoryEnumKey = this.BlockDef.GetValue("categoryEnumKey");
                var list            = dataSource.AsEnumerable().Select(p => new { Value = p.Field <object>(categoryField) }).Distinct().ToList();
                if (!String.IsNullOrEmpty(categoryEnumKey))
                {
                    foreach (var category in list)
                    {
                        xAxisData.Add(enumSev.GetEnumText(categoryEnumKey, category.Value.ToString()));
                    }
                }
                else
                {
                    foreach (var category in list)
                    {
                        xAxisData.Add(category.Value);
                    }
                }
                xAxis.SetValue("categories", xAxisData);
                xAxis.SetValue("crosshair", true);
            }
            xAxisList.Add(xAxis);
            chartData.SetValue("xAxis", xAxisList);
            #endregion

            #region 设置Y轴
            var yAxisList   = new List <Dictionary <string, object> >();
            var yAxisDefine = JsonHelper.ToList(this.BlockDef.GetValue("yAxis"));

            var length = yAxisDefine.Count > 2 ? 2 : yAxisDefine.Count;
            for (int i = 0; i < length; i++)
            {
                var item   = yAxisDefine[i];
                var yAxis  = new Dictionary <string, object>();
                var lables = new Dictionary <string, object>();
                var style  = new Dictionary <string, object>();
                style.SetValue("color", "Highcharts.getOptions().colors[1]");
                lables.SetValue("format", "{value}" + item.GetValue("Unit"));
                lables.SetValue("style", style);

                var title = new Dictionary <string, object>();
                title.SetValue("text", item.GetValue("Title"));
                title.SetValue("style", style);
                yAxis.SetValue("labels", lables);
                yAxis.SetValue("title", title);
                if (i != 0)
                {
                    yAxis.SetValue("opposite", true);
                }
                yAxisList.Add(yAxis);
            }
            chartData.SetValue("yAxis", yAxisList);
            #endregion

            #region 图例设置
            if (this.BlockDef.GetValue("showInLegend").Trim().ToLower() == true.ToString().ToLower())
            {
                var legend = new Dictionary <string, object>();
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("layout")))
                {
                    legend.SetValue("layout", this.BlockDef.GetValue("layout"));
                }
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("align")))
                {
                    legend.SetValue("align", this.BlockDef.GetValue("align"));
                }
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("verticalAlign")))
                {
                    legend.SetValue("verticalAlign", this.BlockDef.GetValue("verticalAlign"));
                }
                chartData.SetValue("legend", legend);
            }
            else
            {
                var legend = new Dictionary <string, object>();
                legend.SetValue("enabled", false);
                chartData.SetValue("legend", legend);
            }
            #endregion

            #region 图表数据
            var seriesList    = new List <Dictionary <string, object> >();
            var graphicDefine = JsonHelper.ToList(this.BlockDef.GetValue("Series"));
            foreach (var item in graphicDefine)
            {
                if (!String.IsNullOrEmpty(item.GetValue("whereString")))
                {
                    string whereStr   = this.fo.ReplaceString(item.GetValue("whereString"));
                    var    groupField = item.GetValue("groupField");
                    var    valueField = item.GetValue("valueField");
                    if (String.IsNullOrEmpty(groupField))
                    {
                        #region
                        var series = new Dictionary <string, object>();
                        series.SetValue("name", item.GetValue("name"));
                        var data = new ArrayList();
                        foreach (var xAxisValue in xAxisData)
                        {
                            var dataRows = dataSource.Select(categoryField + "='" + xAxisValue + "'" + " and " + whereStr);
                            if (dataRows.Length == 0)
                            {
                                data.Add(0);
                            }
                            else
                            {
                                var value = dataRows.FirstOrDefault()[valueField] == null || dataRows.FirstOrDefault()[valueField] == DBNull.Value ? 0m : Convert.ToDecimal(dataRows.FirstOrDefault()[valueField]);
                                data.Add(value);
                            }
                        }
                        series.SetValue("data", data);
                        series.SetValue("type", item.GetValue("type"));
                        if (!String.IsNullOrEmpty(item.GetValue("yAxis")))
                        {
                            series.SetValue("yAxis", Convert.ToInt32(item.GetValue("yAxis")));
                        }
                        if (!String.IsNullOrEmpty(item.GetValue("Unit")))
                        {
                            var tooltip = new Dictionary <string, object>();
                            tooltip.SetValue("valueSuffix", item.GetValue("valueSuffix"));
                            series.SetValue("tooltip", tooltip);
                        }
                        seriesList.Add(series);
                        #endregion
                    }
                    else
                    {
                        #region
                        var groupData = dataSource.Select(whereStr).AsEnumerable().Select(p => new { Value = p.Field <object>(groupField) }).Distinct().ToList();
                        foreach (var groupItem in groupData)
                        {
                            var series = new Dictionary <string, object>();
                            series.SetValue("name", groupItem.Value);
                            var data = new ArrayList();
                            var rows = dataSource.Select(groupField + "='" + groupItem.Value + "'");
                            if (rows.Length == 0)
                            {
                                foreach (var xAxisValue in xAxisData)
                                {
                                    data.Add(0);
                                }
                            }
                            else
                            {
                                foreach (var xAxisValue in xAxisData)
                                {
                                    var dataRows = dataSource.Select(categoryField + "='" + xAxisValue + "' and " + groupField + "='" + groupItem.Value + "'" + " and " + whereStr);
                                    if (dataRows.Length == 0)
                                    {
                                        data.Add(0);
                                    }
                                    else
                                    {
                                        var value = dataRows[0][valueField] == null || dataRows[0][valueField] == DBNull.Value ? 0m : Convert.ToDecimal(dataRows[0][valueField]);
                                        data.Add(value);
                                    }
                                }
                            }
                            series.SetValue("data", data);
                            series.SetValue("type", item.GetValue("type"));
                            if (!String.IsNullOrEmpty(item.GetValue("yAxis")))
                            {
                                series.SetValue("yAxis", Convert.ToInt32(item.GetValue("yAxis")));
                            }
                            if (!String.IsNullOrEmpty(item.GetValue("Unit")))
                            {
                                var tooltip = new Dictionary <string, object>();
                                tooltip.SetValue("valueSuffix", item.GetValue("valueSuffix"));
                                series.SetValue("tooltip", tooltip);
                            }
                            seriesList.Add(series);
                        }
                        #endregion
                    }
                }
                else
                {
                    var groupField = item.GetValue("groupField");
                    var valueField = item.GetValue("valueField");
                    if (String.IsNullOrEmpty(groupField))
                    {
                        #region
                        var series = new Dictionary <string, object>();
                        series.SetValue("name", item.GetValue("name"));
                        var data = new ArrayList();
                        foreach (var xAxisValue in xAxisData)
                        {
                            var dataRows = dataSource.Select(categoryField + "='" + xAxisValue + "'");
                            if (dataRows.Length == 0)
                            {
                                data.Add(0);
                            }
                            else
                            {
                                var value = dataRows.FirstOrDefault()[valueField] == null || dataRows.FirstOrDefault()[valueField] == DBNull.Value ? 0m : Convert.ToDecimal(dataRows.FirstOrDefault()[valueField]);
                                data.Add(value);
                            }
                        }
                        series.SetValue("data", data);
                        series.SetValue("type", item.GetValue("type"));
                        if (!String.IsNullOrEmpty(item.GetValue("yAxis")))
                        {
                            series.SetValue("yAxis", Convert.ToInt32(item.GetValue("yAxis")));
                        }
                        if (!String.IsNullOrEmpty(item.GetValue("Unit")))
                        {
                            var tooltip = new Dictionary <string, object>();
                            tooltip.SetValue("valueSuffix", item.GetValue("valueSuffix"));
                            series.SetValue("tooltip", tooltip);
                        }
                        seriesList.Add(series);
                        #endregion
                    }
                    else
                    {
                        #region
                        var groupData = dataSource.AsEnumerable().Select(p => new { Value = p.Field <object>(groupField) }).Distinct().ToList();
                        foreach (var groupItem in groupData)
                        {
                            var series = new Dictionary <string, object>();
                            series.SetValue("name", groupItem.Value);
                            var data = new ArrayList();
                            var rows = dataSource.Select(groupField + "='" + groupItem.Value + "'");
                            if (rows.Length == 0)
                            {
                                foreach (var xAxisValue in xAxisData)
                                {
                                    data.Add(0);
                                }
                            }
                            else
                            {
                                foreach (var xAxisValue in xAxisData)
                                {
                                    var dataRows = dataSource.Select(categoryField + "='" + xAxisValue + "' and " + groupField + "='" + groupItem.Value + "'");
                                    if (dataRows.Length == 0)
                                    {
                                        data.Add(0);
                                    }
                                    else
                                    {
                                        var value = dataRows[0][valueField] == null || dataRows[0][valueField] == DBNull.Value ? 0m : Convert.ToDecimal(dataRows[0][valueField]);
                                        data.Add(value);
                                    }
                                }
                            }
                            series.SetValue("data", data);
                            series.SetValue("type", item.GetValue("type"));
                            if (!String.IsNullOrEmpty(item.GetValue("yAxis")))
                            {
                                series.SetValue("yAxis", Convert.ToInt32(item.GetValue("yAxis")));
                            }
                            if (!String.IsNullOrEmpty(item.GetValue("Unit")))
                            {
                                var tooltip = new Dictionary <string, object>();
                                tooltip.SetValue("valueSuffix", item.GetValue("valueSuffix"));
                                series.SetValue("tooltip", tooltip);
                            }
                            seriesList.Add(series);
                        }
                        #endregion
                    }
                }
            }
            #endregion
            chartData.SetValue("series", seriesList);
            result.SetValue("chartData", chartData);
            result.SetValue("chartId", this.ID + "_ColumnChart");
            #endregion

            #region 设置列表JSON
            var resultDt = new DataTable();

            var columnDefines = JsonHelper.ToList(this.BlockDef.GetValue("ColumnDefine"));
            #region 列定义
            if (this.BlockDef.GetValue("gridDynColumn") == true.ToString().ToLower())
            {
                if (this.BlockDef.GetValue("independentDataSource").ToLower() == true.ToString().ToLower())
                {
                    dataSourceName = this.BlockDef.GetValue("independentDataSourceName");
                }
                var dsDefine = dataSourceDefList.FirstOrDefault(c => c.GetValue("Code") == dataSourceName);
                if (dsDefine == null)
                {
                    throw new Formula.Exceptions.BusinessValidationException("没有找到编号为【" + dataSourceName + "】的数据源,无法根据数据源列定义绘制列表");
                }
                columnDefines = JsonHelper.ToList(dsDefine.GetValue("columnDefine"));
            }
            foreach (var item in columnDefines)
            {
                if (String.IsNullOrEmpty(item.GetValue("Width")))
                {
                    item.SetValue("Width", Math.Round(100m / columnDefines.Count, 2) + "%");
                }
                else if (item.GetValue("Width").ToLower().Trim() == "auto" || item.GetValue("Width").ToLower().Trim() == "*")
                {
                }
                else if (!item.GetValue("Width").ToLower().Trim().EndsWith("px") && !item.GetValue("Width").ToLower().Trim().EndsWith("%"))
                {
                    item.SetValue("Width", item.GetValue("Width") + "px");
                }
                if (String.IsNullOrEmpty(item.GetValue("Align")))
                {
                    item.SetValue("Align", "center");
                }
                resultDt.Columns.Add(item.GetValue("Field"));
            }
            this.BlockDef.SetValue("ColumnDefine", columnDefines);
            #endregion

            var autoSumRow = false; var sumType = "Sum";

            //判定是否是独立数据源,如果是独立数据源,则不能与图表同步进行数据源切换
            if (this.BlockDef.GetValue("independentDataSource").ToLower() == true.ToString().ToLower())
            {
                //独立数据源,必须指定数据源名称
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("independentDataSourceName")))
                {
                    dataSourceName = this.BlockDef.GetValue("independentDataSourceName");
                    var dsDefine       = dataSourceDefList.FirstOrDefault(c => c.GetValue("Code") == dataSourceName);
                    var gridDataSource = this.DataSource.GetValue <DataTable>(dataSourceName);
                    if (gridDataSource != null && dsDefine != null)
                    {
                        if (dsDefine.GetValue("AutoSumRow").Trim().ToLower() == true.ToString().ToLower() &&
                            !String.IsNullOrEmpty(dsDefine.GetValue("SumType").Trim()))
                        {
                            autoSumRow = true;
                            sumType    = dsDefine.GetValue("SumType").Trim();
                        }
                        resultDt = getGridTable(gridDataSource, autoSumRow, sumType);
                    }
                }
            }
            else
            {
                var dsDefine = dataSourceDefList.FirstOrDefault(c => c.GetValue("Code").Trim() == dataSourceName.Trim());
                if (dsDefine != null && dsDefine.GetValue("AutoSumRow").Trim().ToLower() == true.ToString().ToLower() &&
                    !String.IsNullOrEmpty(dsDefine.GetValue("SumType").Trim()))
                {
                    autoSumRow = true;
                    sumType    = dsDefine.GetValue("SumType").Trim();
                }
                resultDt = getGridTable(dataSource, autoSumRow, sumType);
            }
            result.SetValue("gridHeight", String.IsNullOrEmpty(this.BlockDef.GetValue("gridHeight")) ? "100%" : this.BlockDef.GetValue("gridHeight"));
            var columnSumData = getColumnSumTable(resultDt, columnDefines);
            var gridData      = new DataTable();
            result.SetValue("GridData", gridData);
            result.SetValue("ColumnSumData", columnSumData);
            result.SetValue("GridID", this.ID + "_DataGrid");
            #endregion

            return(result);
        }
示例#6
0
        public override Dictionary <string, object> Render(string parameters = "", bool IsMobile = false)
        {
            var enumSev = Formula.FormulaHelper.GetService <IEnumService>();

            this.FillDataSource(parameters);
            var result = this.BlockDef;

            result.SetValue("ID", this.ID);
            result.SetValue("Title", this.BlockDef.GetValue("MainTitle"));
            result.SetValue("ShowTitle", this.BlockDef.GetValue("ShowTitle"));
            result.SetValue("showSummaryRow", this.BlockDef.GetValue("showSummaryRow"));
            if (String.IsNullOrEmpty(result.GetValue("chartHeight")))
            {
                result.SetValue("chartHeight", "170px");
            }
            this.setStyle(result);

            #region 设置过滤
            var filters = JsonHelper.ToList(this.BlockDef.GetValue("FilterInfo"));
            foreach (var filter in filters)
            {
                var enumData = filter.GetValue("EnumData");

                if (!String.IsNullOrEmpty(enumData))
                {
                    if (enumData.IndexOf("{") >= 0)
                    {
                        var enumList = JsonHelper.ToList(enumData);
                        foreach (var item in enumList)
                        {
                            if (String.IsNullOrEmpty(item.GetValue("IsDefault")))
                            {
                                item.SetValue("IsDefault", "false");
                            }
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                    else
                    {
                        IEnumService enumService = FormulaHelper.GetService <IEnumService>();
                        var          enumList    = JsonHelper.ToList(JsonHelper.ToJson(enumService.GetEnumDataSource(enumData.ToString())));
                        foreach (var item in enumList)
                        {
                            item.SetValue("IsDefault", "false");
                        }
                        filter.SetValue("EnumData", enumList);
                    }
                }
            }
            result.SetValue("FilterInfo", filters);
            #endregion

            var dataSourceName = String.IsNullOrEmpty(HttpContext.Current.Request["DataSourceName"]) ? this.BlockDef.GetValue("DefaultDataSource") : HttpContext.Current.Request["DataSourceName"];
            if (!string.IsNullOrEmpty(parameters))
            {
                var paras = JsonHelper.ToObject <Dictionary <string, string> >(parameters);
                var ds    = paras.GetValue("DataSourceName");
                if (!string.IsNullOrEmpty(ds))
                {
                    dataSourceName = ds;
                }
            }
            DataTable dataSource = new DataTable();
            dataSource = this.DataSource.GetValue <DataTable>(dataSourceName.Trim());
            if (dataSource == null)
            {
                throw new Exception("没有找到指定的数据源");
            }

            #region 设置列表JSON
            var resultDt = new DataTable();
            #region 列定义
            var columnDefines = JsonHelper.ToList(this.BlockDef.GetValue("ColumnDefine"));
            foreach (var item in columnDefines)
            {
                if (String.IsNullOrEmpty(item.GetValue("Width")))
                {
                    item.SetValue("Width", Math.Round(100m / columnDefines.Count, 2) + "%");
                }
                else if (item.GetValue("Width").ToLower().Trim() == "auto" || item.GetValue("Width").ToLower().Trim() == "*")
                {
                }
                else if (!item.GetValue("Width").ToLower().Trim().EndsWith("px") && !item.GetValue("Width").ToLower().Trim().EndsWith("%"))
                {
                    item.SetValue("Width", item.GetValue("Width") + "px");
                }
                if (String.IsNullOrEmpty(item.GetValue("Align")))
                {
                    item.SetValue("Align", "center");
                }
            }
            this.BlockDef.SetValue("ColumnDefine", columnDefines);
            #endregion

            var autoSumRow = false; var sumType = "Sum";
            var dataSourceDefList = JsonHelper.ToList(this.BlockDef.GetValue("dataSource"));
            //判定是否是独立数据源,如果是独立数据源,则不能与图表同步进行数据源切换
            if (this.BlockDef.GetValue("independentDataSource").ToLower() == true.ToString().ToLower())
            {
                //独立数据源,必须指定数据源名称
                if (!String.IsNullOrEmpty(this.BlockDef.GetValue("independentDataSourceName")))
                {
                    dataSourceName = this.BlockDef.GetValue("independentDataSourceName");
                    var dsDefine       = dataSourceDefList.FirstOrDefault(c => c.GetValue("Code") == dataSourceName);
                    var gridDataSource = this.DataSource.GetValue <DataTable>(dataSourceName);
                    if (gridDataSource != null && dsDefine != null)
                    {
                        if (dsDefine.GetValue("AutoSumRow").Trim().ToLower() == true.ToString().ToLower() &&
                            !String.IsNullOrEmpty(dsDefine.GetValue("SumType").Trim()))
                        {
                            autoSumRow = true;
                            sumType    = dsDefine.GetValue("SumType").Trim();
                        }
                        resultDt = getGridTable(gridDataSource, autoSumRow, sumType);
                    }
                }
            }
            else
            {
                var dsDefine = dataSourceDefList.FirstOrDefault(c => c.GetValue("Code").Trim() == dataSourceName.Trim());
                if (dsDefine != null && dsDefine.GetValue("AutoSumRow").Trim().ToLower() == true.ToString().ToLower() &&
                    !String.IsNullOrEmpty(dsDefine.GetValue("SumType").Trim()))
                {
                    autoSumRow = true;
                    sumType    = dsDefine.GetValue("SumType").Trim();
                }
                resultDt = getGridTable(dataSource, autoSumRow, sumType);
            }
            result.SetValue("gridHeight", String.IsNullOrEmpty(this.BlockDef.GetValue("gridHeight")) ? "100%" : this.BlockDef.GetValue("gridHeight"));
            result.SetValue("GridData", new DataTable());
            result.SetValue("ColumnSumData", getColumnSumTable(resultDt, columnDefines));
            result.SetValue("GridID", this.ID + "_DataGrid");
            #endregion

            result.SetValue("chartData", resultDt);

            return(result);
        }