Format pivot table PivotData C# examples
This documentation describes how to customize pivot table produced by
PivotTableHtmlWriter
(Toolkit component).
Format labels and measure headers
Table labels may be customized by defining FormatKey
handler:
var pvtHtmlWr = new PivotTableHtmlWriter(outputWr); pvtHtmlWr.FormatKey = (key, dim) => { // formatting rule based on key type if (key is DateTime) return String.Format("{0:d}", key); // formatting rule based on dimension name if (dim=="month" && key is int) return ResolveMonthName((int)key); return Convert.ToString(key); };
Germany | Belgium | |
---|---|---|
Product1 | 15 | 17 |
Product2 | 10 | 1 |
Product3 | 2 |
For pivot table with multiple values measure headers are displayed and they may be customized with FormatMeasureHeader
:
pvtHtmlWr.FormatMeasureHeader = (aggrFactory, measureIndex) => { // customize header by factory type or measure index return aggrFactory.ToString(); };
Germany | Belgium | |||
---|---|---|---|---|
Count | Sum of Amount | Count | Sum of Amount | |
Product1 | 15 | 677 | 17 | 898 |
Product2 | 10 | 251 | 1 | 12 |
Product3 | 2 | 31 |
Dimension labels may be customized with FormatDimensionLabel
handler:
pvtHtmlWr.FormatDimensionLabel = (dim) => { // dim is a value from PivotData.Dimensions return dim=="product_name" ? "Product" : dim; };
Product | Country | Germany | Belgium |
---|---|---|---|
Product1 | 15 | 17 | |
Product2 | 10 | 1 | |
Product3 | 2 |
Format table values
pvtHtmlWr.FormatValue = (aggr,measureIdx) => { if (aggr.Count==0) return String.Empty; if (measureIdx==1) // index is zero-based return String.Format("${0:0.##}", aggr.Value); // apply default number format return String.Format("{0:0.##}", aggr.Value); };
Germany | Belgium | |||
---|---|---|---|---|
Count | Sum of Amount | Count | Sum of Amount | |
Product1 | 15 | $677 | 17 | $898 |
Product2 | 10 | $251 | 1 | $12 |
Product3 | 2 | $31 |
Note: formatting handlers may return HTML content if PivotTableHtmlWriter.AllowHtml property set to true
.
Advanced table cells customization
When cells formatting and/or visual styling depends on the context it is possible to derive from PivotTableHtmlWriter
class
and override OnWriteKeyCell
and OnWriteValueCell
methods:
public class CustomPvtTblHtmlWriter : PivotTableHtmlWriter { public CustomPvtTblHtmlWriter(TextWriter writer) : base(writer) { // enable HTML content for this writter by default AllowHtml = true; } protected override void OnWriteKeyCell(PivotTableHtmlWriter.KeyCellContext keyContext) { if (keyContext.Dimension=="Product" && keyContext.DimensionKey.Equals("Product1")) { // adds css class to TH with key label keyContext.AddCssClass("product1"); // redefine TH cell's content keyContext.FormattedKey = "<a name='product1'>"+keyContext.FormattedKey+"</a>"; } } protected override void OnWriteValueCell(PivotTableHtmlWriter.ValueCellContext valueContext) { if (valueContext.RowKey.DimKeys[0].Equals("Product1")) { // adds css class to TD with value valueContext.AddCssClass("product1"); // add suffix to TD cell's for some values if (Convert.ToDecimal(valueContext.Aggregator.Value)<10) valueContext.FormattedValue += " (!)"; } } }