The colour of the legend and the Foreground in the ChartArea

6 posts, 0 answers
  1. Max xu
    Max xu avatar
    15 posts
    Member since:
    Oct 2009

    Posted 02 Feb 2010 Link to this post

    Excuse me,please.
    How can I change the colour of the legend and the Foreground in the ChartArea?
     
    private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)  
            {         
                RadChart1.DefaultSeriesDefinition = new PieSeriesDefinition();  
     
                int[] itm = new int[3];  
                itm[0] = 1;  
                itm[1] = 1;  
                itm[2] = 2;  
     
                StylesPalette styles = Helper.FindResource<StylesPalette>("RadialStyle");  
     
                ChartLegendItem ch = new ChartLegendItem() { Label = "Not Started"Foreground=new SolidColorBrush(Colors.Black), ItemStyle = styles[0] };  
                ChartLegendItem ch1 = new ChartLegendItem() { Label = "In Progress"Foreground = new SolidColorBrush(Colors.Black), ItemStyle = styles[1] };  
                ChartLegendItem ch2 = new ChartLegendItem() { Label = "Completed"Foreground = new SolidColorBrush(Colors.Black), ItemStyle = styles[2] };  
     
                ChartLegendItemCollection chC = new ChartLegendItemCollection();  
                chC.Add(ch);  
                chC.Add(ch1);  
                chC.Add(ch2);  
     
                RadChart1.DefaultView.ChartLegend.UseAutoGeneratedItems = false;  
     
                RadChart1.DefaultView.ChartLegend.Items.Add(chC[0]);  
                RadChart1.DefaultView.ChartLegend.Items.Add(chC[1]);  
                RadChart1.DefaultView.ChartLegend.Items.Add(chC[2]);  
                RadChart1.ItemsSource = itm;  
     
                RadChart1.DefaultView.ChartTitle.Content = "Number Of Questions";  
            } 

    I want to change the Foreground in the ChartArea: the white color "1","1","2" to black;
    Also I want to change the color of the legend on the right and the color in the ChartArea.

    Thank you in advance.
  2. Sia
    Admin
    Sia avatar
    755 posts

    Posted 05 Feb 2010 Link to this post

    Hello Max,

    To change the Foreground of the Pie Labels, you need to add the following Style in the UserControl Resources: 
    <UserControl.Resources>
        <Style TargetType="charting:SeriesItemLabel" x:Key="CustomSeriesItemLabelStyle">
            <Setter Property="Foreground" Value="Red" />
         </Style>
    </UserControl.Resources>
    where charting is as follows: 
    xmlns:charting="clr-namespace:Telerik.Windows.Controls.Charting;assembly=Telerik.Windows.Controls.Charting"

    In your code behind you need to add:
    RadChart1.DefaultView.ChartLegend.Foreground = new SolidColorBrush(Colors.Red);
    RadChart1.DefaultView.ChartTitle.Foreground = new SolidColorBrush(Colors.Red);
      
    RadChart1.DefaultSeriesDefinition.SeriesItemLabelStyle = Resources["CustomSeriesItemLabelStyle"] as Style;

    The first two change the ChartLegend Title Foreground and the ChartTitle Foreground. The last one applies your new style.

    I hope that the above information is helpful!

    Sincerely yours,
    Sia
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  3. Grtjn
    Grtjn avatar
    129 posts
    Member since:
    Feb 2010

    Posted 30 Mar 2010 Link to this post

    Hi Max xu,

    Can you post some of your code-behind where you actually add the style to yout legenditems?
    Because this is wheren I'm stuck...

    Kind regards,

    Gertjan
  4. Max xu
    Max xu avatar
    15 posts
    Member since:
    Oct 2009

    Posted 01 Apr 2010 Link to this post

    Hello Gertjan,
    Helper.cs:(not need to be changed,can be use directly)
    using System;  
    using System.Net;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Documents;  
    using System.Windows.Ink;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Animation;  
    using System.Windows.Shapes;  
    using System.IO;  
    using System.Windows.Markup;  
    using System.Windows.Resources;  
    using ResourceDictionary = System.Windows.ResourceDictionary;  
     
     
    namespace VenusPrototype  
    {  
        public static class Helper  
        {  
            private const string DefaultThemeUriFormat = "/Telerik.Windows.Controls.Charting;component/Themes/Generic.xaml";  
     
            public static TResourceType FindResource<TResourceType>(string resourceKey)  
            {  
                Uri resourcesUri = new Uri(DefaultThemeUriFormat, UriKind.Relative);  
                ResourceDictionary dictionary = LoadResourceDictionary(resourcesUri);  
                if (dictionary != null && dictionary.Contains(resourceKey))  
                    return (TResourceType)dictionary[resourceKey];  
     
                return default(TResourceType);  
            }  
     
            private static ResourceDictionary LoadResourceDictionary(Uri source)  
            {  
                StreamResourceInfo sri = null;  
                try  
                {  
                    sri = Application.GetResourceStream(source);  
                    if (sri == null || sri.Stream == null)  
                    {  
                        return null;  
                    }  
                }  
                catch  
                {  
                    return null;  
                }  
     
                using (StreamReader reader = new StreamReader(sri.Stream))  
                {  
                    string resourceString = reader.ReadToEnd();  
                    return XamlReader.Load(resourceString) as ResourceDictionary;  
                }  
            }  
     
        }  
    }  
     

    ProgressOfQuestionsAssigned.xaml.cs:
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Net;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Animation;  
    using System.Windows.Shapes;  
    using Telerik.Windows.Controls.Charting;  
    using Telerik.Windows.Controls;  
     
    namespace VenusPrototype.DrapAndDropPanel  
    {  
        public partial class ProgressOfQuestionsAssigned : UserControl  
        {  
            private static int completedNumber;  
            public static void CompletedNumber(int inputNumber){ completedNumber = inputNumber; }  
            private static int inProgressNumber;  
            public static void InProgressNumber(int inputNumber) { inProgressNumber = inputNumber; }  
            private static int notStartedNumber;  
            public static void NotStartedNumber(int inputNumber) { notStartedNumber = inputNumber; }  
     
            public ProgressOfQuestionsAssigned()  
            {  
                InitializeComponent();       
            }  
     
            private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)  
            {  
                // TODO: Add event handler implementation here.           
                RadChart1.DefaultSeriesDefinition = new PieSeriesDefinition();  
                RadChart1.DefaultSeriesDefinition.ShowItemToolTips = true;  
                  
                RadChart1.DefaultView.ChartLegend.Items.RemoveAll();  
     
                int[] itm = new int[3];  
                itm[0] = notStartedNumber;  
                itm[1] = inProgressNumber;  
                itm[2] = completedNumber;  
     
                StylesPalette styles = Helper.FindResource<StylesPalette>("RadialStyle");  
     
                ChartLegendItem ch = new ChartLegendItem() { Label = "Not Started"Foreground=new SolidColorBrush(Colors.Black), ItemStyle = styles[0] };  
                ChartLegendItem ch1 = new ChartLegendItem() { Label = "In Progress"Foreground = new SolidColorBrush(Colors.Black), ItemStyle = styles[1] };  
                ChartLegendItem ch2 = new ChartLegendItem() { Label = "Completed"Foreground = new SolidColorBrush(Colors.Black), ItemStyle = styles[2] };  
     
                ChartLegendItemCollection chC = new ChartLegendItemCollection();  
                chC.Add(ch);  
                chC.Add(ch1);  
                chC.Add(ch2);  
     
                RadChart1.DefaultView.ChartLegend.UseAutoGeneratedItems = false;  
     
                RadChart1.DefaultView.ChartLegend.Items.Add(chC[0]);  
                RadChart1.DefaultView.ChartLegend.Items.Add(chC[1]);  
                RadChart1.DefaultView.ChartLegend.Items.Add(chC[2]);  
                RadChart1.ItemsSource = itm;  
     
                RadChart1.DefaultView.ChartTitle.Content = "Number Of Questions";  
                RadChart1.DefaultView.ChartLegend.Background = new SolidColorBrush(Colors.White);  
                RadChart1.DefaultView.ChartLegend.Foreground = new SolidColorBrush(Colors.Black);  
            }  
     
        }  
    }  
     

    Hope that it can be helpful to you.

    Sincerely yours,
    Max Xu

  5. Grtjn
    Grtjn avatar
    129 posts
    Member since:
    Feb 2010

    Posted 01 Apr 2010 Link to this post

    Ok, thanks!

    And how did you change the color of your legenditems?
    Because the colors of my piechart should come from the database, and can't find how to set the colors of my legend...
    But i succeeded to change the colors of the pie. For this I used a style:
    <Style x:Key="CustomStyle" TargetType="Telerik_Windows_Controls_Charting:Doughnut">  
                    <Setter Property="Template" > 
                        <Setter.Value> 
                            <ControlTemplate TargetType="Telerik_Windows_Controls_Charting:Doughnut">  
                                <Canvas> 
                                    <Ellipse Clip="{TemplateBinding FigurePath}"   
                                             Width="{TemplateBinding ItemActualWidth}" 
                                             Height="{TemplateBinding ItemActualHeight}" 
                                             StrokeThickness="0" 
                                             Fill="{Binding DataItem.Kleur}" /> 
                                    <Path x:Name="PART_DefiningGeometry"   
                                          Data="{TemplateBinding FigurePath2}" 
                                          Fill="Transparent" 
                                          Style="{TemplateBinding ItemStyle}" /> 
                                    <Ellipse Clip="{TemplateBinding FigurePath3}"   
                                             Fill="{StaticResource DoughnutMaskBrush}"   
                                             Width="{TemplateBinding ItemActualWidth}" 
                                             Height="{TemplateBinding ItemActualHeight}"/>  
                                    <Canvas.RenderTransform> 
                                        <ScaleTransform x:Name="PART_AnimationTransform" ScaleX="0" ScaleY="0" /> 
                                    </Canvas.RenderTransform> 
                                </Canvas> 
                            </ControlTemplate> 
                        </Setter.Value> 
                    </Setter> 
                </Style> 
     You have any advice?

    Kind regards,

    Gertjan
  6. Max xu
    Max xu avatar
    15 posts
    Member since:
    Oct 2009

    Posted 01 Apr 2010 Link to this post

    ChartLegendItem ch = new ChartLegendItem() { Label = "Not Started"Background=new SolidColorBrush(Colors.Black), Foreground=new SolidColorBrush(Colors.Black), ItemStyle = styles[0] };  
                ChartLegendItem ch1 = new ChartLegendItem() { Label = "In Progress"Background = new SolidColorBrush(Colors.White), Foreground = new SolidColorBrush(Colors.Black), ItemStyle = styles[1] };  
                ChartLegendItem ch2 = new ChartLegendItem() { Label = "Completed"Background = new SolidColorBrush(Colors.Yellow), Foreground = new SolidColorBrush(Colors.Black), ItemStyle = styles[2] }; 

    Add this  " Background=new SolidColorBrush(Colors.Black)," 
Back to Top