Change LegendLabel in Code behine

3 posts, 0 answers
  1. Abdulhameed
    Abdulhameed avatar
    27 posts
    Member since:
    Jul 2010

    Posted 10 Apr 2012 Link to this post

    I have a chart with three Series Mapping two lines, and one area chart, for the legend I am using auto generated items and setting the label on the series mapping. see the code below

    <telerik:RadChart HorizontalAlignment="Left" telerik:StyleManager.Theme="Metro"
                              Name="SeisLineChart" VerticalAlignment="Top"
                              ItemsSource="{Binding}"  Height="500" Width="700" Margin="0">
                <telerik:RadChart.DefaultView>
                    <telerik:ChartDefaultView ChartLegendPosition="Bottom">
                        <!--<telerik:ChartDefaultView.ChartTitle>
                            <telerik:ChartTitle FontSize="13" Padding="2" />
                        </telerik:ChartDefaultView.ChartTitle>-->
                        <telerik:ChartDefaultView.ChartLegend>
                            <telerik:ChartLegend x:Name="DailyProductionRefLegend" Header="" UseAutoGeneratedItems="True"  Padding="2,2,5,1" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
                        </telerik:ChartDefaultView.ChartLegend>
                        <telerik:ChartDefaultView.ChartArea>
                            <telerik:ChartArea Grid.Row="1" Grid.RowSpan="2" x:Name="chartArea" LegendName="DailyProductionRefLegend" >
                                <telerik:ChartArea.AxisY>
                                    <telerik:AxisY x:Name="yAxis" Title="Unit" AutoRange="True" DefaultLabelFormat="#VAL{###,###}"/>
                                </telerik:ChartArea.AxisY>
                                <telerik:ChartArea.AxisX>
                                    <telerik:AxisX x:Name="xAxis" Title="Weeks" AutoRange="True" LabelStep="4" IsDateTime="False"/>
                                </telerik:ChartArea.AxisX>
                                 
                            </telerik:ChartArea>
                        </telerik:ChartDefaultView.ChartArea>
                    </telerik:ChartDefaultView>
                </telerik:RadChart.DefaultView>
                <telerik:RadChart.SeriesMappings>
                    <telerik:SeriesMapping x:Name="FiveYearsRangeMapping" LegendLabel="5 Year Ranges">
                        <telerik:SeriesMapping.SeriesDefinition>
                            <telerik:RangeSeriesDefinition SeriesName="FiveYearsRange" ShowItemLabels="False" 
                                                          ShowItemToolTips="True"
                                                          ShowPointMarks="False"
                                                                 EmptyPointBehavior="Zero"/>
                        </telerik:SeriesMapping.SeriesDefinition>
                        <telerik:SeriesMapping.ItemMappings>
                            <telerik:ItemMapping DataPointMember="XCategory"  FieldName="Week" />
                            <telerik:ItemMapping DataPointMember="High" FieldName="HighValue" />
                            <telerik:ItemMapping DataPointMember="Low" FieldName="LowValue" />
                        </telerik:SeriesMapping.ItemMappings>
                    </telerik:SeriesMapping>
     
                    <telerik:SeriesMapping x:Name="LastYearMapping" LegendLabel="Last Year">
                        <telerik:SeriesMapping.SeriesDefinition>
                            <!--<telerik:SplineSeriesDefinition ShowItemLabels="False"  ShowItemToolTips="True" ItemToolTipFormat="#DATAITEM.Quantity{###,###,##0} #DATAITEM.UnitCode"/>-->
                            <telerik:LineSeriesDefinition SeriesName="LastYear" ShowItemLabels="False" 
                                                          ShowItemToolTips="True"
                                                          ShowPointMarks="False"
                                                                 EmptyPointBehavior="Gap"
                                                          />
     
                        </telerik:SeriesMapping.SeriesDefinition>
                        <!--<telerik:SeriesMapping.GroupingSettings>
                            <telerik:GroupingSettings ShouldCreateSeriesForLastGroup="True">
                                <telerik:GroupingSettings.GroupDescriptors>
                                    <telerik:ChartGroupDescriptor Member="Product"/>
                                </telerik:GroupingSettings.GroupDescriptors>
                            </telerik:GroupingSettings>
                        </telerik:SeriesMapping.GroupingSettings>-->
                        <telerik:SeriesMapping.ItemMappings>
                            <telerik:ItemMapping DataPointMember="XCategory"  FieldName="Week" />
                            <telerik:ItemMapping DataPointMember="YValue" FieldName="LastYearQ" />
                        </telerik:SeriesMapping.ItemMappings>
                    </telerik:SeriesMapping>
     
                    <telerik:SeriesMapping x:Name="CurrentYearMapping" LegendLabel="Current Year">
                        <telerik:SeriesMapping.SeriesDefinition>
                            <telerik:LineSeriesDefinition SeriesName="CurrentYear" ShowItemLabels="False" 
                                                          ShowItemToolTips="True"
                                                          ShowPointMarks="False"
                                                                 EmptyPointBehavior="Gap"/>
                        </telerik:SeriesMapping.SeriesDefinition>
                        <telerik:SeriesMapping.ItemMappings>
                            <telerik:ItemMapping DataPointMember="XCategory"  FieldName="Week" />
                            <telerik:ItemMapping DataPointMember="YValue" FieldName="CurrentYeatQ" />
                        </telerik:SeriesMapping.ItemMappings>
                    </telerik:SeriesMapping>
     
                     
                </telerik:RadChart.SeriesMappings>
     
            </telerik:RadChart>


    However when I tired to change the legend label from code behind it didn't work
    SeisLineChart.SeriesMappings[0].LegendLabel = "New Legend label";


    What is the best way yo change the legend label from code behind?
  2. Peshito
    Admin
    Peshito avatar
    497 posts

    Posted 12 Apr 2012 Link to this post

    Hello Abdulhameed,

    The way you are setting the LegendLabel in code behind is the correct one, however I was not able to reproduce the issue you have described. Please, find attached a sample project with a scenario similar to yours. If it does not help you solve your issue, could you send us a small runnable project isolating the problem, so we could provide you with an appropriate solution.

    Regards,
    Peshito
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Abdulhameed
    Abdulhameed avatar
    27 posts
    Member since:
    Jul 2010

    Posted 14 Apr 2012 Link to this post

    The attached project didn't load properly.

    However I figured it out, the below code worked fine
    ((DataSeriesCollection)SeisLineChart.DefaultView.ChartArea.DataSeries)[1].LegendLabel = "Last Year, 2011 ";
Back to Top