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

Custom Control from RadChart problem

1 Answer 40 Views
Chart
This is a migrated thread and some comments may be shown as answers.
jason
Top achievements
Rank 1
jason asked on 20 Jun 2011, 08:36 PM
My company is using telerik radCharts in our enterprise application.  Because they will be used in many places I have made it a custom control (Pie in this case).  All worked well as long as I still did the SeriesMappings in the xaml but I would like to now move that to the control as well.  I have added two dependency objects to the control called XCategoryProperty and YValueProperty which tell me what field in the ItemsSource to use for displaying values on the axis.  Now when I run the application instead of getting the pie chart showing data I just get a thin line.  I was not sure if I need to build this series mapping somewhere besides the controls constructor.  Below is a look at my xaml and also the function I call in the control's constructor:

 private void BuildSeriesMapping()
        {
            SeriesMapping mapping = new SeriesMapping();
            
            if (ChartType == Controls.ChartType.Pie)
            {
                mapping.SeriesDefinition = new PieSeriesDefinition();
            }
            else
            {
                mapping.SeriesDefinition = new DoughnutSeriesDefinition();
            }
 
            ItemMappingCollection itemMappings = new ItemMappingCollection();
            ItemMapping map1 = new ItemMapping(XCategory, DataPointMember.XCategory);
            ItemMapping map2 = new ItemMapping(YValue, DataPointMember.YValue);
            ItemMapping map3 = new ItemMapping(XCategory, DataPointMember.LegendLabel);
 
           
            mapping.ItemMappings.Add(map1);
            mapping.ItemMappings.Add(map2);
            mapping.ItemMappings.Add(map3);
 
            this.SeriesMappings.Add(mapping);
 
        }

Here are the dependency property definitions:
   public static readonly DependencyProperty XCategoryProperty = DependencyProperty.Register("XCategory",typeof(string),typeof(PieChart),new PropertyMetadata("XVal"));
 
   public static readonly DependencyProperty YValueProperty = DependencyProperty.Register("YValue"typeof(string), typeof(PieChart), new PropertyMetadata("YValue"));
Here is the xaml:
        <pie:PieChart Palette="{Binding PaletteData}" x:Name="pieChart" ItemsSource="{Binding Data}" Grid.Row="0" LegendVisibility="{Binding LegendVisible}" XCategory="XVal" YValue="YVal" ChartType="Pie"/>
Any help would be greatly appreciated.  I have attached an image of what I get.

1 Answer, 1 is accepted

Sort by
0
Accepted
Tsvetie
Telerik team
answered on 21 Jun 2011, 03:10 PM
Hi jason,
You code looks correct. However, you have not taken into account, that the constructor of your control will be executed before the setters of the two properties are called. As a result, you enter incorrect strings for your ItemMappings. One simple way to fix this, is the update the SeriesMappings collection, when the value of one of your properties changes as well. For example:

public static readonly DependencyProperty YValueProperty = DependencyProperty.Register("YValue", typeof(string), typeof(CustomPieChart), new PropertyMetadata("YValue", OnYValuePropertyChanged));
 
private static void OnYValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    var customPieChart = sender as CustomPieChart;
    customPieChart.BuildSeriesMapping();
}
 
private void BuildSeriesMapping()
{
    this.SeriesMappings.Clear();
 
    SeriesMapping mapping = new SeriesMapping();
    mapping.SeriesDefinition = new PieSeriesDefinition();
 
    ItemMapping map1 = new ItemMapping(XCategory, DataPointMember.XCategory);
    ItemMapping map2 = new ItemMapping(YValue, DataPointMember.YValue);
    ItemMapping map3 = new ItemMapping(XCategory, DataPointMember.LegendLabel);
 
    mapping.ItemMappings.Add(map1);
    mapping.ItemMappings.Add(map2);
    mapping.ItemMappings.Add(map3);
 
    this.SeriesMappings.Add(mapping);
}


Regards,
Tsvetie
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Chart
Asked by
jason
Top achievements
Rank 1
Answers by
Tsvetie
Telerik team
Share this question
or