Search results

Getting started with Blazor Sparkline component

08 Apr 2021 / 4 minutes to read

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.

Importing Syncfusion Blazor component in the application

  1. Install Syncfusion.Blazor NuGet package to the application by using the NuGet Package Manager.
  2. You can add the client-side resources through CDN or from NuGet package in the HEAD element of the ~/Pages/_Host.cshtml page.
Copied to clipboard
<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.

Copied to clipboard
<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>

Adding component package to the application

Open **~/_Imports.razor file and include the Syncfusion.Blazor.Charts namespace.

Copied to clipboard
@using Syncfusion.Blazor.Charts

Add SyncfusionBlazor service in Startup.cs

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.

Copied to clipboard
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.

Add Sparkline Component

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.

Copied to clipboard
@page "/"

<SfSparkline TValue="WeatherReport">

</SfSparkline>

Populate Sparkline with Data

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.

Copied to clipboard
@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.

Copied to clipboard
<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.

Sparkline Sample

Change the type of sparkline

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.

Copied to clipboard
<SfSparkline XName="Month"
         YName="Celsius"
         ValueType="SparklineValueType.Category"
         Type="SparklineType.Area"
         TValue="WeatherReport"
         DataSource="ClimateData"
         Height="80px"
         Width="150px">
</SfSparkline>

Sparkline Sample

Refer code block to know the property value of ClimateData.

Add data label

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:

  • Start
  • End
  • All
  • High
  • Low
  • Negative
Copied to clipboard
<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.

Sparkline with data label

Enable tooltip

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.

Copied to clipboard
<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.

Sparkline with tooltip

See also