如何为列添加sum的标签。我需要使用silverlight工具包中的图表堆叠系列
来做到这一点
答案 0 :(得分:0)
创建自定义StackedColumnSeriesEx! 在这里你应该找到我们正在使用新的自定义数据点类型的定义,我们可以找到数据点值的总和。我们还应该在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);
}
}
新的CustomDataPoint,几乎没有新的依赖属性
IsTopPoint - bool值,表示当前点是否为最高点!
DependentValueSum - 要显示的double值。依赖属性
新样式的CustomDataPoint用于绘制文本DependentValueSum,以防它的最高值:
`
<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>`