Welcome to irritatedVowel.com Sign in | Help

POKE 53280,0: Pete Brown's Blog

Silverlight, WPF, Woodworking, .NET Programming, CNC, Nature, and other topics.
Pete is an MVP for Silverlight Click here to view the MVP profile.

A Microsoft Silverlight MVP and INETA Speaker, Pete Brown writes on a number of topics including Silverlight, WPF, .NET, woodworking and working as a consultant in the DC area. read more

Subscribe

Subscribe to my feed
Add to Technorati Favorites

Community Events



Applied Information Sciences - My Employer

who's online

AddThis Social Bookmark Button

Dazzling Silverlight Toolkit Pie Charts with Overlays

I’m not sure why I concentrate on Pie Charts. Maybe circles are just more attractive to me than bars or points :)

For the January 2009 Expression Newsletter (which will be out in a week or two), I put together a rather deep article on styling pie charts in the Silverlight Toolkit. At the end, I mentioned to check here for info on overlays.

One effect you can achieve with an overlay, is a glassy effect:

image 

Now, overlays aren’t something built into the control, you have to do a little playing around with them to achieve this.

First, you’ll need to modify the control template. Basically you want to end up with a sandwich with the SeriesContainer in between the shadow and the glossy elements, all inside the PlotAreaContainer.

Here’s what it looks like (the specular is shown with the pie as a background because you wouldn’t be able to see it otherwise)

image

If you want your chart to be pretty color agnostic, or to support having multiple colors for the slices, you will want to change your soft glow to various opacities of white. However, you get a warmer glow if you use a color that is related to the base color of your chart. For example, here’s the same chart, but with a white glow:

image

As you can see, it really lacks that healthy glow :) That said, a template using only white for the highlights is far more flexible in terms of allowing you to color the various slices. Here’s the same white highlighting with some different slice colors:

image

The final appearance, while lacking a little life, definitely makes for a more usable pie chart. Due to the straight lines between the slices, it reminds me of one of those half-round glass paperweights that have a picture on the bottom.

Here’s what the style looks like (if anything is cut off to the right, just download the attached project, linked at the end of this post)

<Style x:Key="GlassPieChartPlotAreaStyle"
       TargetType="Grid">
    <Setter Property="Background"
            Value="Transparent" />
</Style>

<ControlTemplate x:Key="GlassPieChartTemplate"
                 TargetType="charting:Chart">
    
<Border Background="{TemplateBinding Background}"
        BorderThickness="{TemplateBinding BorderThickness}"
        Padding="10">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!-- Title -->
        <datavis:Title Style="{TemplateBinding TitleStyle}"
                       Content="{TemplateBinding Title}"
                       Grid.Row="0"/>


        <Grid Margin="0,15,0,15"
              Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <!-- Legend -->
            <datavis:Legend x:Name="Legend"
                            Style="{TemplateBinding LegendStyle}"
                            Grid.Column="1"
                            Title="{TemplateBinding LegendTitle}"
                            BorderBrush="{x:Null}"
                            Background="{x:Null}" />

            <!-- Chart Area -->
            <Grid x:Name="ChartArea"
                  Grid.Column="0"
                  Style="{TemplateBinding ChartAreaStyle}">


                    <!-- Plot Area -->
                    <Grid x:Name="PlotArea"
                          Style="{TemplateBinding PlotAreaStyle}"
                          >
                        
                        <!-- Underlay Area, for shadows etc -->

                        <local:SquareContainer VerticalAlignment="Center"
                                               HorizontalAlignment="Center"
                                               Margin="10 14 10 2"
                                               IsHitTestVisible="False"
                                               Visibility="Visible">
                            <Ellipse Stretch="Uniform"
                                     Margin="0">
                                <Ellipse.Fill>
                                    <RadialGradientBrush Center=".5,.5"
                                                         GradientOrigin=".5,.5">
                                        <GradientStop Offset="0"
                                                      Color="#CC000000" />
                                        <GradientStop Offset=".8"
                                                      Color="#CC000000" />
                                        <GradientStop Offset="1"
                                                      Color="#00000000" />
                                    </RadialGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                        </local:SquareContainer>

                        
                        <!-- Required containers -->
                        <Grid x:Name="GridLinesContainer" />
                        <Grid x:Name="SeriesContainer" Visibility="Visible" />
                        
                        <Border BorderBrush="{x:Null}"
                                BorderThickness="1" />

                        <!-- Overlays for highlights and similar go after this line -->


                        <!-- Primary Soft Glow -->
                        <local:SquareContainer VerticalAlignment="Center"
                                               HorizontalAlignment="Center"
                                               Margin="14"
                                               IsHitTestVisible="False"
                                               Visibility="Visible">
                            <Ellipse Stretch="Uniform" Margin="5">
                                <Ellipse.Fill>
                                    <RadialGradientBrush Center=".5,.4"
                                                         GradientOrigin=".3,.3">
                                        
                                        <!-- White Highlight -->
                                        
                                        <GradientStop Offset="0"
                                                      Color="#99ffffff" />
                                        <GradientStop Offset=".5"
                                                      Color="#66ffffff" />
                                        <GradientStop Offset="1"
                                                      Color="#00ffffff" />
                                        
                                        <!-- Ruby Colored Highlight -->
                                        
                                        <!--<GradientStop Offset="0"
                                                      Color="#CCf5398e" />
                                        <GradientStop Offset=".5"
                                                      Color="#99f5398e" />
                                        <GradientStop Offset="1"
                                                      Color="#00f5398e" />-->
                                    </RadialGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                        </local:SquareContainer>

                        
                        <!-- Glass Specular Highlight -->
                        <local:SquareContainer VerticalAlignment="Center"
                                               HorizontalAlignment="Center"
                                               Margin="14"
                                               IsHitTestVisible="False"
                                               Visibility="Visible">

                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="0.06*" />
                                    <RowDefinition Height="0.8*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.25*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="0.25*" />
                                </Grid.ColumnDefinitions>


                                <Ellipse Stretch="Fill"
                                         Grid.Row="1"
                                         Grid.Column="1"
                                         Opacity=".75"
                                         >
                                    <Ellipse.Fill>
                                        <RadialGradientBrush Center=".45,0"
                                                             GradientOrigin=".45,0"
                                                             RadiusX="2"
                                                             RadiusY="1">
                                            <GradientStop Offset="0.0"
                                                          Color="#EEFFFFFF" />
                                            <GradientStop Offset="1"
                                                          Color="#11FFFFFF" />
                                        </RadialGradientBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </Grid>
                            
                            
                        </local:SquareContainer>

                    </Grid>
                
            </Grid>

        </Grid>
    </Grid>
