| <div id="midcolumn"> | |
| <h3>Summary row</h3> | |
| <div id="doccontent"> | |
| <div class="chapter"> | |
| <div class="content"> | |
| <p> | |
| You can add a summary row to your NatTable easily by adding and configuring the | |
| <span class="code">SummaryRowLayer</span>. | |
| </p> | |
| <p style="text-align: center;"> | |
| <img align="middle" src="images/summary_row.png" border="0" alt="Summary Row Example"/> | |
| </p> | |
| <p> | |
| The <span class="code">Creating_a_summary_row</span> and <span class="code">CalculatingGridExample</span> | |
| examples are the best places to look at. | |
| </p> | |
| </div> | |
| </div> | |
| <div class="chapter"> | |
| <h5>Adding the SummaryRowLayer to the body layer stack</h5> | |
| <div class="content"> | |
| <p> | |
| To add a summary row to your NatTable, you have to add the <span class="code">SummaryRowLayer</span> | |
| to your body layer stack. As it should deal directly to the data shown in the NatTable, it should | |
| be added to the body layer close to the <span class="code">DataLayer</span>. As with all layers this | |
| can easily be done with the following code snippet: | |
| </p> | |
| <div class="codeBlock">IConfigRegistry configRegistry = new ConfigRegistry(); | |
| IUniqueIndexLayer dataLayer = new DataLayer(myDataProvider); | |
| // Plug in the SummaryRowLayer | |
| IUniqueIndexLayer summaryRowLayer = | |
| new SummaryRowLayer(dataLayer, configRegistry, false); | |
| ViewportLayer viewportLayer = new ViewportLayer(summaryRowLayer);</div> | |
| <p class="subline">Snippet from Creating_a_summary_row example</p> | |
| <p> | |
| Note that the <span class="code">SummaryRowLayer</span> needs the <span class="code">ConfigRegistry</span> | |
| to access the summary row specific configurations. | |
| </p> | |
| </div> | |
| </div> | |
| <div class="chapter"> | |
| <h5>Configuring the summary data</h5> | |
| <div class="content"> | |
| <p> | |
| The next step on adding a summary row to a NatTable is to configure the content that should | |
| be showed within the summary row. This is done by <span class="code">ISummaryProvider</span> | |
| that need to be configured via <span class="code">ConfigRegistry</span>. | |
| </p> | |
| <p> | |
| NatTable comes with three default <span class="code">ISummaryProvider</span> | |
| <ul> | |
| <li>ISummaryProvider.DEFAULT<br> | |
| always returns the String ...</li> | |
| <li>ISummaryProvider.NONE<br> | |
| always returns null which skips calls to the <span class="code">ISummaryProvider</span> and | |
| is a performance tweak</li> | |
| <li>SummationSummaryProvider<br> | |
| needs the body data provider so it can iterate over all values for the column it is | |
| configured to and summarize all the number values to a resulting float value</li> | |
| </ul> | |
| Creating your own <span class="code">ISummaryProvider</span> is quite easy. The following | |
| snippet shows how to implement one to calculate the average value instead of the sum. | |
| <div class="codeBlock">class AverageSummaryProvider implements ISummaryProvider { | |
| public Object summarize(int columnIndex) { | |
| int total = 0; | |
| int rowCount = myDataProvider.getRowCount(); | |
| for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { | |
| Object dataValue = | |
| myDataProvider.getDataValue(columnIndex, rowIndex); | |
| total = total + Integer.parseInt(dataValue.toString()); | |
| } | |
| return "Average: " + total / rowCount; | |
| } | |
| }</div> | |
| <p class="subline">Snippet from Creating_a_summary_row example</p> | |
| <p> | |
| To be able to configure the <span class="code">SummaryRowLayer</span> in detail, it introduces | |
| two new labels for which the specific configurations can be added: | |
| <ul> | |
| <li>SummaryRowLayer#DEFAULT_SUMMARY_ROW_CONFIG_LABEL to all cells in the row</li> | |
| <li>SummaryRowLayer#DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + column index per column</li> | |
| </ul> | |
| </p> | |
| <p> | |
| There is a <span class="code">DefaultSummaryRowConfiguration</span> | |
| to configure the style and the summary formulas of the summary row. You can extend that or | |
| create your own configuration. In this configuration you should add your <span class="code">ISummaryProvider</span> | |
| as you like the <span class="code">SummaryRowLayer</span> to be configured. | |
| <div class="codeBlock">// Default summary provider | |
| configRegistry.registerConfigAttribute( | |
| SummaryRowConfigAttributes.SUMMARY_PROVIDER, | |
| new SummationSummaryProvider(myDataProvider), | |
| DisplayMode.NORMAL, | |
| SummaryRowLayer.DEFAULT_SUMMARY_ROW_CONFIG_LABEL); | |
| // Average summary provider for column index 2 | |
| configRegistry.registerConfigAttribute( | |
| SummaryRowConfigAttributes.SUMMARY_PROVIDER, | |
| new AverageSummaryProvider(), | |
| DisplayMode.NORMAL, | |
| SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX + 2);</div> | |
| <p class="subline">Snippet from MySummaryRowConfig of the Creating_a_summary_row example</p> | |
| </p> | |
| </div> | |
| </div> | |
| <div class="chapter"> | |
| <h5>Summary row label in the row header</h5> | |
| <div class="content"> | |
| <p> | |
| To reflect the summary row within the row header of a grid, you should consider using | |
| the <span class="code">DefaultSummaryRowHeaderDataProvider</span> to create the | |
| <span class="code">DataLayer</span> of your <span class="code">DefaultSummaryRowHeaderDataProvider</span> | |
| or something custom that looks like it. Using this the row header will show incrementing line numbers | |
| for all data rows and by default the word <i>Summary</i> for the summary row. The | |
| <span class="code">DefaultSummaryRowHeaderDataProvider</span> also has a constructor that | |
| accepts a parameter that allows you to specify your own row header label for the summary row. | |
| <div class="codeBlock">IDataProvider rowHeaderDataProvider = new DefaultSummaryRowHeaderDataProvider( | |
| bodyLayer.getDataLayer().getDataProvider(), | |
| "\u2211"); | |
| ILayer rowHeaderLayer = new RowHeaderLayer( | |
| new DefaultRowHeaderDataLayer(rowHeaderDataProvider), | |
| bodyLayer, | |
| selectionLayer);</div> | |
| <p class="subline">Snippet from CalculatingGridLayer of the CalculatingGridExample</p> | |
| </p> | |
| </div> | |
| </div> | |
| </div> | |
| </div> |