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

Dynamic Y-Axis Label

3 Answers 128 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 20 Sep 2019, 08:28 PM

Hi,

I have a dashboard with 3 RadCartesianCharts (2 bar charts, 1 line chart) all based on date values for category bindings and numeric values for value bindings.

Both the date values and the value values can vary considerably depending on filters set, so I set up value converters to dynamically adjust the axis labels and tooltips accordingly.

I was successful with all the horizontal axis and tooltips, but not with the y-axis values.

My goal is to adjust the y-axis values based on the size of the values, i.e. thousands vs millions vs billions. I use the following code to properly format the tooltips:

    double measureValue = double.Parse(value.ToString());
            if (measureValue > 999999999 || measureValue < -999999999)
            {
                formatString = "0,,,.###B";
            }
            else if (measureValue > 999999 || measureValue < -999999)
            {
                formatString = "0,,.##M";
            }

3 Answers, 1 is accepted

Sort by
0
n/a
Top achievements
Rank 1
answered on 20 Sep 2019, 08:37 PM

...sorry about that. Hit the wrong key!

At any rate, I want to do something similar with the y-axis labels, so I used the following Xaml:

LabelFormat="{Binding Converter={StaticResource TrendsMeasureFormatConverter}}"

...but the value object passed in the IValueConverter's Convert method points to my viewmodel, not the specific label value that I wish to format. The viewmodel contains three sets of data, and I couldn't find any way in that method to identify which chart had invoked the converter (and therefore no way to know which dataset to use).

So, instead, I created three value converters, one for each chart. Knowing the chart calling the converter, I then used the maximum value for the underlying dataset to determine and return the correct value format.

This works but feels clunky. Is there another, more elegant way to tackle this?

Wayne

 

0
n/a
Top achievements
Rank 1
answered on 20 Sep 2019, 10:30 PM

Actually, I found a much better way to do this. Using the value converter for y-axis labels had one fatal flaw - it wouldn't update the axis when the data was refreshed. So, instead, I simply built the axis dynamically in the code-behind:

           string measureFormat = formatMeasure();

           LinearAxis calendarYearMeasureAxis = new LinearAxis();

            calendarYearMeasureAxis.ElementBrush = brush;
            calendarYearMeasureAxis.LabelFormat = measureFormat;
            this.calendarYearChart.VerticalAxis = calendarYearMeasureAxis;

...and then called this code on a refresh of the dataset, i.e. by listening to the associated property change.

This now does everything I want it to, but I would still like to hear from the experts on the preferred method of achieving this.

Wayne

0
Martin Ivanov
Telerik team
answered on 25 Sep 2019, 10:02 AM

Hello Wayne,

There are different ways to approach this case. One you already found. Another approach would be to define few string properties in your main view model and bind them to the LabelFormat of the corresponding axes. 

The approach which I personally prefer is using the LabelTemplate property of the axis. This way you can define a DataTemplate with the label visualization. The data context passed to the template is a string value corresponding to the original label.

I hope this helps.

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
ChartView
Asked by
n/a
Top achievements
Rank 1
Answers by
n/a
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or