Silverlight toolkit. StackedColumnSeries add label over column
How to add labels with sum for column. I need to do that with chart 开发者_运维问答stacked series from silverlight toolkit
Was created custom StackedColumnSeriesEx! Here you should find define that we are using new custom data points type and we can find sum of datapoints values. Also we should define which datapoint is located TOP most in StackedColumnSeries:
public class StackedColumnSeriesEx : StackedColumnSeries { protected override DataPoint CreateDataPoint() { // Custom data point with new fields. return new CustomDataPoint(); } protected override void UpdateDataItemPlacement(IEnumerable<DataItem> dataItems) { // Calculate sum here. foreach (var group in this.IndependentValueGroups) { decimal sum = 0; foreach (var dataItem in group.DataItems) { double currentValue = 0; if (ValueHelper.TryConvert(dataItem.ActualDependentValue, out currentValue)) { sum += Convert.ToDecimal(currentValue); } } // Set sum and find most top point foreach (DataItem dataItem in group.DataItems) { int index = group.DataItems.IndexOf(dataItem); var convertedDataItem = dataItem.DataPoint as CustomDataPoint; if (convertedDataItem == null) { continue; } convertedDataItem.SeriesDefinition = dataItem.SeriesDefinition; convertedDataItem.IsTopPoint = index + 1 == group.DataItems.Count(); convertedDataItem.DependentValueSum = sum; } } base.UpdateDataItemPlacement(dataItems); } }
New CustomDataPoint with few new dependency properies
IsTopPoint - bool value indicating whether current point is top most point!
DependentValueSum - double value to display. Dependency property
New style of CustomDataPoint to draw text DependentValueSum in case if its most top value:
`
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomDataPoint">
<Grid x:Name="Root"
Opacity="0">
<ToolTipService.ToolTip>
<ToolTip BorderThickness="0"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Background="Transparent"
BorderBrush="Transparent">
<ToolTip.Template>
<ControlTemplate TargetType="ToolTip">
<Charts:ExpensesToolTip HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</ControlTemplate>
</ToolTip.Template>
</ToolTip>
</ToolTipService.ToolTip>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.1" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected" />
</VisualStateGroup>
<VisualStateGroup x:Name="RevealStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.5" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Shown">
<Storyboard>
<DoubleAnimation Duration="0"
To="1"
Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="Root" />
</Storyboard>
</VisualState>
<VisualState x:Name="Hidden">
<Storyboard>
<DoubleAnimation Duration="0"
To="0"
Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="Root" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups><TextBlock Text="{TemplateBinding DependentValuesSum}"
VerticalAlignment="Top" Margin="-2,-21,0,0" TextAlignment="Center"
Visibility="{Binding IsTopPoint, Converter={StaticResource VisibilityConverter}, RelativeSource={RelativeSource TemplatedParent}}"
HorizontalAlignment="Center" />
<Grid x:Name="grid"
Background="{TemplateBinding Background}">
<Grid.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#87FFFFFF" />
<GradientStop Color="#D6FFFFFF"
Offset="1" />
</LinearGradientBrush>
</Grid.OpacityMask>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>`
精彩评论