Example of Print in ASP.NET Core Charts Control
This sample demonstrates the print option in the charts.
@* 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 = "printChart",
Title = "Sales Comparision",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, Interval = 1, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "${value}k", Minimum = 0, Maximum = 20, Interval = 4 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Manager", YName = "SalesInfo", ShowDataLabel = true }
}
})
@Html.UiChart(new UiChartOptions
{
Id = "printChart",
Title = "Sales Comparision",
Data = (IEnumerable<object>)ViewData["ChartPoints"],
Legend = new UiChartLegend { Visible = false },
PrimaryXAxis = new UiChartAxis { ValueType = UiChartValueType.Category, Interval = 1, ShowGridLines = false },
PrimaryYAxis = new UiChartAxis { LabelFormat = "${value}k", Minimum = 0, Maximum = 20, Interval = 4 },
Series = new List<UiChartSeries>
{
new UiChartSeries { Type = UiChartSeriesType.Column, XName = "Manager", YName = "SalesInfo", 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/ChartPrint.cshtml; it draws what ViewData holds.
public ActionResult ChartPrint()
{
List<PrintChartData> ChartPoints = new List<PrintChartData>
{
new PrintChartData { Manager = "John", SalesInfo = 10, DataLabelMappingName = "$10k" },
new PrintChartData { Manager = "Jake", SalesInfo = 12, DataLabelMappingName = "$12k" },
new PrintChartData { Manager = "Peter", SalesInfo = 18, DataLabelMappingName = "$18k" },
new PrintChartData { Manager = "James", SalesInfo = 11, DataLabelMappingName = "$11k" },
new PrintChartData { Manager = "Mary", SalesInfo = 9.7, DataLabelMappingName = "$9.7k" }
};
ViewData["ChartPoints"] = ChartPoints;
return View();
}
public class PrintChartData
{
public string Manager { get; set; }
public double SalesInfo { get; set; }
public string DataLabelMappingName { 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. *@
By clicking the Print button, you can print a chart directly from the browser using the print method.