Search results

Legend in ASP.NET MVC Stock Chart control

24 Sep 2021 / 2 minutes to read

Legend provides information about the series rendered in the Stock Chart. Legend can be added to a Stock Chart by enabling the visible option in the legendSettings.

Position and Alignment

By using the position property, legend can be placed at Left, Right, Top, Bottom or Custom of the Stock Chart. The legend is positioned at the bottom of the Stock Chart, by default.

razor
position.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Name("China").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true).Position("Top"))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Custom position is used to position the legend anywhere in the Stock Chart using x, y coordinates.

razor
custom.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Name("China").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true).Position("Custom").location(location => location.x(200).y(20)))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Legend Alignment

The legend can be align as Center, Far or Near to the Stock Chart using alignment property.

razor
alignment.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Name("China").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true).Position("Bottom").Alignment("Near"))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Customization

To change the legend icon shape, legendShape property in the series can be used. By default legend icon shape is seriesType.

razor
customization.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Name("China").legendShape("Pentagon").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Legend Size

By default, legend takes 20% - 25% of the Stock Chart’s height horizontally, when it is placed on top or bottom position and 20% - 25% of the width vertically, while placing on left or right position of the Stock Chart. The default legend size can be changed by using the width and height property of the legendSettings.

razor
size.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Name("China").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true).width(500).height(50).border(border => border.width(1).color("pink")))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Legend Item Size

The size of the legend items can customized by using the shapeHeight and shapeWidth property.

razor
item-size.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Name("China").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true).shapeHeight(15).shapeWidth(15))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Collapsing Legend Item

By default, series name will be displayed as legend. To skip the legend for a particular series, empty string to the series name can be given.

razor
collapsing.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Legend Title

The title for legend can be set using title property in legendSettings. 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.

razor
title.cs
Copied to clipboard
<script src="~/datasource.js"></script>

@(Html.EJS().StockChart("container").Load("load").Title("Unemployment Rates 1975-2010")
        .PrimaryXAxis(xaxis => xaxis.ValueType(Syncfusion.EJ2.Charts.ValueType.DateTime))
            .Series(sr =>
            {
                sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Name("China").Add();
            })
            .Tooltip(tooltip => tooltip.Enable(true))
        .LegendSettings(legend => legend.Visible(true).title("Countries").titlePosition("Top"))
        .Render())

<script>
    var data = window.chartData;
    var load = function (args) {
        args.stockChart.indicatorType = [];
        args.stockChart.trendlineType = [];
    }
</script>
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}