Example of Click to add a point in ASP.NET MVC Charts Control
This Scatter Chart example adds a point at the place of the plot that is clicked.
Points: 5
@Html.UiChart(new UiChartOptions
{
Id = "clickAddChart",
Title = "Click the plot",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Animation = false,
OnPlotClick = "addPointAt",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Minimum = 0, Maximum = 100 },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 100 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Scatter, XName = "X", YName = "Y", Name = "Points", Marker = new UiChartMarker { Visible = true, Width = 12, Height = 12 } }
}
})
@using System.Linq
<p class="small text-muted">Points: <span id="pointCount">5</span></p>
<script>
window.addPointAt = function (args, chartId) {
var chart = Lumen.get(chartId);
var rows = chart.getData().slice();
rows.push({ X: Math.round(args.x * 10) / 10, Y: Math.round(args.y * 10) / 10 });
chart.setData(rows);
document.getElementById('pointCount').textContent = rows.length;
};
</script>
@Html.UiChart(new UiChartOptions
{
Id = "clickAddChart",
Title = "Click the plot",
Data = (IEnumerable<object>)ViewData["ChartData"],
Height = "380px",
Animation = false,
OnPlotClick = "addPointAt",
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Double, Minimum = 0, Maximum = 100 },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 100 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Scatter, XName = "X", YName = "Y", Name = "Points", Marker = new UiChartMarker { Visible = true, Width = 12, Height = 12 } }
}
})
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/ClickToAddPoint.cshtml; it draws what ViewData holds.
public ActionResult ClickToAddPoint()
{
ViewData["ChartData"] = new[] { new { X = 10.0, Y = 20.0 }, new { X = 30.0, Y = 55.0 }, new { X = 50.0, Y = 35.0 }, new { X = 70.0, Y = 80.0 }, new { X = 90.0, Y = 60.0 } }.ToList();
return View();
}
}
}
@* 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 anywhere in the plot to add a point there: OnPlotClick gives the X and Y value under the pointer, and the rows are replaced with setData.