Skip to content

Commit a6dc1de

Browse files
authored
docs(chart): add label template info for numerical charts (#1743)
1 parent 4cbf777 commit a6dc1de

File tree

1 file changed

+123
-13
lines changed

1 file changed

+123
-13
lines changed

components/chart/labels-template-and-format.md

Lines changed: 123 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,23 @@ The Blazor Chart uses client-side rendering and the label templates are JavaScri
8282
The JavaScript function for each label template will receive an argument that exposes different properties, depending on the template type. The mechanism is similar to the `context` of Blazor `RenderFragment`s. The sections below list the available method argument properties:
8383

8484
* [Series labels](#series-label-template)
85-
* [Category axis labels](#category-axis-label-template)
86-
* [Value axis labels](#value-axis-label-template)
85+
* [Category axis labels](#category-axis-label-template) (for categorical Charts)
86+
* [X axis labels](#x-axis-label-template) (for numerical Charts)
87+
* [Value and Y axis labels](#value-and-y-axis-label-template)
8788
* [Legend item labels](#legend-item-label-template)
8889
* [How to add new lines to label templates](#new-line-in-the-label-template)
89-
* [Example for all types of label templates](#label-template-example)
90+
* [Example for categorical Charts](#label-template-in-categorical-charts)
91+
* [Example for numerical Charts](#label-template-in-numerical-charts)
9092

9193
### Series Label Template
9294

9395
The `Template` function of `ChartSeriesLabels` exposes the following fields in the template context:
9496

9597
* `category` - the category name. Available for Area, Bar, Column, Donut, Line, and Pie series.
96-
* `dataItem` - the original data item used to construct the point. Will be `null` if binding to array. Sample syntax: `context.dataItem.MyModelPropertyName`.
98+
* `dataItem` - the original data item used to construct the point. Will be `null` if binding to array. Sample syntax: `context.dataItem.MyPropertyName`.
9799
* `percentage` - the point value represented as a percentage value. Available only for Donut, Pie and 100% stacked charts.
98100
* `stackValue` - the cumulative point value on the stack. Available only for stackable series.
99-
* `value` - the point value. Can be a number or object containing each bound field.
101+
* `value` - the point value. Can be a number for categorical series or an object with `x` and `y` properties for numerical series.
100102

101103
<!--* `series` - the data series-->
102104
<!--* runningTotal - the sum of point values since the last "runningTotal" summary point. Available for waterfall series.
@@ -106,23 +108,40 @@ The `Template` function of `ChartSeriesLabels` exposes the following fields in t
106108

107109
The `Template` function of `CategoryAxisLabels` exposes the following fields in the template context:
108110

109-
* `value` - the category value
110-
* `format` - the default format of the label
111+
* `count` - the number of labels on the axis
112+
* `format` - the numeric or date format of the label
113+
* `index` - the order index of the label
114+
* `text` - the label string if no template is used
115+
* `value` - the category value as a string, number or JavaScript `Date` object
116+
117+
### X Axis Label Template
118+
119+
The `Template` function of `XAxisLabels` exposes the following fields in the template context:
120+
121+
* `count` - the number of labels on the axis
122+
* `format` - the numeric or date format of the label
123+
* `index` - the order index of the label
124+
* `text` - the label string if no template is used
125+
* `value` - the label as a number or JavaScript `Date` object
111126

112127
<!--* `dataItem` - the data item, in case a field has been specified. If the category does not have a corresponding item in the data then an empty object will be passed.-->
113128
<!--* culture - the default culture (if set) on the label-->
114129

115-
### Value Axis Label Template
130+
### Value and Y Axis Label Template
116131

117-
The `Template` function of `ValueAxisLabels` exposes the following fields in the template context:
132+
The `Template` function of `ValueAxisLabels` and `YAxisLabels` exposes the following fields in the template context:
118133

119-
* `value` - the label value
134+
* `count` - the number of labels on the axis
135+
* `format` - the default or specified format of the label
136+
* `index` - the order index of the label
137+
* `text` - the label string if no template is used
138+
* `value` - the numeric representation of the label
120139

121140
### Legend Item Label Template
122141

123142
The `Template` function of `ChartLegendLabels` exposes the following fields in the template context:
124143

125-
* `text` - the text the legend item
144+
* `text` - the text of the legend item
126145
* `series` - the data series object
127146
* `value` - the data point value. Available only for Donut and Pie charts.
128147
* `percentage` - the data point value as a number between 0 and 1. Available only for Donut, Pie and 100% stacked charts.
@@ -139,9 +158,9 @@ function chartLabelFunction(context) {
139158
}
140159
````
141160

142-
### Label Template Example
161+
### Label Template in Categorical Charts
143162

144-
>caption Using Chart label templates for series, axes and legend
163+
>caption Using categorical Chart label templates for series, axes and legend
145164
146165
````CSHTML
147166
<TelerikChart>
@@ -288,6 +307,97 @@ function chartLabelFunction(context) {
288307
}
289308
````
290309

310+
### Label Template in Numerical Charts
311+
312+
>caption Using numerical Chart label templates for series, axes and legend
313+
314+
````CSHTML
315+
<TelerikChart>
316+
<ChartTitle Text="Signal Level vs. Errors per Minute"></ChartTitle>
317+
318+
<ChartSeriesItems>
319+
<ChartSeries Type="ChartSeriesType.Scatter"
320+
Data="@Series1Data"
321+
Name="APSK modulation"
322+
XField="@nameof(ChartModel.Signal)"
323+
YField="@nameof(ChartModel.Errors)"
324+
Color="red">
325+
<ChartSeriesLabels Visible="true"
326+
Template="chartSeriesLabelTemplate" />
327+
</ChartSeries>
328+
329+
<ChartSeries Type="ChartSeriesType.Scatter"
330+
Data="@Series2Data"
331+
Name="QAM modulation"
332+
XField="@nameof(ChartModel.Signal)"
333+
YField="@nameof(ChartModel.Errors)"
334+
Color="blue">
335+
<ChartSeriesLabels Visible="true"
336+
Template="chartSeriesLabelTemplate" />
337+
</ChartSeries>
338+
</ChartSeriesItems>
339+
340+
<ChartXAxes>
341+
<ChartXAxis Min="-95" Max="-70" AxisCrossingValue="@(new object[] { -95 })">
342+
<ChartXAxisLabels Template="chartXAxisLabelTemplate" />
343+
</ChartXAxis>
344+
</ChartXAxes>
345+
346+
<ChartYAxes>
347+
<ChartYAxis Max="25" MajorUnit="5">
348+
<ChartYAxisLabels Template="chartYAxisLabelTemplate" />
349+
</ChartYAxis>
350+
</ChartYAxes>
351+
352+
<ChartLegend Position="ChartLegendPosition.Top">
353+
<ChartLegendLabels Template="chartLegendItemLabelTemplate"></ChartLegendLabels>
354+
</ChartLegend>
355+
</TelerikChart>
356+
357+
<!-- Move JavaScript code to a separate JS file in production -->
358+
<script suppress-error="BL9992">
359+
function chartSeriesLabelTemplate(context) {
360+
return "X Value: " + context.value.x + "\n" +
361+
"Y Value: " + context.value.y + "\n" +
362+
"Extra info: " + context.dataItem.ExtraData;
363+
}
364+
365+
function chartXAxisLabelTemplate(context) {
366+
return context.value + " dBm " +
367+
"(" + (context.index + 1) + "/" + context.count +")";
368+
}
369+
370+
function chartYAxisLabelTemplate(context) {
371+
return context.value + " " +
372+
"(" + (context.index + 1) + "/" + context.count +")";
373+
}
374+
375+
function chartLegendItemLabelTemplate(context) {
376+
return context.text + " (" + context.series.color + ")";
377+
}
378+
</script>
379+
380+
@code {
381+
private List<ChartModel> Series1Data { get; set; } = new List<ChartModel>() {
382+
new ChartModel() { Signal = -92, Errors = 15, ExtraData = "foo 1" },
383+
new ChartModel() { Signal = -84, Errors = 7, ExtraData = "bar 1" },
384+
new ChartModel() { Signal = -75, Errors = 1, ExtraData = "baz 1" }
385+
};
386+
387+
private List<ChartModel> Series2Data { get; set; } = new List<ChartModel>() {
388+
new ChartModel() { Signal = -88, Errors = 18, ExtraData = "foo 2" },
389+
new ChartModel() { Signal = -78, Errors = 12, ExtraData = "bar 2" },
390+
};
391+
392+
public class ChartModel
393+
{
394+
public decimal Signal { get; set; }
395+
public int Errors { get; set; }
396+
public string ExtraData { get; set; } = string.Empty;
397+
}
398+
}
399+
````
400+
291401
### Hide Label Conditionally
292402

293403
In some cases, you want the series to have labels, but certain data points must not have a label. For example, in a [stacked series]({%slug components/chart/stack%}) where a certain value is `0`.

0 commit comments

Comments
 (0)