Skip to content

Commit 738046f

Browse files
github-actions[bot]KB Bot
and
KB Bot
authored
Added new kb article htmlchart-customize-legend-dash-type (#600)
Co-authored-by: KB Bot <[email protected]>
1 parent 8843e0d commit 738046f

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: Customizing Legend Dash Type in RadHtmlChart
3+
description: Learn how to modify the legend item's dash type in RadHtmlChart to match the dash style of its corresponding series.
4+
type: how-to
5+
page_title: How to Customize Legend Dash Type in RadHtmlChart for ASP.NET AJAX
6+
slug: htmlchart-customize-legend-dash-type
7+
tags: radhtmlchart, asp.net ajax, legend, dash type, visualization
8+
res_type: kb
9+
ticketid: 1666132
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>RadHtmlChart for ASP.NET AJAX</td>
19+
</tr>
20+
<tr>
21+
<td>Version</td>
22+
<td>All</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
When using a RadHtmlChart with a line series displayed as a dashed line, the default behavior shows the legend item as a solid line. The goal is to make the legend item appear with a dash type matching its corresponding series.
30+
31+
This KB article also answers the following questions:
32+
- How can I change the appearance of legend items in RadHtmlChart?
33+
- Is it possible to apply custom styles to legend items based on the series style?
34+
- Can I use a custom visual to modify legend items in RadHtmlChart?
35+
36+
## Solution
37+
38+
To customize the dash type of a legend item in RadHtmlChart, use a custom visual function. This approach allows the legend item to reflect the same dash type as its corresponding series item.
39+
40+
1. Define the RadHtmlChart control with the necessary series and specify the `DashType` for each series.
41+
42+
2. Use the `Visual` property of the `Legend` > `Item` to assign a JavaScript function that customizes the appearance of the legend item.
43+
44+
3. Implement the custom visual function (`legendItemVisual`) to create a legend item with a dashed line matching the series dash type.
45+
46+
Here's a code snippet demonstrating this solution:
47+
48+
````ASP.NET
49+
<telerik:RadHtmlChart runat="server" ID="RadHtmlChart1" Height="400px">
50+
<Legend>
51+
<Appearance Position="Bottom" />
52+
<Item Visual="legendItemVisual" />
53+
</Legend>
54+
<PlotArea>
55+
<Series>
56+
<telerik:LineSeries Name="Actual">
57+
<LineAppearance DashType="Dash" />
58+
<SeriesItems>
59+
<telerik:CategorySeriesItem Y="190000" />
60+
<telerik:CategorySeriesItem Y="25000" />
61+
<telerik:CategorySeriesItem Y="175000" />
62+
</SeriesItems>
63+
</telerik:LineSeries>
64+
<!-- Include other series as needed -->
65+
</Series>
66+
</PlotArea>
67+
<ChartTitle Text="Product Sales for 2014">
68+
</ChartTitle>
69+
</telerik:RadHtmlChart>
70+
````
71+
72+
4. Define the `legendItemVisual` JavaScript function to adjust the legend's item appearance.
73+
74+
````JavaScript
75+
legendItemVisual = function (e) {
76+
var color = e.options.markers.background;
77+
var labelColor = e.options.labels.color ? e.options.labels.color : "#000";
78+
var rect = new kendo.geometry.Rect([0, 0], [200, 50]);
79+
var layout = new kendo.drawing.Layout(rect, {
80+
spacing: 5,
81+
alignItems: "center"
82+
});
83+
84+
var marker = new kendo.drawing.Path({
85+
stroke: {
86+
color: color,
87+
width: 3,
88+
dashType: e.series.dashType
89+
}
90+
}).moveTo(0, 0).lineTo(30, 0);
91+
92+
var label = new kendo.drawing.Text(e.series.name, [0, 0], {
93+
fill: {
94+
color: labelColor
95+
}
96+
});
97+
98+
layout.append(marker, label);
99+
layout.reflow();
100+
101+
return layout;
102+
};
103+
````
104+
105+
Adopting this approach allows for a consistent visual representation between the series and their corresponding legend items.
106+
107+
## See Also
108+
109+
- [Visual Template in RadHtmlChart Documentation](https://docs.telerik.com/devtools/aspnet-ajax/controls/htmlchart/functionality/visual-template#visual-template)
110+
- [RadHtmlChart Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/htmlchart/overview)

0 commit comments

Comments
 (0)