1

我们需要为服务类型创建一个图表。这些类型可以动态添加,并且我没有为它们中的每一个提供正确的颜色定义。

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

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

4

1 回答 1

0

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>
于 2013-10-11T19:01:03.360 回答