/// <summary> /// Initializes a new instance of the <see cref="TableColumn"/> class. /// </summary> /// <param name="columnData">The column data.</param> /// <param name="columnName">Name of the column.</param> /// <param name="columnWidth">Width of the column.</param> /// <param name="dataType">Type of the data.</param> public TableColumn(string columnData, string columnName, Unit columnWidth, Type dataType) : this(columnData, columnName, columnWidth) { DataType = dataType; }
/// <summary> /// Creates the aggregate items. /// </summary> /// <param name="currentReport">The current report.</param> /// <param name="forGroups">if set to <c>true</c> [for groups].</param> /// <returns>List{TextBox}.</returns> private List<TextBox> CreateAggregateItems(Report currentReport, bool forGroups) { var height = new Unit(16); var aggregateItems = new Dictionary<string, List<TextBox>>(); var additionalInfoForLastAggregateItem = new Dictionary<Unit, float>(); //calculate server-side aggregates var aggregateDefinitions = _aggregateDict.ToList() .SelectMany(i => i.Value) .Where(d => !AggregateHelper.IsPageAggregateType(d.SummaryType)) .ToList(); var queryCriteria = new AggregateDataSourceQueryCriteria { ProcessName = _processName, FilterDefinition = GetFilterDefinition(currentReport) }; var serverAggregateResults = AggregateProcessor.CalculateAggregates(aggregateDefinitions, queryCriteria); foreach (var aggregateInfo in _aggregateDict) { var col = _colDict.FirstOrDefault(x => x.Value[0] == aggregateInfo.Key); if (!col.Equals(default(KeyValuePair<int, string[]>))) { var fieldValueMap = ((TextBox)currentReport.Items["detail"].Items["rowPanel"].Items[col.Key - 1]).Value.TrimStart('='); var locationX = ((TextBox)currentReport.Items["detail"].Items["rowPanel"].Items[col.Key - 1]).Location.X; var width = GetWidth(currentReport, aggregateInfo); var locationY = new Unit(0, locationX.Type); var list = new List<TextBox>(); foreach (var criteria in aggregateInfo.Value) { var textbox = new TextBox(); textbox.Style.Padding.Left = new Unit(1); textbox.Style.Padding.Right = new Unit(1); textbox.Style.Font.Size = new Unit(9); textbox.Style.Font.Name = "Arial Unicode MS"; textbox.Style.VerticalAlign = VerticalAlign.Middle; textbox.Size = new SizeU(Unit.Inch(width), height); textbox.CanGrow = true; textbox.TextWrap = true; textbox.Location = new PointU(locationX, locationY); if (_aggregateDict.Last().Equals(aggregateInfo)) { EventHandler textboxItemDataBoundHandler = (s, e) => { var ft = new FormattedText((string)((Telerik.Reporting.Processing.TextBox)s).Value, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(new System.Windows.Media.FontFamily(((Telerik.Reporting.Processing.VisualElement)s).Style.Font.Name), new System.Windows.FontStyle(), new FontWeight(), new FontStretch()), ((Telerik.Reporting.Processing.VisualElement)s).Style.Font.Size.Value, System.Windows.Media.Brushes.Black); if (ft.Width > ((Telerik.Reporting.Processing.TextBox)s).Size.Width.Value) { foreach (var f in additionalInfoForLastAggregateItem) { ((Telerik.Reporting.Processing.ReportItem)s).Location = new PointU(f.Key, ((Telerik.Reporting.Processing.ReportItem)s).Location.Y); ((Telerik.Reporting.Processing.ReportItem)s).Size = new SizeU(Unit.Inch(f.Value + ((Telerik.Reporting.Processing.ReportItem)s).Size.Width.Value), height); if (((Telerik.Reporting.Processing.ReportItem)s).Size.Width.Value >= ft.Width) { break; } } } }; textbox.ItemDataBound += textboxItemDataBoundHandler; EventHandler textboxDisposedHandler = null; textboxDisposedHandler = (sender, args) => { textbox.ItemDataBound -= textboxItemDataBoundHandler; textbox.Disposed -= textboxDisposedHandler; }; textbox.Disposed += textboxDisposedHandler; } if (AggregateHelper.IsPageAggregateType(criteria.SummaryType)) { var aggregateFunction = forGroups ? "Exec" : "PageExec"; var aggregateFunctionParameter = forGroups ? ":scope:" : aggregateInfo.Key + "TextBox"; textbox.Value = string.Format( @"='{0}: ' + AfterCalculateAggregates({7}('{8}', {2}(BeforeCalculateAggregates(Fields.[{1}], ""{2}"", ""{3}"", ""{4}"", ""{5}"", ""{6}""))), ""{2}"", ""{3}"", ""{4}"", ""{5}"", ""{6}"")", AggregateHelper.GetAttribute(criteria.SummaryType).ShortName, aggregateInfo.Key, criteria.SummaryType, criteria.CustomConverter, criteria.TargetType, criteria.TypeName, criteria.ConverterParameter, aggregateFunction, aggregateFunctionParameter); } else { var result = serverAggregateResults.FirstOrDefault(r => r.ColumnName == criteria.ColumnName && r.SummaryType == criteria.SummaryType); textbox.Value = string.Format("{0}: {1}", AggregateHelper.GetAttribute(criteria.SummaryType).ShortName, result); } list.Add(textbox); locationY += height; } aggregateItems.Add(aggregateInfo.Key, list); } } if (aggregateItems.Count > 1) { var lastItem = aggregateItems.Last(); aggregateItems.Remove(lastItem.Key); var penultimateItem = aggregateItems.Last(); var indexOfPenultimateItem = _colDict.First(x => x.Value[0] == penultimateItem.Key).Key; var indexOfLastItem = _colDict.First(x => x.Value[0] == lastItem.Key).Key; if (indexOfLastItem - indexOfPenultimateItem > 1) // if exists at least one column between last and penultimate aggregate columns { // fill additional info for last aggrigate item var i = Convert.ToInt32((indexOfLastItem - indexOfPenultimateItem - 1) / 2); foreach (var column in _colDict) { if (column.Key >= i + indexOfPenultimateItem + 1 && column.Value[0] != lastItem.Key) { var item = ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items.FirstOrDefault(x => x.Name == string.Format("{0}TextBox", column.Value[0]))); if (item != null) { additionalInfoForLastAggregateItem.Add(item.Location.X, item.Size.Width.Value); } } } var descending = additionalInfoForLastAggregateItem.Reverse(); additionalInfoForLastAggregateItem = descending.ToDictionary(x => x.Key, x => x.Value); // new size of penultimate item foreach (var textbox in penultimateItem.Value) { textbox.Size = new SizeU(Unit.Inch(textbox.Size.Width.Value / 2), height); } } aggregateItems.Add(lastItem.Key, lastItem.Value); } var returnList = new List<TextBox>(); foreach (var aggregateItem in aggregateItems) { returnList.AddRange(aggregateItem.Value); } return returnList; }
/// <summary> /// Initializes a new instance of the <see cref="TableColumn"/> class. /// </summary> /// <param name="columnData">The column data.</param> /// <param name="columnName">Name of the column.</param> /// <param name="columnWidth">Width of the column.</param> public TableColumn(string columnData, string columnName, Unit columnWidth) { Name = columnName; Width = columnWidth; Data = columnData; }
/// <summary> /// This method automatically evaluates the optimal width of each column based on its contents /// </summary> /// <param name="currentReport">Current Telerik.Reporting.Report object</param> /// <param name="itemList">List of *Info objects belonging to the process which is related to this report</param> /// <param name="graphics">System.Drawing.Graphics object is needed to retrieve text's width in pixels</param> //TODO: Method is too complex. Refactor! private void RearrangeColumns(Report currentReport, IEnumerable<IInfoClass> itemList, Graphics graphics) { //Initializing variables var colNum = 0; //counter for columns double maxRows = 0; //max number of rows from all headers double extraSpace = 0; //space left after the first rearrangement var headerWidthDict = new Dictionary<int, double>(); //key - column index, value - width of column's header in pixels var columnToExpand = new Dictionary<int, double>(); //columns which were limited in there desired width var unitType = currentReport.Width.Type; //may be inches or centimetres or smth else double headerHeight = 0; //the height of one row of header's text in pixels var emptyColumns = new List<int>(); var columnInitialSizeDict = new Dictionary<int, double>(); var widthOfAllPreviousColumns = (100 / Unit.FromPixels(100, unitType).Value) * _indentOffset; //all the space occupied to the left of current processing column _dataHeights = new Dictionary<string, List<int>>(); foreach (var col in _colDict) //first rearrangement { colNum++; var propName = col.Value[0]; //field's system name var propDisplayName = col.Value[1]; //field's display name //full width of column's header in pixels double headerLength = 0; var pageHeaderItem = currentReport.Items["pageHeader"].Items.ElementAt(col.Key) as TextBox; if (pageHeaderItem != null) { var headerSize = MeasureDisplayStringSize(propDisplayName, ToSystemFont(pageHeaderItem.Style.Font), graphics); headerLength = headerSize.Width; headerHeight = headerSize.Height; headerWidthDict.Add(col.Key, headerLength); } //is based on the ItemList and stores the contents of the specified column var contentList = new List<ReportData>(); if (itemList != null && itemList.Any()) { var prop = itemList.First().GetPropertyByFullName(propName); if (prop != null) { contentList.AddRange(itemList.Select(x => { var ancestor = x.GetAncestor(prop); if (ancestor == null) return ReportData.GetDefault(prop.PropertyType); var fieldProperty = _fieldAttrDict.Keys.FirstOrDefault(p => p.Name == propName); return new ReportData(prop, ancestor, fieldProperty == null ? null : _fieldAttrDict[fieldProperty], currentReport); })); } } if (contentList.Count > 0) { _dataHeights.Add(propName, contentList.Select(x => x.Height).ToList()); } double lessThanColName = 0; //strings from the contents of the column that contain less symbols than the column's header double moreThanColName = 0; //strings from the contents of the column that contain more symbols than the column's header double contentMaxLength = 0; //the longest string from the contents of the column if (contentList.Count > 0) { lessThanColName = contentList.Count(x => x.Width < propDisplayName.Length); moreThanColName = contentList.Count(x => x.Width >= propDisplayName.Length); var contentMax = contentList.OrderByDescending(x => x.Width).First(); if (contentMax.Width > 0) { contentMaxLength = contentMax.GetPixels(currentReport, graphics); } else contentMaxLength = 0; } double contentResultLength = 0; //the desired width of the column in pixels double headerPartMaxLength = 0; //width in pixels of the biggest part of column's name if it contains ' ' (spaces) or '_' (underscores) if (contentMaxLength < headerLength && Regex.IsMatch(propDisplayName, @"[\s_]")) { string[] parts; if (propDisplayName.Contains(" ")) { parts = propDisplayName.Split(' '); headerPartMaxLength = parts.Max( x => MeasureDisplayStringSize(x, ToSystemFont(currentReport.Items["pageHeader"].Style.Font), graphics).Width); } else { parts = propDisplayName.Split('_'); headerPartMaxLength = parts.Max( x => MeasureDisplayStringSize(x, ToSystemFont(currentReport.Items["pageHeader"].Style.Font), graphics).Width); } //if column's header contains spaces or underscores and the longest string from the column's contents //is shorter than the full-length header name then the optimal column width can be found as maximum //between thw longest string from the contents and the longest part of the column name contentResultLength = Math.Max(contentMaxLength, headerPartMaxLength); } else if (contentMaxLength < headerLength) { //if the header cannot be logically splitted into parts and the longest string from the column's contents //is shorter than column name then the header's width is the optimal contentResultLength = headerLength; } else { //if the longest string from the column's contents is longer than header then the desired width may be represented by: // 1. the longest string from the column's contents in case when the portion of strings which contain more symbols than the column's name is bigger than 20% of all strings // 2. the header in other case contentResultLength = (moreThanColName / lessThanColName) > 0.25 ? contentMaxLength : headerLength; } //how many units is the average width of the column (all the space is divided by the number of columns) //and then how many pixels is this average width, calculated in units var defaultLenght = (100 / Unit.FromPixels(100, unitType).Value) * ((currentReport.Width.Value - Unit.FromPixels(widthOfAllPreviousColumns, unitType).Value) / (_colDict.Count - (colNum - 1))); var newWidth = Math.Min(contentResultLength + 3, defaultLenght); //the final width of column in pixels var resultWidth = widthOfAllPreviousColumns + newWidth; //offset for next columns in pixels widthOfAllPreviousColumns = resultWidth; columnInitialSizeDict.Add(col.Key, newWidth); if (colNum < _colDict.Count) //if this is not the last column { ((TextBox)currentReport.Items["pageHeader"].Items[col.Key + 1]).Location = new PointU(Unit.FromPixels(resultWidth, unitType), new Unit(0.4, unitType)); ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum]).Location = new PointU(Unit.FromPixels(resultWidth, unitType), new Unit(0, unitType)); } ((TextBox)currentReport.Items["pageHeader"].Items[col.Key]).Size = new SizeU(Unit.FromPixels(newWidth, unitType), new Unit(0.2, unitType)); ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum - 1]).Size = new SizeU(Unit.FromPixels(newWidth, unitType), new Unit(0.2, unitType)); if (colNum == _colDict.Count) //if this is the last column the free space to the right could remain extraSpace = (currentReport.Width.Value) - Unit.FromPixels(widthOfAllPreviousColumns, unitType).Value; if (headerLength > 0) maxRows = Math.Max(maxRows, (headerLength / (newWidth - (headerLength / propDisplayName.Length)))); if ((contentResultLength + 3) > defaultLenght && contentMaxLength > 0) columnToExpand.Add(col.Key, Unit.FromPixels(contentResultLength - defaultLenght, unitType).Value); if (contentMaxLength == 0) emptyColumns.Add(col.Key); } if (extraSpace > 0) //if there is free space to the right of the last column after first rearrangement { if (columnToExpand.Count == 0) //if there are no columns which were cut to the average column width { var sum = columnInitialSizeDict.Where(x => !emptyColumns.Contains(x.Key)).Sum(x => x.Value); //var spacePerColumn = extraSpace * ((_colDict.Count - emptyColumns.Count) / sum); //additional space (in units) to add to non-empty columns double addedSpace = 0; maxRows = 0; colNum = 0; SizeU oldCellSize; //size in units which the column has after first rearrangement PointU oldCellLocation; //location point of the column after first rearrangement foreach (var col in _colDict) //second rearrangement (simple: all extra space is divided equally between the columns ) { var auxSpace = emptyColumns.Contains(col.Key) ? 0 : extraSpace * (columnInitialSizeDict[col.Key] / sum); colNum++; if (colNum < _colDict.Count) //relocation of columns next to the current taking added space into account { oldCellLocation = ((TextBox)currentReport.Items["pageHeader"].Items[col.Key + 1]).Location; ((TextBox)currentReport.Items["pageHeader"].Items[col.Key + 1]).Location = new PointU(new Unit(oldCellLocation.X.Value + auxSpace + addedSpace, unitType), new Unit(0.4, unitType)); oldCellLocation = ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum]).Location; ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum]).Location = new PointU(new Unit(oldCellLocation.X.Value + auxSpace + addedSpace, unitType), new Unit(0, unitType)); } //resizing the columns oldCellSize = ((TextBox)currentReport.Items["pageHeader"].Items[col.Key]).Size; ((TextBox)currentReport.Items["pageHeader"].Items[col.Key]).Size = ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum - 1]).Size = new SizeU(new Unit(oldCellSize.Width.Value + auxSpace, unitType), new Unit(0.2, unitType)); if (headerWidthDict.ContainsKey(col.Key)) //getting number of rows for which the column name was wrapped maxRows = Math.Max(maxRows, Unit.FromPixels(headerWidthDict[col.Key], unitType).Value / (oldCellSize.Width.Value + auxSpace - Unit.FromPixels(headerWidthDict[col.Key] / col.Value[1].Length, unitType).Value)); addedSpace += auxSpace; } } else //if there are columns which were cut to the average column width { double spacePerColumn = 0; maxRows = 0; colNum = 0; SizeU oldCellSize; PointU oldCellLocation; var sum = columnToExpand.Sum(x => x.Value); //summarized space in units which forcibly shortened columns wanted to occupy over the average width value double addedSpace = 0; //units already got by the shortened columns from the extra space //columns which weren't shortened forcibly var headers = currentReport.Items["pageHeader"].Items.Cast<TextBox>().ToList(); headers = headers.Where(x => headers.IndexOf(x) > 0 && !columnToExpand.ContainsKey(headers.IndexOf(x)) && !emptyColumns.Contains(headers.IndexOf(x))).ToList(); double fullWidthHeadersSum = headers.Sum(x => x.Size.Width.Value); //summarized width of non-shortened columns in units double maxHeaderWidth = headers.Max(x => x.Size.Width.Value); //the widest header from all non-shortened headers in units double averageHeaderWidth = headers.Average(x => x.Size.Width.Value); //average width of non-shortened headers in units averageHeaderWidth += (maxHeaderWidth - averageHeaderWidth) * 0.3; //average value is taken 30% closer to maximum value to ensure that more columns could get additional space double avgUp = headers.Where(x => x.Size.Width.Value > averageHeaderWidth).Count(); //number of headers that are wider than average double extraSizeBoost = 0; //units used to enlarge headers that are narrower than average //second rearrangement (complex: if the extra space is smaller than the sum of units that shortened columns wanted to occupy over the average width, //than this extra space is devided between shortened columns according to the portion of each column in that sum; //otherwise each shortened column gets its desired additional space and the remaining is redistributed between the non-shortened columns //based on the principle of less space to shorter columns, more space to wider ones) foreach (var col in _colDict) { colNum++; if (!emptyColumns.Contains(col.Key)) { if (columnToExpand.ContainsKey(col.Key)) //if column was forcibly shortened { if (extraSpace > sum) spacePerColumn = columnToExpand[col.Key]; else spacePerColumn = extraSpace * (columnToExpand[col.Key] / sum); } else { if (extraSpace > sum) { var currentSize = ((TextBox)currentReport.Items["pageHeader"].Items[col.Key]).Size.Width.Value; if (currentSize < averageHeaderWidth) { if (_colDict.Count != colNum) { var overSize = currentSize * 0.5f; //more space to columns which are narrower than the average currentSize += overSize; extraSizeBoost += overSize; } } else { currentSize -= (float)(extraSizeBoost / avgUp); } spacePerColumn = (extraSpace - sum) * (currentSize / fullWidthHeadersSum); } else spacePerColumn = 0; } } else spacePerColumn = 0; if (colNum < _colDict.Count) { oldCellLocation = ((TextBox)currentReport.Items["pageHeader"].Items[col.Key + 1]).Location; ((TextBox)currentReport.Items["pageHeader"].Items[col.Key + 1]).Location = new PointU(new Unit(oldCellLocation.X.Value + spacePerColumn + addedSpace, unitType), new Unit(0.4, unitType)); oldCellLocation = ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum]).Location; ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum]).Location = new PointU(new Unit(oldCellLocation.X.Value + spacePerColumn + addedSpace, unitType), new Unit(0, unitType)); } oldCellSize = ((TextBox)currentReport.Items["pageHeader"].Items[col.Key]).Size; ((TextBox)currentReport.Items["pageHeader"].Items[col.Key]).Size = ((ReportItem)currentReport.Items["detail"].Items["rowPanel"].Items[colNum - 1]).Size = new SizeU(new Unit(oldCellSize.Width.Value + spacePerColumn, unitType), new Unit(0.2, unitType)); addedSpace += spacePerColumn; oldCellSize = ((TextBox)currentReport.Items["pageHeader"].Items[col.Key]).Size; if (headerWidthDict.ContainsKey(col.Key)) maxRows = Math.Max(maxRows, Unit.FromPixels(headerWidthDict[col.Key], unitType).Value / (oldCellSize.Width.Value + spacePerColumn - Unit.FromPixels(headerWidthDict[col.Key] / col.Value[1].Length, unitType).Value)); } } } //allocation of group aggregates foreach (var groupName in _groups) { var group = currentReport.Groups.FirstOrDefault(x => x.Name == groupName); if (group != null) { var footerPanel = (Panel)group.GroupFooter.Items[0]; var groupIndex = _groups.IndexOf(groupName); var groupOffset = new Unit(groupIndex * 0.15, currentReport.Width.Type); if (footerPanel != null) { var aggregateItems = CreateAggregateItems(currentReport, true); foreach (var item in aggregateItems) { item.Location = new PointU(item.Location.X - groupOffset, item.Location.Y); item.Value = item.Value.Replace(":scope:", groupName); footerPanel.Items.Add(item); } } } } //page aggregates var pageAggregatePanel = currentReport.Items["pageFooter"].Items["pageAggregatePanel"]; if (pageAggregatePanel != null) { var pageFooter = currentReport.Items["pageFooter"] as PageFooterSection; var aggregateItems = CreateAggregateItems(currentReport, false); var maxFuncCount = _aggregateDict.Count > 0 ? _aggregateDict.Max(x => x.Value.Count) : 0; pageFooter.Height += Unit.FromPixels(16, unitType) * maxFuncCount; foreach (var item in aggregateItems) { item.Style.Font.Size = new Unit(10); pageAggregatePanel.Items.Add(item); } } if (maxRows < 1) //height of Page Header Section is reduced by one row { ((PageHeaderSection)currentReport.Items["pageHeader"]).Height -= Unit.Inch(0.1); //_originalPageHeaderHeight -= Unit.Inch(0.1); } else if (maxRows > 2) //height of Page Header Section is increases by number of rows (if there are more than two) { ((PageHeaderSection)currentReport.Items["pageHeader"]).Height += Unit.Inch(Math.Min(Math.Ceiling(maxRows - 2) * (headerHeight > 0 ? Unit.FromPixels(headerHeight, unitType).Value : 0.25), 1.5)); //_originalPageHeaderHeight += Unit.Inch(Math.Min(Math.Ceiling(maxRows - 2) * (headerHeight > 0 ? Unit.FromPixels(headerHeight, unitType).Value : 0.25), 1.5)); } }