Labels in ASP.NET MVC Sankey Chart component

23 Mar 202617 minutes to read

Labels display descriptive text associated with nodes in the Sankey Chart, making the diagram more understandable and interpretable. The Sankey Chart provides comprehensive label customization options including visibility control, font styling, individual label configuration, and dynamic rendering events.

This guide covers label appearance configuration, visibility control, font styling, and advanced label customization.

Label Settings Properties

The LabelSettings property provides options to control label appearance, text styling, and visibility. These properties apply globally to all node labels.

Label Configuration Properties

Property Type Default Description
visible boolean true Shows or hides all node labels.
fontSize string | number ‘12px’ Font size of the labels.
color string ’’ Text color of the labels.
fontFamily string null Font family for the label text.
fontWeight string ‘400’ Font weight (e.g., ‘400’ for normal, ‘700’ for bold).
fontStyle string ‘normal’ Font style (e.g., ‘normal’ or ‘italic’).
padding number 10 Space around the label text.

Configure global label styling for all nodes by setting properties like font size, color, font family, and font weight

Here is an example of customizing label appearance:

@Html.EJS().Sankey("sankey-container")
    .Width("90%")
    .Height("450px")
    .Tooltip(tooltip => tooltip.Enable(true))
    .LegendSettings(legend => legend.Visible(true))
    .LabelSettings(label => label
        .Visible(true)
        .FontFamily("Segoe UI")
        .FontStyle("Normal")
        .FontWeight("bold")
        .FontSize("14px")
        .Color("#333")
    )
    .Nodes(ViewBag.SankeyNodes)
    .Links(ViewBag.SankeyLinks)
    .Render()
public ActionResult Index()
{
    List<SankeyNode> nodes = new List<SankeyNode>
    {
        new SankeyNode { Id = "Agricultural Waste" },
        new SankeyNode { Id = "Biomass Residues" },
        new SankeyNode { Id = "Bio-conversion" },
        new SankeyNode { Id = "Liquid Biofuel" },
        new SankeyNode { Id = "Electricity" },
        new SankeyNode { Id = "Heat" }
    };

    List<SankeyLink> links = new List<SankeyLink>
    {
        new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
        new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
    };

    ViewBag.SankeyNodes = nodes;
    ViewBag.SankeyLinks = links;

    return View();
}

Hiding Labels

Control label visibility using the Visible property in LabelSettings. Set it to false to hide all node labels, which can be useful for creating cleaner visualizations when labels take up too much space:

@Html.EJS().Sankey("sankey-container")
    .Width("90%")
    .Height("450px")
    .Tooltip(tooltip => tooltip.Enable(true))
    .LegendSettings(legend => legend.Visible(true))
    .LabelSettings(label => label.Visible(false))
    .Nodes(ViewBag.SankeyNodes)
    .Links(ViewBag.SankeyLinks)
    .Render()
public ActionResult Index()
{
    List<SankeyNode> nodes = new List<SankeyNode>
    {
        new SankeyNode { Id = "Agricultural Waste" },
        new SankeyNode { Id = "Biomass Residues" },
        new SankeyNode { Id = "Bio-conversion" },
        new SankeyNode { Id = "Liquid Biofuel" },
        new SankeyNode { Id = "Electricity" },
        new SankeyNode { Id = "Heat" }
    };

    List<SankeyLink> links = new List<SankeyLink>
    {
        new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
        new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
    };

    ViewBag.SankeyNodes = nodes;
    ViewBag.SankeyLinks = links;

    return View();
}

Font Styling

Apply custom font styling to all labels using properties such as:

  • FontSize: Adjust text size (e.g., ‘12px’, ‘14px’)
  • FontFamily: Specify font family (e.g., ‘Arial’, ‘Times New Roman’)
  • FontWeight: Control text thickness (‘400’ = normal, ‘700’ = bold)
  • FontStyle: Apply text styling (‘normal’ or ‘italic’)
  • Color: Set text color (hex or color names)
@Html.EJS().Sankey("sankey-container")
    .Width("90%")
    .Height("450px")
    .Tooltip(tooltip => tooltip.Enable(true))
    .LegendSettings(legend => legend.Visible(true))
    .LabelSettings(label => label
        .Visible(true)
        .FontFamily("Times New Roman")
        .FontStyle("Italic")
        .FontWeight("bold")
        .FontSize("16px")
        .Color("#C00")
    )
    .Nodes(ViewBag.SankeyNodes)
    .Links(ViewBag.SankeyLinks)
    .Render()
public ActionResult Index()
{
    List<SankeyNode> nodes = new List<SankeyNode>
    {
        new SankeyNode { Id = "Agricultural Waste" },
        new SankeyNode { Id = "Biomass Residues" },
        new SankeyNode { Id = "Bio-conversion" },
        new SankeyNode { Id = "Liquid Biofuel" },
        new SankeyNode { Id = "Electricity" },
        new SankeyNode { Id = "Heat" }
    };

    List<SankeyLink> links = new List<SankeyLink>
    {
        new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
        new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
    };

    ViewBag.SankeyNodes = nodes;
    ViewBag.SankeyLinks = links;

    return View();
}

Individual Node Labels

Customize the appearance of specific node labels by configuring the Label property on each node object. This allows you to override global label settings for specific nodes, enabling data-driven label customization:

@Html.EJS().Sankey("sankey-container")
    .Width("90%")
    .Height("450px")
    .Tooltip(tooltip => tooltip.Enable(true))
    .LegendSettings(legend => legend.Visible(true))
    .LabelSettings(label => label.Visible(true))
    .Nodes(ViewBag.SankeyNodes)
    .Links(ViewBag.SankeyLinks)
    .Render()
public ActionResult Index()
{
    List<SankeyNode> nodes = new List<SankeyNode>
    {
        new SankeyNode { Id = "Agricultural Waste", Label = new SankeyChartDataLabel { Text = "Agri Waste", Padding = 0 } },
        new SankeyNode { Id = "Biomass Residues", Label = new SankeyChartDataLabel { Text = "Biomass", Padding = 0 } },
        new SankeyNode { Id = "Bio-conversion", Label = new SankeyChartDataLabel { Text = "Bio", Padding = 0 } },
        new SankeyNode { Id = "Liquid Biofuel",Label = new SankeyChartDataLabel { Text = "Liquid", Padding = 0 } },
        new SankeyNode { Id = "Electricity", Label = new SankeyChartDataLabel { Text = "Electricity", Padding = 0 } },
        new SankeyNode { Id = "Heat", Label = new SankeyChartDataLabel { Text = "Heat", Padding = 0 } }
    };

    List<SankeyLink> links = new List<SankeyLink>
    {
        new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
        new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
    };

    ViewBag.SankeyNodes = nodes;
    ViewBag.SankeyLinks = links;

    return View();
}

Advanced Label Configuration

Dynamic Label Customization

Use the LabelRendering event to customize label text, styling, and appearance dynamically during the render process. This event is triggered for each label before rendering, allowing you to apply conditional formatting, modify text, or adjust styling based on data values:

@Html.EJS().Sankey("sankey-container")
    .Width("90%")
    .Height("450px")
    .Tooltip(tooltip => tooltip.Enable(true))
    .LegendSettings(legend => legend.Visible(true))
    .LabelSettings(label => label.Visible(true))
    .LabelRendering("onLabelRendering")
    .Nodes(ViewBag.SankeyNodes)
    .Links(ViewBag.SankeyLinks)
    .Render()

<script>
    function onLabelRendering(args) {
        if (args.text === 'Agricultural Waste 84.152') {
            args.labelStyle = {
                fontWeight: 'bold',
                color: '#FF6B6B',
                fontSize: '14px',
                fontFamily: 'Arial',
                fontStyle: 'normal'
            };
        }
    }
</script>
public ActionResult Index()
{
    List<SankeyNode> nodes = new List<SankeyNode>
    {
        new SankeyNode { Id = "Agricultural Waste" },
        new SankeyNode { Id = "Biomass Residues" },
        new SankeyNode { Id = "Bio-conversion" },
        new SankeyNode { Id = "Liquid Biofuel" },
        new SankeyNode { Id = "Electricity" },
        new SankeyNode { Id = "Heat" }
    };

    List<SankeyLink> links = new List<SankeyLink>
    {
        new SankeyLink { SourceId = "Agricultural Waste", TargetId = "Bio-conversion", Value = 84.152 },
        new SankeyLink { SourceId = "Biomass Residues", TargetId = "Bio-conversion", Value = 24.152 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Liquid Biofuel", Value = 10.597 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Electricity", Value = 36.862 },
        new SankeyLink { SourceId = "Bio-conversion", TargetId = "Heat", Value = 60.845 }
    };

    ViewBag.SankeyNodes = nodes;
    ViewBag.SankeyLinks = links;

    return View();
}