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

Custom labels AxisX

1 Answer 64 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Mickael
Top achievements
Rank 1
Mickael asked on 23 May 2011, 02:22 PM
Hello, 

I have a problem, 
How we can use personal convertors on labels on AxisX?

For instance : 
data(int) :3600,7200,10800
label(string) :1h,2h,3h

We need to use integers in our datas but labels need to be custom strings.

Best regards,

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 26 May 2011, 09:49 AM
Hello Mickael,

You can set converter on the AxisX labels by retemplating the AxisLabel2D and plug it in like this:

<Style x:Key="myLabelStyle" TargetType="telerik:AxisLabel2D">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="telerik:AxisLabel2D">
                <telerik:LayoutTransformControl x:Name="PART_LayoutTransformControl"
                                             VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                             HorizontalAlignment="{TemplateBinding HorizontalAlignment}">
                    <telerik:LayoutTransformControl.Content>
                        <TextBlock Style="{TemplateBinding ItemLabelStyle}"
                             Text="{Binding Converter={StaticResource converter}}" />
                    </telerik:LayoutTransformControl.Content>
                    <telerik:LayoutTransformControl.LayoutTransform>
                        <RotateTransform x:Name="PART_RotateTransform" />
                    </telerik:LayoutTransformControl.LayoutTransform>
                </telerik:LayoutTransformControl>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

If you want to apply your converter to all Axis labels (both on X and Y axis) you can remove the Key from the style. This way it will turn into implicit style and it will be applied to all Axis labels. On the other hand setting your converter only to one of the axis requires some code. I will demonstrate how to apply the converter only on the X-axis.

First wire up to the Loaded event of the chartarea in your constructor like this:
RadChart.DefaultView.ChartArea.Loaded += ChartArea_Loaded;
Next in the loaded event handler you can find the visual container of the AxisLabels. For the X-Axis it is called HorizontalAxisLabels2D. When you have it you can find its children and apply the style declared above.
void ChartArea_Loaded(object sender, RoutedEventArgs e)
{
    ChartArea area = sender as ChartArea;
    HorizontalAxisLabels2D axisLabelsContainer = area.FindChildByType<HorizontalAxisLabels2D>();
    var axisLabels = axisLabelsContainer.ChildrenOfType<AxisLabel2D>();
    System.Windows.Style style = this.Resources["myLabelStyle"] as System.Windows.Style;
 
    foreach (var item in axisLabels)
    {
        item.Style = style;
    }
}

Notice the extension methods FindChildByType<T> and ChildrenOfType<T>. They are declared in the Telerik.Windows.Controls namespace, so you may have to add a using for it.

Hope this helps!

Best wishes,
Yavor Ivanov
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
Mickael
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or