This is a migrated thread and some comments may be shown as answers.

The colour of the legend and the Foreground in the ChartArea

5 Answers 198 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Max xu
Top achievements
Rank 1
Max xu asked on 03 Feb 2010, 02:50 AM
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.

5 Answers, 1 is accepted

Sort by
0
Sia
Telerik team
answered on 05 Feb 2010, 02:43 PM
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.
0
Grtjn
Top achievements
Rank 1
answered on 30 Mar 2010, 09:02 AM
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
0
Max xu
Top achievements
Rank 1
answered on 01 Apr 2010, 06:59 AM
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

0
Grtjn
Top achievements
Rank 1
answered on 01 Apr 2010, 07:53 AM
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
0
Max xu
Top achievements
Rank 1
answered on 01 Apr 2010, 10:08 AM
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)," 
Tags
Chart
Asked by
Max xu
Top achievements
Rank 1
Answers by
Sia
Telerik team
Grtjn
Top achievements
Rank 1
Max xu
Top achievements
Rank 1
Share this question
or