Silverlight工具包;避免某些颜色的饼图

本文关键字:颜色 工具包 Silverlight | 更新日期: 2023-09-27 18:29:01

我们需要为服务类型创建一个图表。这些类型可以动态添加,我没有为每个类型定义合适的颜色。

因此,我们使用Telerik Silverlight RadChart,一切都很好,直到客户端提出了技巧要求。我们不应该在网格中显示两种颜色:红色和绿色。

有什么方法可以避免所有的红色和绿色调色板,仍然可以用随机颜色渲染图表?

Silverlight工具包;避免某些颜色的饼图

Toolkit图表的解决方案是在C#代码中生成颜色,并完全重写饼图数据点模板。顺便说一句,我发现一篇文章解释了Telerik图表的相同方法:绑定系列项目的颜色

因此,如果要消除2种颜色,只需将其他一些颜色设置为ChartItemModel.Color属性即可。C#代码将是这样的:

public class ChartItemModel
{
    public string Title { get; set; }
    public double Value { get; set; }
    public Brush Color { get; set; }
}
var chartItems = new [] 
{ 
    new ChartItem { Title = "Item1", Value = 25, Color = (Brush)Resources["BlueBrush"] }, 
    new ChartItem { Title = "Item2", Value = 75, Color = (Brush)Resources["YellowBrush"] }
    // other items, the Color property of which doesn't have a red or green brush
};
// binding

PieDataPoint模板是从内置模板复制而来的,并通过将绑定添加到笔刷(Fill="{Binding Color}")进行更改:

<charting:PieSeries.DataPointStyle>
    <Style TargetType="charting:PieDataPoint">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="charting:PieDataPoint">
                    <Grid x:Name="Root" Opacity="0">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.1" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="MouseOverHighlight" Storyboard.TargetProperty="Opacity" To="0.6" Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.1" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="SelectionHighlight" Storyboard.TargetProperty="Opacity" To="0.6" Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="RevealStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.5" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Shown">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Hidden">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0" Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Path x:Name="Slice" Data="{TemplateBinding Geometry}" Fill="{Binding Color}" Stroke="{TemplateBinding BorderBrush}" StrokeMiterLimit="1">
                            <ToolTipService.ToolTip>
                                <StackPanel>
                                    <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
                                    <ContentControl Content="{TemplateBinding FormattedRatio}" />
                                </StackPanel>
                            </ToolTipService.ToolTip>
                        </Path>
                        <Path x:Name="SelectionHighlight" Data="{TemplateBinding GeometrySelection}" Fill="Red" StrokeMiterLimit="1" IsHitTestVisible="False" Opacity="0" />
                        <Path x:Name="MouseOverHighlight" Data="{TemplateBinding GeometryHighlight}" Fill="White" StrokeMiterLimit="1" IsHitTestVisible="False" Opacity="0" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</charting:PieSeries.DataPointStyle>