/// <summary> /// Customize the display text of individual column and row headers and filter dropdown items /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e) { string txt; PivotGridValueType vt = e.ValueType; // Value, Total, GrantTotal, CustomTotal PivotGridFieldContext fc = GetPivotGridFieldContext(e.Field); if (fc == null) { return; } if (fc.Aggregation.IsGroupingType) { object vo = e.Value; txt = GroupingMethods.FormatGroupedValue(fc.Qc, fc.Aggregation, vo); e.DisplayText = txt; } else if (fc.Aggregation.Role == AggregationRole.DataSummary) // data area header when 2 or more data fields { txt = PivotGridControlMx.BuildFieldCaption(fc.F); e.DisplayText = txt; } else { return; } return; }
/// <summary> /// Calculate group value from incoming data value /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_CustomGroupInterval(object sender, PivotCustomGroupIntervalEventArgs e) { PivotGridFieldContext fc = GetPivotGridFieldContext(e.Field); PivotGridFieldMx f = fc.F; AggregationDef ad = f.Aggregation; if (e.Field.Area != PivotArea.ColumnArea && e.Field.Area != PivotArea.RowArea) { return; // not sure why this is happening but ignore if so } if (!ad.IsGroupingType) // must check this since when moving a field to a grouping area in the PivotGridDialog, this event fires before the FieldAreaChanged event (true?) { return; // throw new Exception("Expected Group Role"); } AggregationTypeDetail td = ad.TypeDetail; if (td.GroupingMethod == null) { return; } QueryColumn qc = fc.Qc; object vo = e.Value; // value to apply group function to object groupValue = td.GroupingMethod(qc, ad, vo); e.GroupValue = groupValue; }
/// <summary> /// GetPivotGridFieldContext /// </summary> /// <param name="f"></param> /// <returns>PivotGridFieldContext</returns> public static PivotGridFieldContext GetPivotGridFieldContext( PivotGridFieldBase fb, bool throwException = false) { AggregationDef a; PivotGridFieldContext fc = new PivotGridFieldContext(); PivotGridFieldMx f = fb as PivotGridFieldMx; if (f == null) { if (throwException) { throw new Exception("PivotGridFieldMx is null"); } else { return(null); } } fc.F = f; fc.ResultsField = f.ResultsField as ResultsField; if (fc.ResultsField == null) { if (throwException) { throw new Exception("ResultsField is null"); } else { return(null); } } fc.Qc = fc.ResultsField.QueryColumn; fc.Mc = fc.Qc.MetaColumn; fc.Q = fc.Qc.QueryTable.Query; fc.Qm = fc.Q.QueryManager as QueryManager; if (fc.Qm != null) { fc.Qe = fc.Qm.QueryEngine; } a = fc.Aggregation = f.Aggregation; if (a != null && a.Role != PivotGridFieldMx.DxAreaToMxRole(f)) // be sure Mx role is in synch with Dx FieldArea which may change before FieldAreaChanged event fires { a.Role = PivotGridFieldMx.DxAreaToMxRole(f); a.SetDefaultTypeIfUndefined(fc.Mc); // also set type if not defined yet } return(fc); }
/// <summary> /// Get unbound data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PivotGridControl_CustomUnboundFieldData(object sender, DevExpress.XtraPivotGrid.CustomFieldDataEventArgs e) { MobiusDataType mdt = null; NumberMx nex; StringMx sex; DateTimeMx dex; QualifiedNumber qn; double dVal; string cid; int iVal, rti, dri, dri2; //int t0 = TimeOfDay.Milliseconds(); if (PivotGrid.DataSource == null) { return; } PivotGridFieldContext fc = GetPivotGridFieldContext(e.Field); dri = e.ListSourceRowIndex; object vo = Dtm.GetColumnValueWithRowDuplication(fc.ResultsField, dri); if (vo != null) { Type voType = vo.GetType(); if (MobiusDataType.IsMobiusDataType(vo)) { if (vo is QualifiedNumber) { qn = vo as QualifiedNumber; if (!Lex.IsDefined(qn.Qualifier)) // if no qualifier then just use double NumberValue { vo = qn.NumberValue; } } else // otherwise convert to a primitive { vo = MobiusDataType.ConvertToPrimitiveValue(vo); } } if (vo is DateTime) { vo = ((DateTime)vo).ToBinary(); // handle DateTimes as int64 (longs) to avoid performance hit in Dx code } } e.Value = vo; return; }
/// <summary> /// The area that a field is located in has changed. /// If changed by the DX UI then the added members in PivotGridFieldMx must be updated /// If changed by Mobius code (InSetup = true) then nothing needed here. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_FieldAreaChanged(object sender, PivotFieldEventArgs e) { if (InSetup) { return; } try { InSetup = true; PivotGridFieldContext fc = GetPivotGridFieldContext(e.Field); if (fc == null) { return; } PivotGridFieldMx f = fc.F; f.SyncMxRoleToDxArea(); ResultsField rfld = fc.ResultsField; if (rfld == null || rfld.MetaColumn == null) { return; } f.Aggregation.SetDefaultTypeIfUndefined(rfld.MetaColumn); // may need to set default type as well PivotGridControlMx.SetFieldCaption(f); PivotGrid.RefreshData(); // recalc summaries if (UpdateViewWhenGridControlChanges) { View.UpdateViewFieldsFromGridFields(); } return; } finally { InSetup = false; } }
/// <summary> /// Calculate summary value /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_CustomSummary(object sender, PivotGridCustomSummaryEventArgs e) { int ri, dri; int queryEngineInstanceId = -1; PivotGridFieldContext f = GetPivotGridFieldContext(e.DataField); if (f.Aggregation.Role != AggregationRole.DataSummary) // must check this since when moving a field to a new area in the PivotGridDialog, this event fires before the FieldAreaChanged event { return; // throw new Exception("Expected DataSummary Role"); } DataTableMx dt = f.Qm.DataTable; PivotDrillDownDataSource ds = e.CreateDrillDownDataSource(); List <object[]> voList = new List <object[]>(); for (ri = 0; ri < ds.RowCount; ri++) // get list of rows containing values to summarize { PivotDrillDownDataRow row = ds[ri]; dri = row.ListSourceRowIndex; if (dri >= dt.Rows.Count) { throw new Exception("dri >= dt.Rows.Count"); } object[] voa = dt.Rows[dri].ItemArrayRef; voList.Add(voa); continue; } if (f.Qe != null) { queryEngineInstanceId = f.Qe.Id; } Aggregator ag = new Aggregator(); object r = ag.AggregateQueryColumn(queryEngineInstanceId, f.Qc, f.Aggregation, voList); // do the aggregation e.CustomValue = r; // return value return; }
/// <summary> /// Customize the display text for the cells displayed within the Data Area. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_CustomCellDisplayText(object sender, PivotCellDisplayTextEventArgs e) { string txt; QualifiedNumber qn = e.Value as QualifiedNumber; // summary values should be QNs if (qn == null) { return; } PivotGridFieldContext f = GetPivotGridFieldContext(e.DataField); AggregationTypeDetail atd = f.Aggregation.TypeDetail; if (atd == null || !atd.FractionalSummaryResult) { return; // just return if not numeric type } if (qn.IsNull) { e.DisplayText = ""; return; } ColumnFormatEnum displayFormat = f.Qc.ActiveDisplayFormat; int decimals = f.Qc.ActiveDecimals; if (f.Mc.DataType == MetaColumnType.Integer) { displayFormat = ColumnFormatEnum.Decimal; decimals = 1; } txt = qn.Format(f.Qc, false, displayFormat, decimals, QnfEnum.Combined | QnfEnum.NValue, OutputDest.WinForms); e.DisplayText = txt; return; }
/// <summary> /// BuildFieldPopupMenu /// </summary> /// <param name="e"></param> void BuildFieldPopupMenu( PopupMenuShowingEventArgs e) { PivotArea area = (PivotArea)(-1); string areaName = ""; Image areaImage = null; DXSubMenuItem smi; PivotGridFieldContext f = GetPivotGridFieldContext(e.Field); if (f == null) { return; } PivotGridField = f.F; // save as current field ResultsField rfld = f.ResultsField; QueryColumn qc = f.Qc; GetFieldAreaAttributes(e, out area, out areaName, out areaImage); DXPopupMenu dxpMenu = e.Menu; // clear dest menu dxpMenu.Items.Clear(); AggregationDefMenus ats = new AggregationDefMenus(); // used to build menus // Build DataArea summary type items if (area == PivotArea.DataArea) { ContextMenuStrip modelMenu = ats.SetupAggregationTypeMenu(qc, f.Aggregation, null, includeGroupingItems: false, includeSummaryItems: true); ConvertAndAppendContextMenuStripToDxPopupMenu(modelMenu, dxpMenu); } // Build ColumnArea or RowArea Grouping items if (area == PivotArea.ColumnArea || area == PivotArea.RowArea) { ContextMenuStrip modelMenu = ats.SetupAggregationTypeMenu(qc, f.Aggregation, null, includeGroupingItems: true, includeSummaryItems: false); ConvertAndAppendContextMenuStripToDxPopupMenu(modelMenu, dxpMenu); } if (dxpMenu.Items.Count == 0) // just add to menu if nothing there so far { smi = dxpMenu; } else // add to submenu { smi = new DXSubMenuItem("Move Field to"); smi.BeginGroup = true; dxpMenu.Items.Add(smi); } smi.Items.Add(new DXMenuCheckItem("Column Area", area == PivotArea.ColumnArea, Bitmaps16x16.Images[0], ColumnAreaMenuItem_Click)); smi.Items.Add(new DXMenuCheckItem("Row Area", area == PivotArea.RowArea, Bitmaps16x16.Images[1], RowAreaMenuItem_Click)); smi.Items.Add(new DXMenuCheckItem("Data Area", area == PivotArea.DataArea, Bitmaps16x16.Images[2], DataAreaMenuItem_Click)); smi.Items.Add(new DXMenuCheckItem("Filter Area", area == PivotArea.FilterArea, Bitmaps16x16.Images[3], FilterAreaMenuItem_Click)); smi.Items.Add(new DXMenuCheckItem("None", area == (PivotArea)(-1), null, UnassignedAreaMenuItem_Click)); return; }
/// <summary> /// Display cell text as hyperlinks to allow drilldown /// Also, set the background color if appropriate (e.g. bin that an SP/CRC value is in) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e) { e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Underline); e.Appearance.ForeColor = Color.Blue; PivotGridFieldContext f = GetPivotGridFieldContext(e.DataField); if (f == null) { return; } ResultsField rfld = f.ResultsField; if (rfld == null) { return; } Mobius.Data.QueryColumn qc = rfld.QueryColumn; MetaColumn mc = qc.MetaColumn; CondFormat cf = f.Qc.CondFormat; //if (cf == null || cf.Rules.Count == 0) return; object cv = e.GetCellValue(e.ColumnIndex, e.RowIndex); double d = -1; if (cv is int) { d = (int)cv; } else if (cv is double) { d = (double)cv; } else { return; } if (Math.Abs(1) == 2) // for compound by target { if (d >= 1 && d <= 10) { // todo: set proper color Color c = UnpivotedAssayResult.CalculateBinColor((int)d); e.Appearance.BackColor = c; } } if (DebugMx.False) // color by count { d = d / 45; if (d > 10) { d = 10; } if (d < 1) { d = 1; } if (d >= 1 && d <= 10) { // todo: set proper color Color c = UnpivotedAssayResult.CalculateBinColor((int)d); e.Appearance.BackColor = c; } } if (DebugMx.False) // for bin by target { PivotGridField[] rowFields = e.GetRowFields(); if (d > 0 && rowFields.Length > 0) { object rfv = e.GetFieldValue(rowFields[0]); if (rfv == null) { return; } else if (rfv is int) { Color c = UnpivotedAssayResult.CalculateBinColor((int)rfv); e.Appearance.BackColor = c; } } } }
/// <summary> /// Draw conditional formatting field value headers with appropriate background color /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Grid_CustomDrawFieldValue(object sender, PivotCustomDrawFieldValueEventArgs e) { string txt; PivotGridFieldContext fc = GetPivotGridFieldContext(e.Field); if (fc == null) { return; } ResultsField rfld = fc.ResultsField; if (rfld == null) { return; } //DebugLog.Message(rfld.QueryColumn.MetaColumn.Name); //if (rfld.QueryColumn.MetaColumn.Name == "ACTIVITY_BIN") rfld = rfld; // debug //if (e.DisplayText == "0") e = e; // debug CondFormat cf = rfld.QueryColumn.CondFormat; if (cf == null || cf.Rules.Count == 0) { return; } foreach (CondFormatRule rule in cf.Rules) { // look for rule name that matches field value text if (Lex.Eq(rule.Name, e.DisplayText) || Lex.Eq(rule.Value, e.DisplayText)) { if (!Lex.IsNullOrEmpty(rule.Name)) { txt = rule.Name; // display rule name if defined } else { txt = e.DisplayText; // otherwise display field value } Rectangle rect = e.Bounds; ControlPaint.DrawBorder3D(e.Graphics, e.Bounds); Brush brush = e.GraphicsCache.GetSolidBrush(rule.BackColor1); rect.Inflate(-1, -1); e.Graphics.FillRectangle(brush, rect); e.Appearance.DrawString(e.GraphicsCache, txt, e.Info.CaptionRect); //e.Appearance.DrawString(e.GraphicsCache, e.Info.Caption, e.Info.CaptionRect); foreach (DevExpress.Utils.Drawing.DrawElementInfo info in e.Info.InnerElements) { DevExpress.Utils.Drawing.ObjectPainter.DrawObject(e.GraphicsCache, info.ElementPainter, info.ElementInfo); } e.Handled = true; return; //e.Painter.DrawObject(e.Info); // change tone of skin //e.Graphics.FillRectangle(e.GraphicsCache.GetSolidBrush(Color.FromArgb(50, 0, 0, 200)), e.Bounds); //e.Handled = true; } } return; }