Gaining better experience by converting input data

Alexander Bub

It is a regular situation when a developer has to deal with the input data having the structure not really suitable for the report output. Of course, it would be a lot more convenient for the developer if the input data was initially converted to a proper format, but this could cause a number of problems. First of all, if the data is presented by the subsystem the developer cannot affect, it would cause difficulties in data coordination. Secondly, as we know, many various reports can use the same data and this would make the generation of specific input data for each particular report quite an uncomfortable task . Thirdly, the report structure can vary easily and rewriting the data subsystem each time is not the best thing a developer can think of. On the other hand, the developer can use the initial data format without modifying it, but this can also be unacceptable as the report complexity would increase without a necessity.

The most acceptable approach in this situation would be modifying the raw data into the easy-to-use structure. To do this, a couple of things should be taken into consideration. The first is that report’s CommonScript property makes possible describing our own classes. And the second one is that by using DataObjects.Add method we can register our own data sources on par with other report data sources.
 
A study case.
 
Let’s look into the sample from our first article (http://blogs.perpetuumsoft.com/dotnet/taming-the-crossband/) and assume we receive the data in the following view, which looks pretty similar to picking the data from an ordinary database:
 

  1. public class RawData  
  2. {  
  3.     public int Year { getset; }  
  4.     public String ParameterName { getset; }  
  5.     public double Value { getset; }  
  6. }  

 
If you managed to read the first article of the series, you possibly noticed the fact that the idea of our success is using the convenient data structures, rather than RawData as such. It appears the RawData would not let us create anything so easy and so simple for our test cases. But basically, we are not limited to anything, that’s why we can easily transform the data to look the same way as it was in our first article.
 
To begin with, we should declare those ‘comfy’ data structures in CommonScript of our sample report. Please note, the constructors of our classes allow creating the whole matrix by passing the parameter names and year values which will come in handy when filling declared data structures:
 
  1. public class TableData  
  2. {  
  3.     public TableData(string[] parameterNames, string[] columnNames)  
  4.     {  
  5.         Rows = new System.Collections.Generic.List<TableRow>();  
  6.         ColumnNames = new System.Collections.Generic.List<string>(columnNames);  
  7.         for(int i = 0;i < parameterNames.Length;i++)  
  8.         {  
  9.             TableRow row = new TableRow(columnNames.Length);  
  10.             row.Name = parameterNames[i];  
  11.             Rows.Add(row);  
  12.         }  
  13.     }  
  14.     public System.Collections.Generic.List<string> ColumnNames {get;set;}  
  15.     public System.Collections.Generic.List<TableRow> Rows { getprivate set; }  
  16. }  
  17.   
  18. public class TableRow  
  19. {  
  20.     public string Name { getset; }  
  21.     public TableRow(int columnsCount)  
  22.     {  
  23.         Values = Enumerable.Repeat(0d, columnsCount).ToList();  
  24.     }  
  25.     public System.Collections.Generic.List<double> Values { getset; }  
  26. }  

 
Further we get our data source in GenerateScript of the document:
 
  1. System.Collections.Generic.List rawData = DataObjects["rawData"as System.Collections.Generic.List;  

 
At this point we should convert this data into a structure defined in CommonScript. Let’s assume the data subsystem developers overlooked the aspect of grouping the data, and occasionally we come across multiple data for the same combination of parameters. In this case we should sum this data up.
For simplicity’s sake we can use Linq (please refer to the article under the link for more information http://helpcenter.perpetuumsoft.com/KB/a496/using-linq-in-complex-scenarios-in-sharpshooter-reports.aspx). We would get the years and parameter lists, create the matrix and fill it in by iterating through the data:
 
  1. var years = rawData.Select(x => x.Year.ToString()).Distinct().OrderBy(x=>x).ToArray();  
  2. var parameters = rawData.Select(x => x.ParameterName).Distinct().OrderBy(x=>x).ToArray();  
  3.   
  4. var data = new TableData(parameters, years);  
  5. for(int i = 0;i < rawData.Count;i++)  
  6. {  
  7.     int rowIndex = Array.IndexOf(parameters, rawData[i].ParameterName);  
  8.     int columnIndex = Array.IndexOf(years, rawData[i].Year.ToString());  
  9.       
  10.     data.Rows[rowIndex].Values[columnIndex] += rawData[i].Value;  
  11. }  

 
Finally, we should add a newly created data source to our report data sources list:
 
  1. DataObjects.Add(“data”, data);  

 
…and see the result.
 
pict_ure
 
The sample for the article can be found under the following link:
 
http://perpetuumsoft.com/Support/samples/CrossBandSampleDataConvertation.zip

April 16th, 2014

Leave a Comment