Hi,
I have two questions about XamPivotGrid:
1) How can I display TimeSpan values, defined in a custom FlatDataSource? I created a class PivotData with a value property of TimeSpan type. Then I fill a List<PivotData> with values, and create a FlatDataSource.This leaves the PivotGrid values empty (Something like "Drop Data Items here" shows up).2) Another thing that is somewhat related to this, is that I sometimes need int values, and sometimes need TimeSpan values. I tried to make the PivotData.Value property an "object", but the PivotGrid doesn't work with this either. I can imagine this could be done with a ValueConverter, but I don't really see how I can do this since the binding is on the FlatDataSource object. Also, this would still not work if the TimeSpan values do not display properly.PivotData and FlatDataSource:
public
class PivotData{ public string Row { get; set; } public string Column { get; set; } public TimeSpan Value { get; set; }}FlatDataSource = new FlatDataSource{ ItemsSource = pivotData, Cube = DataSourceBase.GenerateInitialCube("PivotData"), Rows = DataSourceBase.GenerateInitialItems("[Row]"), Columns = DataSourceBase.GenerateInitialItems("[Column]"), Measures = DataSourceBase.GenerateInitialItems("Value")};
Hello,
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.
Thanks a lot, exactly what I needed!
Great support guys!
Hi
The way to do custom formatted value is as follow:
1. Create new cell style as resource<Style TargetType="igPivot:PivotCellControl" x:Key="NewCellStyle"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="igPivot:PivotCellControl"> <Grid>
… <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Cell.Data,Converter={StaticResource CellValueFormat}}" />
… </Grid> </ControlTemplate> </Setter.Value> </Setter></Style>
2. Add converter class to your project. In convert method apply your custom formatting logic.
using System.Windows.Data;using Infragistics.Olap.Data; public class FormatValueConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ICell cell = value as ICell; if (cell != null) { //apply here your formatting string return new DateTime(long.Parse(cell.Value.ToString())).ToString("hh:mm:ss"); } return ""; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ""; } #endregion }
3. Apply the new style to pivot cell
<ig:XamPivotGrid Grid.Column="0" Name="pivotGrid" DataSource="{StaticResource pivotGridDataSource}" CellStyle="{StaticResource NewCellStyle }"/>
I have attached the full sample
Thank you, this does indeed show the values in the XamPivotGrid. I use an int value, as the .Seconds property is sufficient for my grid.
I do however still not know how to display the values like:
00:02:05
instead of just
125
As I can see from your post, you try to use “Value” property as measure. FlatDataSource is designed to work only with numeric values as measures. That is because the estimation can be made only on numbers. If you want to calculate timespan, you should wrap timespan property in long property.
public class PivotData { public string Row { get; set; } public string Column { get; set; } public long LongValue { get { return this.Value.Ticks; } } public TimeSpan Value { get; set; } }
Measures = DataSourceBase.GenerateInitialItems("LongValue")
You can use metadata to format the value
CubeMetadata cmd = new CubeMetadata { DataTypeFullName = typeof(PivotData).FullName };
cmd.DimensionSettings.Add(new DimensionMetadata { DisplayName = " LongValue ", SourcePropertyName = " LongValue ", DisplayFormat = "{0} %" }); fl.CubesSettings.Add(cmd);
Regards
Todor