Example of Data Editing in ASP.NET Core Charts Control
This sample shows the behavior of the data editing in the chart. Drag and drop the points to change the data values dynamically.
@* The chart's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiChart(new UiChartOptions
{
Id = "dataEditingChart",
Title = "Electricity - Production",
Data = (IEnumerable<object>)ViewData["ConsumerDetails"],
Tooltip = new UiChartTooltip { Enable = false },
Legend = new UiChartLegend { Visible = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false, LabelFormat = "yyyy", IntervalType = UiChartIntervalType.Years },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 100, Interval = 20, LabelFormat = "{value}B", Title = "Production(Billion in kWh)" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, Width = 2, XName = "Period", YName = "ProductA_Sales", Name = "Renewable", Opacity = 1, AllowDataEditing = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7 } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "ProductB_Sales", Name = "Non-Renewable", Opacity = 1, AllowDataEditing = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, IsFilled = true } }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "dataEditingChart",
Title = "Electricity - Production",
Data = (IEnumerable<object>)ViewData["ConsumerDetails"],
Tooltip = new UiChartTooltip { Enable = false },
Legend = new UiChartLegend { Visible = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, ShowGridLines = false, LabelFormat = "yyyy", IntervalType = UiChartIntervalType.Years },
PrimaryYAxis = new UiChartAxis { Minimum = 0, Maximum = 100, Interval = 20, LabelFormat = "{value}B", Title = "Production(Billion in kWh)" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, Width = 2, XName = "Period", YName = "ProductA_Sales", Name = "Renewable", Opacity = 1, AllowDataEditing = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7 } },
new UiChartSeries { Type = UiChartSeriesType.Line, Width = 2, XName = "Period", YName = "ProductB_Sales", Name = "Non-Renewable", Opacity = 1, AllowDataEditing = true, Marker = new UiChartMarker { Visible = true, Width = 7, Height = 7, IsFilled = 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/DataEditing.cshtml; it draws what ViewData holds.
public ActionResult DataEditing()
{
List<DataEditingData> ConsumerDetails = new List<DataEditingData>
{
new DataEditingData { Period = "2005", ProductA_Sales = 21, ProductB_Sales= 28},
new DataEditingData { Period = "2006", ProductA_Sales = 24, ProductB_Sales= 44},
new DataEditingData { Period = "2007", ProductA_Sales = 36, ProductB_Sales= 48},
new DataEditingData { Period = "2008", ProductA_Sales = 38, ProductB_Sales= 50},
new DataEditingData { Period = "2009", ProductA_Sales = 54, ProductB_Sales= 66},
new DataEditingData { Period = "2010", ProductA_Sales = 57, ProductB_Sales= 78},
new DataEditingData { Period = "2011", ProductA_Sales = 70, ProductB_Sales= 84}
};
ViewData["ConsumerDetails"] = ConsumerDetails;
return View();
}
public class DataEditingData
{
public string Period { get; set; }
public double ProductA_Sales { get; set; }
public double ProductB_Sales { get; set; }
}
}
}
@* 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. *@
In this example, you can see how to drag and drop the data points in the chart by setting Enable property in ChartDataEditSettings to true. Also, you can set data editing’s minimum and maximum range using the MinY and MaxY properties.