Example of Annotation in ASP.NET Core Charts Control
This example puts text and markup on a chart, placed by axis values, pixels or percent of the plot.
Best region
@Html.UiChart(new UiChartOptions
{
Id = "annotationChart",
Title = "Sales by region",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0", Maximum = 80000 },
Legend = new UiChartLegend { Visible = false },
Annotations = new List<UiChartAnnotation>
{
new UiChartAnnotation { Template = "#bestRegionBadge", CoordinateUnits = UiChartAnnotationUnits.Axis, X = "Luzon", Y = 66000 },
new UiChartAnnotation { Content = "Figures are in pesos", CoordinateUnits = UiChartAnnotationUnits.Pixel, X = 12, Y = 8, HorizontalAlignment = UiChartHorizontalAlignment.Near, VerticalAlignment = UiChartVerticalAlignment.Top },
new UiChartAnnotation { Content = "DRAFT", CoordinateUnits = UiChartAnnotationUnits.Percent, Region = UiChartAnnotationRegion.Series, X = "70%", Y = "20%" }
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q4", Name = "Q4", ShowDataLabel = true }
}
})
<template id="bestRegionBadge"><span style="background:#09af74;color:#fff;border-radius:10px;padding:2px 10px;font-weight:600">Best region</span></template>
@Html.UiChart(new UiChartOptions
{
Id = "annotationChart",
Title = "Sales by region",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
PrimaryYAxis = new UiChartAxis { LabelFormat = "N0", Maximum = 80000 },
Legend = new UiChartLegend { Visible = false },
Annotations = new List<UiChartAnnotation>
{
new UiChartAnnotation { Template = "#bestRegionBadge", CoordinateUnits = UiChartAnnotationUnits.Axis, X = "Luzon", Y = 66000 },
new UiChartAnnotation { Content = "Figures are in pesos", CoordinateUnits = UiChartAnnotationUnits.Pixel, X = 12, Y = 8, HorizontalAlignment = UiChartHorizontalAlignment.Near, VerticalAlignment = UiChartVerticalAlignment.Top },
new UiChartAnnotation { Content = "DRAFT", CoordinateUnits = UiChartAnnotationUnits.Percent, Region = UiChartAnnotationRegion.Series, X = "70%", Y = "20%" }
},
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q4", Name = "Q4", ShowDataLabel = true }
}
})
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ChartController : Controller
{
// The page is Views/Chart/Annotation.cshtml; it draws what ViewData holds.
public ActionResult Annotation()
{
ViewData["ChartData"] = RegionSalesData();
return View();
}
/// <summary>The sales of one region per quarter: the rows of the Bar, Stacked Column and Bar Gradient samples.</summary>
public class RegionData
{
public string Region { get; set; }
public double Q1 { get; set; }
public double Q2 { get; set; }
public double Q3 { get; set; }
public double Q4 { get; set; }
}
private static List<RegionData> RegionSalesData()
{
return new List<RegionData>
{
new RegionData { Region = "Luzon", Q1 = 42000, Q2 = 48500, Q3 = 51200, Q4 = 60300 },
new RegionData { Region = "Visayas", Q1 = 27500, Q2 = 29800, Q3 = 33100, Q4 = 36900 },
new RegionData { Region = "Mindanao", Q1 = 21300, Q2 = 24100, Q3 = 23800, Q4 = 28700 },
new RegionData { Region = "Metro Manila", Q1 = 55800, Q2 = 58200, Q3 = 61900, Q4 = 70400 }
};
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Annotations put text or markup on the chart. CoordinateUnits says what X and Y mean: Axis values (the note over Luzon), Pixel (top left) or Percent (the watermark in the middle of the plot). Template points to markup in the page.