Custom caption

With the introduction of InFront 3.2 we have made it possible to override the column and row header caption. On the row and column axis in the report designer there is a new property field “Custom caption” where you are able to write a c# method that will be executed for each column/row on the axis.

The method has the following input parameters:

  • Filters
    • A list of the current filters for the report.
  • Caption
    • The current translated caption of the header

There is also a helper method for a common usage pattern, transforming a date string to a formatted date.

  • ParseDate(inputFormat, outputFormat, value *optional)
    • inputFormat: the format you are mapping from.
    • outputFormat: the format you are mapping to.
    • Optionally you can add in the value string that you want to transform. If it’s not provided the Caption property will be used.
    • Documentation of valid format strings in C#: Custom Date and Time Format Strings

Example:

This is an example of how to transform a Caption from “201605” to “May 2016 “.

ParseDate(“yyyyMM”, “MMMM yyyy”);
Before After
CustomCaption3 CustomCaption4

Example:

This is an example on how to take one or more selected filter and display it in the column header.

if (string.IsNullOrEmpty(Caption)) return "";

var caption = "";

var lastCharacterCaption = Caption.Substring(Caption.Length- 1);

var senario = Filters.FirstOrDefault(f => f.OriginalFilterID == "filterreportsenario" + lastCharacterCaption);
if (senario == null || senario.Members == null || senario.Members.Count == 0) return caption;
caption += senario.Members.First().DisplayName;

var retailstore= Filters.FirstOrDefault(f => f.OriginalFilterID == "filterreportretailstore" + lastCharacterCaption);
if (retailstore== null || retailstore.Members == null || retailstore.Members.Count == 0) return caption;
if (!string.IsNullOrEmpty(Caption)) caption += Environment.NewLine;
caption += retailstore.Members.First().DisplayName;

return caption;
Before After
CustomCaption1 CustomCaption2

Quick Tip

Since there is no way to debug the c# method and inspect properties. You can use the following code snippet to list all the properties on a given object and inspect them that way. A quick tip on how to list the current properties that’s available on an object. Replace <<object>> with the object you want to inspect.

var obj = <<object>>;//object to explore
return string.Join(", ", obj.GetType().GetProperties().Select(p => p.Name + ":" + p.GetValue(obj, null)));

Example:

var filter = Filters.FirstOrDefault();
if (filter == null) { return ""; }
var obj = filter;
return string.Join(", ", obj.GetType().GetProperties().Select(p => p.Name + ":" + p.GetValue(obj, null)));

Leave a Reply