</Border>
</ControlTemplate>

 

Here’s a set of four charts. The default chart appearance is on the upper left. The remaining three all use the same template, but different StylePalettes.

image

Here’s the xaml for the four charts (without the control template)

<Grid Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <charting:Chart x:Name="DefaultPieChart"
                    Grid.Row="0"
                    Grid.Column="0"
                    >
        <charting:Chart.Series>
            <charting:PieSeries IndependentValueBinding="{Binding Label}"
                                DependentValueBinding="{Binding Value}">
            </charting:PieSeries>
        </charting:Chart.Series>
    </charting:Chart>



    <charting:Chart x:Name="MulticolorGlassPieChart"
                    Grid.Row="0"
                    Grid.Column="1"
                    Template="{StaticResource GlassPieChartTemplate}"
                    PlotAreaStyle="{StaticResource GlassPieChartPlotAreaStyle}">
        <charting:Chart.Series>
            <charting:PieSeries IndependentValueBinding="{Binding Label}"
                                DependentValueBinding="{Binding Value}">
                <charting:PieSeries.StylePalette>
                    <datavis:StylePalette>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="#FF57007f" />
                            <Setter Property="BorderBrush"
                                    Value="#FF2e0007" />
                        </Style>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="#FF00757f" />
                            <Setter Property="BorderBrush"
                                    Value="#FF2e0007" />
                        </Style>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="#FF007f0f" />
                            <Setter Property="BorderBrush"
                                    Value="#FF2e0007" />
                        </Style>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="#FF7f2100" />
                            <Setter Property="BorderBrush"
                                    Value="#FF2e0007" />
                        </Style>
                    </datavis:StylePalette>
                </charting:PieSeries.StylePalette>
            </charting:PieSeries>
        </charting:Chart.Series>
    </charting:Chart>



    <charting:Chart x:Name="RubyGlassPieChart"
                    Grid.Row="1"
                    Grid.Column="1"
                    Template="{StaticResource GlassPieChartTemplate}"
                    PlotAreaStyle="{StaticResource GlassPieChartPlotAreaStyle}">
        <charting:Chart.Series>
            <charting:PieSeries IndependentValueBinding="{Binding Label}"
                                DependentValueBinding="{Binding Value}">
                <charting:PieSeries.StylePalette>
                    <datavis:StylePalette>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="#FF7f0013" />
                            <Setter Property="BorderBrush"
                                    Value="#FF2e0007" />
                        </Style>
                    </datavis:StylePalette>
                </charting:PieSeries.StylePalette>
            </charting:PieSeries>
        </charting:Chart.Series>
    </charting:Chart>


    <charting:Chart x:Name="BrightMulticolorGlassPieChart"
                    Grid.Row="1"
                    Grid.Column="0"
                    Template="{StaticResource GlassPieChartTemplate}"
                    PlotAreaStyle="{StaticResource GlassPieChartPlotAreaStyle}">
        <charting:Chart.Series>
            <charting:PieSeries IndependentValueBinding="{Binding Label}"
                                DependentValueBinding="{Binding Value}">
                <charting:PieSeries.StylePalette>
                    <datavis:StylePalette>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="Orange" />
                            <Setter Property="BorderBrush"
                                    Value="White" />
                        </Style>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="Red" />
                            <Setter Property="BorderBrush"
                                    Value="White" />
                        </Style>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="Blue" />
                            <Setter Property="BorderBrush"
                                    Value="White" />
                        </Style>
                        <Style TargetType="charting:PieDataPoint">
                            <Setter Property="Background"
                                    Value="Green" />
                            <Setter Property="BorderBrush"
                                    Value="White" />
                        </Style>
                    </datavis:StylePalette>
                </charting:PieSeries.StylePalette>
            </charting:PieSeries>
        </charting:Chart.Series>
    </charting:Chart>


