Special Axis Text Formatting

1 Answer 69 Views
Chart
Nico
Top achievements
Rank 1
Veteran
Iron
Nico asked on 10 Dec 2021, 10:19 AM

Hello!
How can I format the Axis texts of the Chart like it is shown in the image below?

So for the horizontal axis and vertical axis there should be only numbers, but we want to place a text at the bottom left corner.

Lance | Manager Technical Support
Telerik team
commented on 10 Dec 2021, 05:32 PM

Hello Nico,

That area you are calling out is not where the title of any axis will get rendered. Therefore, to get the result you want, you can just add a Label at the bottom left corner of the same parent container you put the chart into.

For example:

<Grid>
    <!-- Telerik Chart Goes Here, oriented to fill the parent Grid -->
    <telerikChart:RadCartesianChart HorizontalOptions="Fill" VerticalOptions="Fill" ...>

    <!-- Label Goes Here, oriented to bottom-left of the parent Grid-->
    <Label Text="kWh" HorizontalOptions="Start" VerticalOptions="End"/>
</Grid>

Regards,
Lance | Manager Technical Support
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

1 Answer, 1 is accepted

Sort by
0
Accepted
Nico
Top achievements
Rank 1
Veteran
Iron
answered on 14 Dec 2021, 05:28 PM | edited on 14 Dec 2021, 05:28 PM

Unfortunately, this is not working as expected because the formatting is very bad.

The solution seems quite simple, I just have to override the LabelFormatter of the vertical axis like this:


public class FirstValueOverrideLabelFormatter : ILabelFormatter
{
        private string overrideValue;
        private string lookupValue;

        public FirstValueOverrideLabelFormatter(string overrideValue, string lookupValue)
        {
            this.overrideValue = overrideValue;
            this.lookupValue = lookupValue;
        }

        public Type ValueType => typeof(double?);

        public object ConvertionContext => new object();

        public string FormatValue(object value)
        {
            var result = value.ToString();

            if (result == lookupValue)
            {
                return overrideValue;
            }

            return value.ToString();
        }
}
Lance | Manager Technical Support
Telerik team
commented on 14 Dec 2021, 05:53 PM

Hi Nico,

Yes, you can always replace any of the existing values in an axis using the Label Formatter. As long as you're okay with replacing that first value in the axis with a custom lookup, this is a good approach that won't add too much overhead to your rendering pass.

As to your comment about the formatting of the Label, that's because you probably didn't set the Label's Font family or size. There is some additional work if you want it to perfectly match.

Tags
Chart
Asked by
Nico
Top achievements
Rank 1
Veteran
Iron
Answers by
Nico
Top achievements
Rank 1
Veteran
Iron
Share this question
or