Skip to content

Commit eb5e2bf

Browse files
docs(chart): format percentage KB
1 parent 688e4f5 commit eb5e2bf

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: How to format the percent in a label for a pie or donut chart
3+
description: How to format the percent in a label for a pie or donut chart
4+
type: how-to
5+
page_title: How to format the percent in a label for a pie or donut chart
6+
slug: chart-format-percent
7+
position:
8+
tags:
9+
ticketid: 1419362
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>Charts for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
When you use templates to customize the appearance of the labels, you may need to implement some application logic there or to implement complex formatting of the numbers.
26+
27+
This article shows how to format the percent in a label for a pie or donut chart to have a desired number of decimals and to be a number between 0 and 100, instead of the default number between 0 and 1 that has many decimal places:
28+
29+
![](images/pie-chart-formatted-percent.png)
30+
31+
## Solution
32+
To customize the percentage display, you need to
33+
34+
1. Use a custom [template]({%slug components/chart/label-template-format%}#templates) to show the `percentage` field.
35+
36+
2. Implement the desired rounding/formatting function in a JavaScript file (in the example below, we will call it `template-helpers.js` and it resides in the `wwwroot` folder).
37+
38+
3. Reference that file in your root component (`_Host.cshtml` for a server-side app, or `index.html` for a client-side app).
39+
40+
4. Call the custom formatting function from the template and pass the needed arguments to it. It must return the string you want shown in the template.
41+
42+
````Razor
43+
@using Telerik.Blazor
44+
@using Telerik.Blazor.Components.Chart
45+
46+
<TelerikChart>
47+
<TelerikChartSeriesItems>
48+
<TelerikChartSeries Type="ChartSeriesType.Pie" Data="@pieData"
49+
Field="@nameof(MyPieChartModel.SegmentValue)" CategoryField="@nameof(MyPieChartModel.SegmentName)">
50+
<TelerikChartSeriesLabels Visible="true" Template="@segmentTemplate"></TelerikChartSeriesLabels>
51+
</TelerikChartSeries>
52+
</TelerikChartSeriesItems>
53+
54+
<TelerikChartTitle Text="Revenue per product"></TelerikChartTitle>
55+
56+
<TelerikChartLegend Position="ChartLegendPosition.Right">
57+
</TelerikChartLegend>
58+
</TelerikChart>
59+
60+
@code {
61+
//this is where we call our custom rounding function
62+
string segmentTemplate = "#=value#\n#= round(percentage * 100, 1)#%";
63+
64+
//below is just some data to feed the display
65+
public class MyPieChartModel
66+
{
67+
public string SegmentName { get; set; }
68+
public double SegmentValue { get; set; }
69+
}
70+
71+
public List<MyPieChartModel>
72+
pieData = new List<MyPieChartModel> {
73+
new MyPieChartModel {
74+
SegmentName = "Product 1",
75+
SegmentValue = 1
76+
},
77+
new MyPieChartModel {
78+
SegmentName = "Product 2",
79+
SegmentValue = 3
80+
},
81+
new MyPieChartModel {
82+
SegmentName = "Product 3",
83+
SegmentValue = 5
84+
}
85+
};
86+
}
87+
````
88+
````JavaScript
89+
//From https://www.jacklmoore.com/notes/rounding-in-javascript/
90+
function round(value, decimals) {
91+
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
92+
}
93+
````
94+
````Index
95+
<head>
96+
<!-- there may be other content here -->
97+
98+
<script src="~/template-helpers.js"></script>
99+
100+
<!-- there may be other content here -->
101+
</head>
102+
````
103+
12 KB
Loading

0 commit comments

Comments
 (0)