</Grid>

While I did these examples 100% in xaml in Visual Studio, using the techniques presented here and in previous posts, a talented designer with Blend will be able to create all sorts of interesting designs for the charts.

I hope that gives you some idea of what you can do with a control template, and specifically with something like the Pie Charts from the Silverlight Toolkit.

You can download the full project here.

  Add to Technorati Favorites
Posted: Friday, January 09, 2009 11:58 PM by Pete.Brown

Comments

Pete.Brown said:

Forgot to mention that a key part of these chart templates is the SquareContainer from my previous post: http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2009/01/09/Creating-a-Silverlight-Control-that-Stays-Square.aspx Pete
# January 10, 2009 1:18 AM

Shawn Burke said:

Awesome. Just awesome. Solid work Pete!
# January 10, 2009 1:54 AM

automotive crm software guy said:

Wow that's pretty awesome.
# January 10, 2009 8:56 AM

Chad Campbell said:

Freakin' sweet! Great post.
# January 10, 2009 8:58 AM

Christopher Steen said:

Link Listing - January 10, 2009
# January 10, 2009 9:34 AM

Christopher Steen said:

Code Camps First New Hampshire Code Camp on February 28th [Via: cbowen ] Link Collections Interesting...
# January 10, 2009 9:35 AM

Community Blogs said:

In this issue: Michael Washington, Pete Brown(2), Shawn Wildermuth(2), Jesse Liberty, Mike Snow, Terence
# January 10, 2009 9:06 PM

POKE 53280,0: Pete Brown's Blog said:

The Expression Newsletter is out. In the current issue , you’ll find my article about styling charts
# January 20, 2009 11:30 PM

Delay's Blog said:

It's been a couple of months since I shared my semi-comprehensive page of Charting resources on the web
# February 6, 2009 2:33 AM

Microsoft Weblogs said:

It's been a couple of months since I shared my semi-comprehensive page of Charting resources on the web
# February 6, 2009 7:09 AM

Felix Wang | Evangelizing the Next Web said:

Overviews ( 100 level ) Silverlight Toolkit Released – More controls! - Tim Heuer 's during the PDC keynote
# February 11, 2009 12:52 AM

DotNetClub Complutense Madrid said:

En el blog de Felix Wang , he encontrado este artículo donde se agrupan una colección de
# February 19, 2009 4:14 AM

geordie said:

very nice. when i load your project in Blend, it crashes. can this not be done in Blend?
# February 20, 2009 3:15 PM

Tushar said:

Thanks Pete. I had one question. When I use the chart as follows: I get my glassy effect and the regular colors. This is fine and what I want. Then I go on to try to customize the tooltip by extracting the template of a PieDataPoint out, and changing its tooltip, saving this template this way: This time, all the slices appear yellow. Nothing in the PieDataPointTemplate makes them yellow by themselves, I checked. Something is being overridden. I'll appreciate any help on this. Thanks a lot!
# March 2, 2009 12:25 PM

Daniel Vogt said:

How can I integrate the datavis namespace in order to make this tutorial functioning?
# April 11, 2009 4:34 AM

Pete.Brown said:

@Daniel

In your usercontrol/page defintion, add an xmlns for the System.Windows.Controls.DataVisualization.Toolkit dll. Make sure you have that referenced before you do it.

In vs2008, when you type xmlns:datavis=" it will give you intellisense to help you figure out the rest.

Pete

# April 11, 2009 2:42 PM

Delay's Blog said:

It's been a while since the March 09 release of the Silverlight Toolkit - and even longer since I last
# April 24, 2009 3:30 AM

Microsoft Weblogs said:

It's been a while since the March 09 release of the Silverlight Toolkit - and even longer since I last
# April 24, 2009 8:40 AM

Melinda Marsh said:

Does 'StylePalette' only work within the xaml page and not the app.xaml. Ideally if multiple charts were being used throughout an application and they all used the same styling, I'd like to put all styling info in app.xaml. Can this be done? I've tried but it never recognises 'StylePalette'. If I've misunderstood how this should be working please let me know.
# June 15, 2009 6:19 AM

Gareth said:

Can this type of chart be done in WPF for a desktop application?
# July 2, 2009 12:59 PM

Gareth Segree said:

Pete, I'd like charts like these in WPF. Is there anything like this for WPF?
# July 2, 2009 8:53 PM

Pete.Brown said:

@Gareth The chart source is on http://silverlight.codeplex.com I'm not sure that they have WPF versions out yet, but worst-case, you could use the existing source code and port to WPF. The port should be easy: http://blogs.msdn.com/delay/archive/2009/03/26/if-they-can-build-it-they-will-come-enabling-anyone-to-compile-wpf-charting-from-the-silverlight-charting-sources.aspx Pete
# July 2, 2009 11:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the text you see in the image:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS