Example of Events in ASP.NET MVC Charts Control
This example writes the events of a chart (clicks, loaded rows and a data error) to a log under it.
@Html.AntiForgeryToken()
@Html.UiChart(new UiChartOptions
{
Id = "eventsChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "300px",
OnPointClick = "ChartDemo.onPointClick",
OnLoaded = "ChartDemo.onLoaded",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q2", Name = "Q2" }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "eventsRenderChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "300px",
OnLegendRender = "legendRenderDemo",
OnTextRender = "textRenderDemo",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1", ShowDataLabel = true },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q2", Name = "Q2", ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "failingChart",
DataUrl = Url.Action("Failing", "ChartDemo"),
Height = "300px",
ErrorText = "This chart fails on purpose to show OnDataError.",
OnDataError = "ChartDemo.onDataError",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1" }
}
})
@Html.AntiForgeryToken()
@* Samples/_Events.cshtml *@
@Html.UiChart(new UiChartOptions
{
Id = "eventsChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "300px",
OnPointClick = "ChartDemo.onPointClick",
OnLoaded = "ChartDemo.onLoaded",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1" },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q2", Name = "Q2" }
}
})
<script>
// OnLegendRender words each legend entry; OnTextRender words each data label.
window.legendRenderDemo = function (args) { args.text = args.name + ' (quarter)'; };
window.textRenderDemo = function (args) { if (args.value >= 45000) { args.text = args.text + ' *'; } };
</script>
@Html.UiChart(new UiChartOptions
{
Id = "eventsRenderChart",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "300px",
OnLegendRender = "legendRenderDemo",
OnTextRender = "textRenderDemo",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1", Name = "Q1", ShowDataLabel = true },
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q2", Name = "Q2", ShowDataLabel = true }
}
})
@Html.Partial("Samples/_FailingChart")
@* Samples/_FailingChart.cshtml *@
@Html.UiChart(new UiChartOptions
{
Id = "failingChart",
DataUrl = Url.Action("Failing", "ChartDemo"),
Height = "300px",
ErrorText = "This chart fails on purpose to show OnDataError.",
OnDataError = "ChartDemo.onDataError",
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Region", YName = "Q1" }
}
})
@section below {
<h3>Event log</h3>
<ul id="eventLog" style="font-family:monospace;font-size:12px;max-height:200px;overflow-y:auto;border:1px solid var(--ui-border, #dadce3);padding:8px;"></ul>
}
@section scripts {
<script>
window.ChartDemo = (function () {
'use strict';
function log(message) {
var list = document.getElementById('eventLog');
if (!list) { return; }
var item = document.createElement('li');
item.textContent = new Date().toLocaleTimeString() + ' - ' + message;
list.insertBefore(item, list.firstChild);
}
return {
// args = { seriesName, seriesIndex, pointIndex, x, y, data, rows }
onPointClick: function (args, chartId) {
log(chartId + ': clicked ' + args.seriesName + ' / ' + args.x + ' = ' + args.y);
},
onLoaded: function (rows, chartId) {
log(chartId + ': loaded ' + rows.length + ' row(s)');
},
onDataError: function (message, chartId) {
log(chartId + ': data error - ' + message);
}
};
})();
</script>
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class ChartController : Controller
{
// The page is Views/Chart/Events.cshtml; it draws what ViewData holds.
public ActionResult Events()
{
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 }
};
}
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Failing()
{
throw new InvalidOperationException("Simulated failure for the Lumen.UI.Mvc chart demo.");
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Charts with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Click a column to raise OnPointClick. The second chart calls a failing URL to raise OnDataError; OnLoaded also writes to the event log.