Legend in ASP.NET Core Chart Component
18 Nov 20188 minutes to read
Legend provides information about the series rendered in the chart.
Enable Legend
You can use legend for the chart by setting the visible property to true in legendSettings object.
Position and Alignment
By using the position property, you can position the legend at left, right, top or bottom of the chart. The legend is positioned at the bottom of the chart, by default.
Custom position helps you to position the legend anywhere in the chart using x, y coordinates.
Legend Reverse
You can reverse the order of the legend items by using the reverse property. By default, legend for the first series in the collection will be placed first.
Legend Alignment
You can align the legend as center, far or near to the chart using alignment property.
Customization
To change the legend icon shape, you can use legendShape property in the series. By default, legend icon shape is seriesType.
Legend Size
By default, legend takes 20% - 25% of the chart’s height horizontally, when it is placed on top or bottom position and 20% - 25% of the chart’s width vertically, when placed on left or right position of the chart. You can change this default legend size by using the width and height property of the legendSettings.
Legend Item Size
You can customize the size of the legend items by using the shapeHeight and shapeWidth property.
Paging for Legend
Paging will be enabled by default, when the legend items exceeds the legend bounds. You can view each legend items by navigating between the pages using navigation buttons.
Legend Text Wrap
When the legend text exceed the container, the text can be wrapped by using the textWrap property. End user can also wrap the legend text based on the maximumLabelWidth property.
Set the label color based on series color
You can set the legend label color based on series color by using chart’s loaded event.
<ejs-chart id="charts" title="Olympic Medals" loaded="onChartLoaded">
<e-chart-primaryxaxis valueType="Category"></e-chart-primaryxaxis>
<e-series-collection>
<e-series dataSource="ViewBag.dataSource" name="Gold" xName="country" width="2" opacity="1" yName="gold" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column"></e-series>
<e-series dataSource="ViewBag.dataSource" name="Silver" xName="country" width="2" opacity="1" yName="silver" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column"></e-series>
<e-series dataSource="ViewBag.dataSource" name="bronze" xName="country" width="2" opacity="1" yName="bronze" type="@Syncfusion.EJ2.Charts.ChartSeriesType.Column"></e-series>
</e-series-collection>
</ejs-chart>
<script type="text/javascript">
var colors = ['#00BDAE', '#404041', '#357CD2'];
function onChartLoaded(args) {
let chart = document.querySelector('.e-chart');
let legendTextCol = chart.querySelectorAll('[id*="chart_legend_text_"]');
for (let i = 0; i < legendTextCol.length; i++) {
//set the color to legend label
legendTextCol[i].setAttribute('fill', colors[i]);
}
}
</script>public IActionResult Index()
{
List<ColumnChartData> chartData = new List<ColumnChartData>
{
new ColumnChartData{ country= "USA", gold=50, silver=70, bronze=45 },
new ColumnChartData{ country="China", gold=40, silver= 60, bronze=55 },
new ColumnChartData{ country= "Japan", gold=70, silver= 60, bronze=50 },
new ColumnChartData{ country= "Australia", gold=60, silver= 56, bronze=40 },
new ColumnChartData{ country= "France", gold=50, silver= 45, bronze=35 },
new ColumnChartData{ country= "Germany", gold=40, silver=30, bronze=22 },
new ColumnChartData{ country= "Italy", gold=40, silver=35, bronze=37 },
new ColumnChartData{ country= "Sweden", gold=30, silver=25, bronze=27 }
};
ViewBag.dataSource = chartData;
return View();
}
public class ColumnChartData
{
public string country;
public double gold;
public double silver;
public double bronze;
}
Series Selection on Legend
By default, legend click enables to collapse the series visibility. On other hand, if you need to select a series through legend click, disable the toggleVisibility.
Enable Animation
You can customize the animation while clicking legend by setting enableAnimation as true or false using enableAnimation property in chart.
Collapsing Legend Item
By default, series name will be displayed as legend. To skip the legend for a particular series, you can give empty string to the series name.
Legend Title
You can set title for legend using Title property in LegendSettings. You can also customize the FontStyle, Size, FontWeight, Color, TextAlignment, FontFamily, Opacity and TextOverflow of legend title. TitlePosition is used to set the legend position in Top, Left and Right position. MaximumTitleWidth is used to set the width of the legend title. By default, it will be 100px.
Arrow Page Navigation
By default, the page number will be enabled while legend paging. Now, you can disable that page number and also you can get left and right arrows for page navigation. You have to set false value to EnablePages to get this support.
Legend Item Padding
The itemPadding property can be used to adjust the space between the legend items.
Legend layout
The Layout property in LegendSettings allows you to display the legend either horizontally or vertically. By default, the Layout is set to Auto. The MaximumColumns property in LegendSettings defines the maximum number of columns that can be displayed within the available space when using the auto layout. Additionally, enabling the FixedWidth property in LegendSettings ensures that all legend items are displayed with equal widths. The width of each item is determined by the maximum width among the legend items.
Legend template
Legend templates allow you to replace default legend icons and text with custom HTML for each series. This enables branded styles, richer content (icons, multi-line text, badges), improved readability, and localization.
You can customize the legend items by using the template property of legendSettings. Legend interactions (click to toggle series) remain unless ToggleVisibility is set to false. Templates work with all legend positions, alignments, and paging.