I am using the NReco library to build Pivot Tables. I'm following the examples and I was able to build a simple PvT from a DataSet. Now I want to build a PvT that receives a number of measures that are chosen at run time, but I'm having trouble with the dynamic nature of the process of using a list of measures each with it´s own formula of aggregation. The formula is known at run-time, and it's nothing more than a sum or an average, but is specific to the measure. I have the following code:
private string CreatePivotTable(DataTable dt, string[] lines, string[] columns, string[] dimensions, string measure)
{
var pivotData = new PivotData(dimensions, new SumAggregatorFactory(measure), new DataTableReader(dt));
var pivotTable = new PivotTable(lines, columns, pivotData);
var htmlResult = new StringWriter();
var pvtHtmlWr = new PivotTableHtmlWriter(htmlResult);
pvtHtmlWr.Write(pivotTable);
return htmlResult.ToString();
}
I wanted to do something like the following code to add the measures and the aggregator dynamically at run time:
private string CreatePivotTable(DataTable dt, string[] lines, string[] columns, string[] dimensions, Measure[] measures)
{
var pivotData = new PivotData(dimensions, null, new DataTableReader(dt));
foreach(var m in measures)
{
if (m.Formula.equals("sum"))
pivotData.AggregatorFactory.Create(new SumAggregator(m.ColName));
else if(m.Formula.equals("avg")){
pivotData.AggregatorFactory.Create(new AvgAggregator(m.ColName));
}
}
}
How can I achieve something like this? Is there a way to do it?