This section briefly explains how to include a Sparkline
in your Blazor server-side application. You can refer Getting Started with Syncfusion Blazor for Server-Side in Visual Studio 2019 page for introduction and configuring common specifications.
<head>
<link href="_content/Syncfusion.Blazor.Themes/bootstrap4.css" rel="stylesheet" />
<!---CDN--->
@*<link href="https://cdn.syncfusion.com/blazor/18.4.42/styles/bootstrap4.css" rel="stylesheet" />*@
</head>
For Internet Explorer 11, kindly refer the polyfills. Refer the
documentation
for more information.
<head>
<link href="https://cdn.syncfusion.com/blazor/18.4.42/styles/bootstrap4.css" rel="stylesheet" />
<script src="https://github.com/Daddoon/Blazor.Polyfill/releases/download/3.0.1/blazor.polyfill.min.js"></script>
</head>
Open **~/_Imports.razor
file and include the Syncfusion.Blazor.Charts
namespace.
@using Syncfusion.Blazor.Charts
Open the Startup.cs file and add services required by Syncfusion components using services.AddSyncfusionBlazor() method. Add this method in the ConfigureServices function as follows.
using Syncfusion.Blazor;
namespace BlazorApplication
{
public class Startup
{
....
....
public void ConfigureServices(IServiceCollection services)
{
....
....
services.AddSyncfusionBlazor();
}
}
}
To enable custom client-side source loading from CRG or CDN, please refer to the section about custom resources in Blazor application.
To initialize the Sparkline
component, add the below code to your Index.razor view page under ~/Pages folder. In a new application, if Index.razor page has any default content template, then those content can be completely removed and following code can be added.
@page "/"
<SfSparkline TValue="WeatherReport">
</SfSparkline>
To bind data for the Sparkline
component, you can assign a IEnumerable
object to
the DataSource
property. It can also be provided as an instance of the DataManager
.
@code {
public class WeatherReport
{
public string Month { get; set; }
public double Celsius { get; set; }
};
public List<WeatherReport> ClimateData = new List<WeatherReport> {
new WeatherReport { Month= "Jan", Celsius= 34 },
new WeatherReport { Month= "Feb", Celsius= 36 },
new WeatherReport { Month= "Mar", Celsius= 32 },
new WeatherReport { Month= "Apr", Celsius= 35 },
new WeatherReport { Month= "May", Celsius= 40 },
new WeatherReport { Month= "Jun", Celsius= 38 },
new WeatherReport { Month= "Jul", Celsius= 33 },
new WeatherReport { Month= "Aug", Celsius= 37 },
new WeatherReport { Month= "Sep", Celsius= 34 },
new WeatherReport { Month= "Oct", Celsius= 31 },
new WeatherReport { Month= "Nov", Celsius= 30 },
new WeatherReport { Month= "Dec", Celsius= 29}
};
}
Now map Month
and Celsius
fields from the datasource to XName
and YName
properties for x-axis and y-axis in Sparkline
and then set the ClimateData
to DataSource
property. Since the Month
field is a value based category, so it specify by ValueType
property.
<SfSparkline XName="Month"
YName="Celsius"
ValueType="SparklineValueType.Category"
TValue="WeatherReport"
DataSource="ClimateData"
Height="80px"
Width="150px">
</SfSparkline>
On successful compilation of your application, the Syncfusion Blazor Sparkline
component will render in the web browser as shown below.
You can change the Sparkline
type to specify by Type
property as Line, Column, WinLoss, Pie or Area. Here, the sparkline type is set to Area.
<SfSparkline XName="Month"
YName="Celsius"
ValueType="SparklineValueType.Category"
Type="SparklineType.Area"
TValue="WeatherReport"
DataSource="ClimateData"
Height="80px"
Width="150px">
</SfSparkline>
Refer code block to know the property value of ClimateData.
You can add data labels to improve the readability of the Sparkline
component. This can be achieved by Visible
property in the SparklineDataLabelSettings
.
Available types are:
<SfSparkline DataSource="ClimateData"
TValue="WeatherReport"
XName="Month"
YName="Celsius"
ValueType="SparklineValueType.Category"
Height="80px"
Width="150px">
<SparklineDataLabelSettings Visible="new List<VisibleType> { VisibleType.Start, VisibleType.End }"></SparklineDataLabelSettings>
<SparklinePadding Left="10" Right="10"></SparklinePadding>
</SfSparkline>
Refer code block to know the property value of ClimateData.
When space constraints prevent you from displaying information using data labels, the tooltip comes in handy.
The tooltip can be enabled by setting the Visible
property as true in SparklineTooltipSettings
.
<SfSparkline DataSource="ClimateData"
TValue="WeatherReport"
XName="Month"
YName="Celsius"
ValueType="SparklineValueType.Category"
Height="80px"
Width="150px">
<SparklineDataLabelSettings Visible="new List<VisibleType> { VisibleType.Start, VisibleType.End }"></SparklineDataLabelSettings>
<SparklinePadding Left="10" Right="10"></SparklinePadding>
<SparklineTooltipSettings TValue="WeatherReport" Visible="true"></SparklineTooltipSettings>
</SfSparkline>
Refer code block to know the property value of ClimateData.