Example of Area Negative Points in ASP.NET Core Charts Control
This sample illustrates an area series with negative points. Data points with negative points are shown here.
@* 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 = "areaNegativeChart",
Title = "Profit and Loss",
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", ShowGridLines = false, MinimumDate = new DateTime(2017, 01, 01), MaximumDate = new DateTime(2021, 01, 01), IntervalType = UiChartIntervalType.Years },
PrimaryYAxis = new UiChartAxis { Minimum = -4000, Maximum = 8000, Interval = 2000, LabelFormat = "${value}" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, Width = 2, XName = "Vegetable", YName = "Price", Name = "Company A", Opacity = 0.75, Data = (IEnumerable<object>)ViewData["MaryValues"], Marker = new UiChartMarker { Visible = true, IsFilled = true, Height = 7, Width = 7, Shape = UiChartMarkerShape.Circle } },
new UiChartSeries { Type = UiChartSeriesType.Area, Width = 2, XName = "Vegetable", YName = "Price", Name = "Company B", Opacity = 0.75, Data = (IEnumerable<object>)ViewData["PatriciaValues"], Marker = new UiChartMarker { Visible = true, IsFilled = true, Height = 7, Width = 7, Shape = UiChartMarkerShape.Diamond } },
new UiChartSeries { Type = UiChartSeriesType.Area, Width = 2, XName = "Vegetable", YName = "Price", Name = "Company C", Opacity = 0.75, Data = (IEnumerable<object>)ViewData["LindaValues"], Marker = new UiChartMarker { Visible = true, IsFilled = true, Height = 5, Width = 5, Shape = UiChartMarkerShape.Rectangle } }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "areaNegativeChart",
Title = "Profit and Loss",
Tooltip = new UiChartTooltip { Enable = true, ShowNearestTooltip = true, EnableHighlight = true },
Legend = new UiChartLegend { Visible = true, EnableHighlight = true },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.DateTime, LabelFormat = "yyyy", ShowGridLines = false, MinimumDate = new DateTime(2017, 01, 01), MaximumDate = new DateTime(2021, 01, 01), IntervalType = UiChartIntervalType.Years },
PrimaryYAxis = new UiChartAxis { Minimum = -4000, Maximum = 8000, Interval = 2000, LabelFormat = "${value}" },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Area, Width = 2, XName = "Vegetable", YName = "Price", Name = "Company A", Opacity = 0.75, Data = (IEnumerable<object>)ViewData["MaryValues"], Marker = new UiChartMarker { Visible = true, IsFilled = true, Height = 7, Width = 7, Shape = UiChartMarkerShape.Circle } },
new UiChartSeries { Type = UiChartSeriesType.Area, Width = 2, XName = "Vegetable", YName = "Price", Name = "Company B", Opacity = 0.75, Data = (IEnumerable<object>)ViewData["PatriciaValues"], Marker = new UiChartMarker { Visible = true, IsFilled = true, Height = 7, Width = 7, Shape = UiChartMarkerShape.Diamond } },
new UiChartSeries { Type = UiChartSeriesType.Area, Width = 2, XName = "Vegetable", YName = "Price", Name = "Company C", Opacity = 0.75, Data = (IEnumerable<object>)ViewData["LindaValues"], Marker = new UiChartMarker { Visible = true, IsFilled = true, Height = 5, Width = 5, Shape = UiChartMarkerShape.Rectangle } }
}
})
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/AreaNegativePoints.cshtml; it draws what ViewData holds.
public ActionResult AreaNegativePoints()
{
List<AreaNegativeChartData> MaryValues = new List<AreaNegativeChartData>
{
new AreaNegativeChartData { Vegetable = new DateTime(2017, 01, 01), Price = 3000 },
new AreaNegativeChartData { Vegetable = new DateTime(2018, 01, 01), Price = 4000 },
new AreaNegativeChartData { Vegetable = new DateTime(2019, 01, 01), Price = -4000 },
new AreaNegativeChartData { Vegetable = new DateTime(2020, 01, 01), Price = -2000 },
new AreaNegativeChartData { Vegetable = new DateTime(2021, 01, 01), Price = 5000 }
};
List<AreaNegativeChartData> PatriciaValues = new List<AreaNegativeChartData>
{
new AreaNegativeChartData { Vegetable = new DateTime(2017, 01, 01), Price = 2000 },
new AreaNegativeChartData { Vegetable = new DateTime(2018, 01, 01), Price = 3000 },
new AreaNegativeChartData { Vegetable = new DateTime(2019, 01, 01), Price = 4000 },
new AreaNegativeChartData { Vegetable = new DateTime(2020, 01, 01), Price = 2000 },
new AreaNegativeChartData { Vegetable = new DateTime(2021, 01, 01), Price = 3000 }
};
List<AreaNegativeChartData> LindaValues = new List<AreaNegativeChartData>
{
new AreaNegativeChartData { Vegetable = new DateTime(2017, 01, 01), Price = 2000 },
new AreaNegativeChartData { Vegetable = new DateTime(2018, 01, 01), Price = -1000 },
new AreaNegativeChartData { Vegetable = new DateTime(2019, 01, 01), Price = -3000 },
new AreaNegativeChartData { Vegetable = new DateTime(2020, 01, 01), Price = 4000 },
new AreaNegativeChartData { Vegetable = new DateTime(2021, 01, 01), Price = 1000 }
};
ViewData["MaryValues"] = MaryValues;
ViewData["PatriciaValues"] = PatriciaValues;
ViewData["LindaValues"] = LindaValues;
return View();
}
public class AreaNegativeChartData
{
public DateTime Vegetable { get; set; }
public double Price { 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 render an area series with negative points. Also, a legend is enabled in the shape of the series type.