/// <summary> /// CReate the colour values for column data element. /// </summary> /// <param name="queryColumnId">The query column unique identifier.</param> /// <param name="data">The data.</param> /// <returns>ColorRule.</returns> public ColorRule ColourForColumnData(string queryColumnId, object data) { if (!_formatRules.ContainsKey(queryColumnId) || data == null) { return(null); } // Run the list of column formatting rules using this value ConditionInfo rule = SelectMatchingRule(_formatRules[queryColumnId].ToArray(), data); return(rule != null ? rule.ColorRule : null); }
/// <summary> /// Tries to get the rule based on the available rules for the column (if any). /// </summary> /// <param name="columnId">The column unique identifier.</param> /// <param name="data">The data.</param> /// <param name="ruleIndex">Index of the rule.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> /// <exception cref="System.InvalidOperationException">Predicate has not been set of condition.</exception> public bool TryGetRule(string columnId, object data, out long ruleIndex) { if (data == null || !_formatRules.ContainsKey(columnId)) { ruleIndex = -1; return(false); } List <ConditionInfo> rules = _formatRules [columnId]; int count = rules.Count; int last = count - 1; // Check each rule for (int i = 0; i < count; i++) { ConditionInfo rule = rules[i]; // Get the condition for this rule if (rule.Predicate == null) { continue; } if (rule.ColorRule != null) { // Accept the last rule regardless if (i == last && rule.ColorRule.Condition.Operator == ConditionType.Unspecified) { ruleIndex = i; return(true); } } else if (rule.IconRule != null) { // Accept the last rule regardless if (i == last && rule.IconRule.Condition.Operator == ConditionType.Unspecified) { ruleIndex = i; return(true); } } // Test the value against the rule if (rule.Predicate(data)) { ruleIndex = i; return(true); } } ruleIndex = -1; return(false); }
/// <summary> /// Initializes a new instance of the <see cref="ConditionalFormatter" /> class. /// </summary> /// <param name="columnFormats">The column formats.</param> /// <param name="additionalColumnInfo">The additional column info.</param> /// <param name="reportResult">The report result.</param> public ConditionalFormatter(IList <ColumnFormatting> columnFormats, IEnumerable <ResultColumn> additionalColumnInfo, ReportResult reportResult) { _reportResult = reportResult; if (columnFormats != null) { _formatRules = new Dictionary <string, List <ConditionInfo> >(columnFormats.Count); // Cache the formatting rules into a dictionary for fast lookup. Dictionary <string, ColumnFormatting> formatters = columnFormats.ToDictionary(cf => cf.EntityId.ToString(CultureInfo.InvariantCulture)); foreach (KeyValuePair <string, ColumnFormatting> keyValuePair in formatters.Where(f => f.Value != null)) { BarFormattingRule barFormattingRule = keyValuePair.Value.FormattingRule as BarFormattingRule; if (barFormattingRule != null) { ConditionInfo condition = new ConditionInfo { ColorRule = new ColorRule { BackgroundColor = barFormattingRule.Color }, Style = ConditionalFormatStyleEnum.ProgressBar, ShowText = keyValuePair.Value.ShowText }; DatabaseType columnForFormatType = additionalColumnInfo.FirstOrDefault(ac => ac.RequestColumn.EntityId.ToString(CultureInfo.InvariantCulture) == keyValuePair.Key).ColumnType; if (columnForFormatType != null) { barFormattingRule.Minimum.Type = columnForFormatType; barFormattingRule.Maximum.Type = columnForFormatType; } DateTime minumumDateTime; if (barFormattingRule.Minimum.Value != null && (columnForFormatType is DateType || columnForFormatType is TimeType || columnForFormatType is DateTimeType) && DateTime.TryParse(barFormattingRule.Minimum.Value.ToString(), out minumumDateTime)) { condition.LowerBounds = minumumDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ"); } else { float minimum; condition.LowerBounds = float.TryParse(barFormattingRule.Minimum.ValueString, out minimum) ? minimum : 0.0f; } DateTime maximumDateTime; if (barFormattingRule.Maximum.Value != null && (columnForFormatType is DateType || columnForFormatType is TimeType || columnForFormatType is DateTimeType) && DateTime.TryParse(barFormattingRule.Maximum.Value.ToString(), out maximumDateTime)) { condition.Upperbounds = maximumDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ"); } else { float maximum; if (float.TryParse(barFormattingRule.Maximum.ValueString, out maximum)) { condition.Upperbounds = maximum; } } _formatRules.Add(keyValuePair.Key, new List <ConditionInfo>(new[] { condition })); } ColorFormattingRule colorFormattingRule = keyValuePair.Value.FormattingRule as ColorFormattingRule; if (colorFormattingRule != null) { IList <ColorRule> colorRules = colorFormattingRule.Rules; List <ConditionInfo> predicates = new List <ConditionInfo>(colorRules.Count); foreach (ColorRule colorRule in colorRules) { ResultColumn gridResultColumn = additionalColumnInfo.FirstOrDefault(gu => gu.RequestColumn.EntityId.ToString(CultureInfo.InvariantCulture) == keyValuePair.Key); if (gridResultColumn == null) { continue; } colorRule.Condition.Arguments = TypedValuesForOperatorColumnType(colorRule.Condition.Arguments, colorRule.Condition.Operator, gridResultColumn.ColumnType); } predicates.AddRange(from colorRule in colorRules let gridResultColumn = additionalColumnInfo.FirstOrDefault(gu => gu.RequestColumn.EntityId.ToString(CultureInfo.InvariantCulture) == keyValuePair.Key) let predicate = PredicateForRule(colorRule.Condition, IsResourceCondition(gridResultColumn), gridResultColumn, this) where (predicate != null) select new ConditionInfo { Predicate = predicate, ColorRule = colorRule, Operator = colorRule.Condition.Operator, Values = colorRule.Condition.Arguments, Style = ConditionalFormatStyleEnum.Highlight, ShowText = keyValuePair.Value.ShowText }); _formatRules.Add(keyValuePair.Key, predicates); } IconFormattingRule iconFormattingRule = keyValuePair.Value.FormattingRule as IconFormattingRule; if (iconFormattingRule != null) { IList <IconRule> iconRulesRules = iconFormattingRule.Rules; foreach (IconRule iconRule in iconRulesRules) { ResultColumn gridResultColumn = additionalColumnInfo.FirstOrDefault(gu => gu.RequestColumn.EntityId.ToString(CultureInfo.InvariantCulture) == keyValuePair.Key); if (gridResultColumn == null) { continue; } iconRule.Condition.Arguments = TypedValuesForOperatorColumnType(iconRule.Condition.Arguments, iconRule.Condition.Operator, gridResultColumn.ColumnType); } List <ConditionInfo> predicates = new List <ConditionInfo>(iconRulesRules.Count); predicates.AddRange(from iconRule in iconRulesRules let gridResultColumn = additionalColumnInfo.FirstOrDefault(gu => gu.RequestColumn.EntityId.ToString(CultureInfo.InvariantCulture) == keyValuePair.Key) let predicate = PredicateForRule(iconRule.Condition, IsResourceCondition(gridResultColumn), gridResultColumn, this) where (predicate != null) select new ConditionInfo { Predicate = predicate, IconRule = iconRule, Operator = iconRule.Condition.Operator, Values = iconRule.Condition.Arguments, Style = ConditionalFormatStyleEnum.Icon, ShowText = keyValuePair.Value.ShowText }); _formatRules.Add(keyValuePair.Key, predicates); } } } }