Skip to main content Skip to footer

Displaying Custom Labels on C1Chart3D Axes

This brief post details a solution to display custom string labels on a C1Chart3D axis as the title suggests. For a getting started tutorial on the C1Chart3D control, read my other blog post here. The key property to take advantage of is the AnnoTemplate property. Assign this property, on the axis or axes of your choice, to a DataTemplate that you define. It can be just a simple TextBlock. C1Chart3D XAML:


<c1:C1Chart3D Name="c1Chart3D1" Grid.Row="1">  
    <c1:C1Chart3D.Resources>  
        <local:AnnoLabelConverter x:Key="alc" />  
        <DataTemplate x:Key="AnnoLabelTemplate">  
            <TextBlock Text="{Binding Converter={StaticResource alc} }"/>  
        </DataTemplate>  
    </c1:C1Chart3D.Resources>  
    <c1:C1Chart3D.AxisY>  
        <c1:Axis3D  AnnoTemplate="{StaticResource AnnoLabelTemplate}"/>  
    </c1:C1Chart3D.AxisY>  
    <c1:C1Chart3D.Legend>  
        <c1:C1Chart3DLegend />  
    </c1:C1Chart3D.Legend>  
</c1:C1Chart3D>  

Next, we will define the AnnoLabelConverter class that will convert the numeric value of the axis to a string. This is where we can apply some logic to determine the string content. Add the following class to your code and build the project. Also make sure you add an "xmlns:local" namespace to the XAML page.


public class AnnoLabelConverter : IValueConverter  
{  
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    {  
        if (value != null)  
        {  
            int d;  
            if(int.TryParse(value.ToString(), out d))  
            {  
                if (d == 0)  
                    return "Low";  
                else if (d == 5)  
                    return "Medium";  
                else if (d == 10)  
                    return "High";  
                else  
                    return "";  
            }  
        }  
        return "";  
    }  

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    {  
        return null;  
    }  
}  

This value converter uses a pre-defined array of 3 strings and assigns them to specific axis values. Your solution may look very different as you can take advantage of better programming techniques in code-behind.

ComponentOne Product Manager Greg Lutz

Greg Lutz

comments powered by Disqus