Silverlight 工具包。 StackedColumnSeries 在列上添加标签

发布于 2024-10-07 00:31:22 字数 123 浏览 2 评论 0原文

如何为列添加带有总和的标签。我需要使用 silverlight 工具包中的图表堆叠系列来做到这一点

alt text

How to add labels with sum for column. I need to do that with chart stacked series from silverlight toolkit

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-10-14 00:31:22
  1. 已创建自定义 StackedColumnSeriesEx!
    在这里您应该找到定义我们正在使用新的自定义数据点类型,并且我们可以找到数据点值的总和。此外,我们还应该定义哪个数据点位于 StackedColumnSeries 中最靠前的位置:

     公共类 StackedColumnSeriesEx : StackedColumnSeries
    {
        受保护的重写 DataPoint CreateDataPoint()
        {
        // 具有新字段的自定义数据点。
            返回新的 CustomDataPoint();
        }
    
        protected override void UpdateDataItemPlacement(IEnumerable dataItems)
            {
            // 在这里计算总和。
                foreach(this.IndependentValueGroups 中的 var 组)
                {
    
                    小数和 = 0;
    
    
    
               foreach(组中的var dataItem.DataItems)
                        {
                            双电流值 = 0;
                            if (ValueHelper.TryConvert(dataItem.ActualDependentValue, out currentValue))
                            {
                                sum += Convert.ToDecimal(currentValue);
                            }
                        }
    
                        // 设置总和并找到最高点
                        foreach(组中的DataItem dataItem.DataItems)
                        {
                            int index = group.DataItems.IndexOf(dataItem);
    
                            var ConvertedDataItem = dataItem.DataPoint as CustomDataPoint;
                            if (convertedDataItem == null)
                            {
                                继续;
                            }
    
                            ConvertedDataItem.SeriesDefinition = dataItem.SeriesDefinition;
                            ConvertedDataItem.IsTopPoint = index + 1 == group.DataItems.Count();
                            ConvertedDataItem.DependentValueSum = sum;
                        }
                    }
    
    
    
                        base.UpdateDataItemPlacement(dataItems);
                    }
                }
    
  2. 新的 CustomDataPoint 几乎没有新的依赖属性

    IsTopPoint - 布尔值,指示当前点是否是最上面的点!

    DependentValueSum - 要显示的双精度值。依赖属性

  3. 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>`
  1. 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);
                    }
                }
    
  2. 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

  3. 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>`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文