|
| 1 | +--- |
| 2 | +title: How to Conditionally Change Axis Labels Units |
| 3 | +description: Learn how to format and convert the Y-Axis labels in Charts for Blazor to display inch or metric values based on user preference. |
| 4 | +type: how-to |
| 5 | +page_title: Customizing Y-Axis Labels in Charts for Blazor |
| 6 | +meta_title: Customizing Y-Axis Labels in Charts for Blazor |
| 7 | +slug: chart-kb-axis-labels-units |
| 8 | +tags: blazor, charts, axis, labels, template |
| 9 | +res_type: kb |
| 10 | +ticketid: 1690815 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>Charts for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +I want to format and convert the Y-Axis labels to display either metric (mm) or imperial (inches) values based on user preference. |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +Use the [`Template` parameter of the `ChartYAxisLabels`](slug:components/chart/label-template-format) to apply custom formatting and conversion logic. Below is an example implementation: |
| 31 | + |
| 32 | +````RAZOR |
| 33 | +<TelerikChart Height="380px" Width="100%"> |
| 34 | + <ChartTitle Text="Charge current vs. charge time" /> |
| 35 | + <ChartTooltip Visible="true" Shared="true" /> |
| 36 | + <ChartLegend Position="ChartLegendPosition.Bottom" /> |
| 37 | +
|
| 38 | + <ChartSeriesItems> |
| 39 | + <ChartSeries Type="ChartSeriesType.ScatterLine" |
| 40 | + Style="ChartSeriesStyle.Smooth" |
| 41 | + Name="Example Series" |
| 42 | + Color="black" |
| 43 | + Data="@Series1Data" |
| 44 | + XField="@nameof(ModelData.X)" |
| 45 | + YField="@nameof(ModelData.Y)"> |
| 46 | + <ChartSeriesMarkers Visible="true" Size="6" Type="ChartSeriesMarkersType.Circle" /> |
| 47 | + </ChartSeries> |
| 48 | + </ChartSeriesItems> |
| 49 | +
|
| 50 | + <ChartXAxes> |
| 51 | + <ChartXAxis Max="100" MajorUnit="10"> |
| 52 | + <ChartXAxisTitle Text="Time (minutes)" /> |
| 53 | + <ChartXAxisLabels Format="{0}m" /> |
| 54 | + </ChartXAxis> |
| 55 | + </ChartXAxes> |
| 56 | +
|
| 57 | + <ChartYAxes> |
| 58 | + <ChartYAxis> |
| 59 | + <ChartYAxisTitle Text="@(IsMetric ? "Charge (mm)" : "Charge (in)")" /> |
| 60 | + <ChartYAxisLabels Template="@YAxisLabelTemplate" /> |
| 61 | + </ChartYAxis> |
| 62 | + </ChartYAxes> |
| 63 | +</TelerikChart> |
| 64 | +
|
| 65 | +<TelerikButton OnClick="@ToggleUnits" Class="mt-4"> |
| 66 | + Toggle Units (Currently: @(IsMetric ? "Metric" : "Inches")) |
| 67 | +</TelerikButton> |
| 68 | +
|
| 69 | +<script suppress-error="BL9992"> |
| 70 | + function yAxisLabelMetric(context) { |
| 71 | + return context.value.toFixed(2) + " mm"; |
| 72 | + } |
| 73 | +
|
| 74 | + function yAxisLabelInches(context) { |
| 75 | + return (context.value / 25.4).toFixed(2) + " in"; |
| 76 | + } |
| 77 | +</script> |
| 78 | +
|
| 79 | +@code { |
| 80 | + private bool IsMetric = true; |
| 81 | + private string YAxisLabelTemplate => IsMetric ? "yAxisLabelMetric" : "yAxisLabelInches"; |
| 82 | +
|
| 83 | + private void ToggleUnits() |
| 84 | + { |
| 85 | + IsMetric = !IsMetric; |
| 86 | + } |
| 87 | +
|
| 88 | + public class ModelData |
| 89 | + { |
| 90 | + public int X { get; set; } |
| 91 | + public int Y { get; set; } |
| 92 | + } |
| 93 | +
|
| 94 | + public List<ModelData> Series1Data = new() |
| 95 | + { |
| 96 | + new ModelData() { X = 10, Y = 10 }, |
| 97 | + new ModelData() { X = 15, Y = 20 }, |
| 98 | + new ModelData() { X = 20, Y = 25 }, |
| 99 | + new ModelData() { X = 32, Y = 40 }, |
| 100 | + new ModelData() { X = 43, Y = 50 }, |
| 101 | + new ModelData() { X = 55, Y = 60 }, |
| 102 | + new ModelData() { X = 60, Y = 70 }, |
| 103 | + new ModelData() { X = 70, Y = 80 }, |
| 104 | + new ModelData() { X = 90, Y = 100 }, |
| 105 | + }; |
| 106 | +} |
| 107 | +```` |
| 108 | + |
| 109 | +## See Also |
| 110 | + |
| 111 | +* [Chart Overview](slug:components/chart/overview) |
| 112 | +* [ChartYAxisLabels Documentation](slug:components/chart/label-template-format) |
0 commit comments