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

时间:2013-08-26 17:39:21

标签: c# silverlight telerik

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

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

有一些方法可以避免所有红色和绿色调色板,并且仍然可以使用随机颜色渲染图表吗?

1 个答案:

答案 0 :(得分:0)

Toolkit图表的解决方案是在C#代码中生成颜色并完全重写饼图数据点模板。 顺便说一下,我找到了一篇文章,解释了Telerik图表的相同方法: Binding the Color of Series Items

因此,如果您想消除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>