Search results

Getting Started

This section briefly explains how to include a Sparkline in your Blazor client-side application. Refer to this Getting Started with Syncfusion Blazor for Client-Side in Visual Studio 2019 Preview documentation for introduction and configuring common specifications.

Importing Syncfusion Blazor component in an application

  1. Install Syncfusion.EJ2.Blazor NuGet package in application using the NuGet Package Manager. Please ensure to check the Include prerelease option.
  2. Add the client-side resources through CDN in the <head> element of the ~/wwwroot/index.html page.
<head>
    <environment include="Development">
        <script src="https://cdn.syncfusion.com/ej2/17.3.29/dist/ej2.min.js"></script>
     </environment>
</head>

Adding component package to an application

Open the ~/_Imports.razor file and import the Syncfusion.EJ2.Blazor.Charts package.

@using Syncfusion.EJ2.Blazor
@using Syncfusion.EJ2.Blazor.Charts

Adding Sparkline component to an application

Now, add the Syncfusion Blazor Sparkline component in any webpage razor in the Pages folder. For example, the sparkline component is added in the ~/Pages/Index.razor page.

You can use the DataSource property to load climate data into Sparkline. Also using the XName and YName properties, you can map the values of the x-axis and y-axis in sparkline from the data source. Since the Month is a value based on categories, specify it using the ValueType property.

<EjsSparkline XName="Month"
              YName="Celsius"
              ValueType="SparklineValueType.Category"
              TValue="WeatherReport"
              DataSource="@climateData"
              Height="80px"
              Width="150px">
</EjsSparkline>

@code {
    public class WeatherReport
    {
        public string Month;
        public double Celsius;
    };
    private 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}
    };
}

Run the application

After the successful compilation of your application, press F5 to run the application. The Blazor Sparkline component will be rendered in the web browser as illustrated in the following screenshot.

Sparkline Sample

Change the type of sparkline

You can change the sparkline type by setting the Type property to Line, Column, WinLoss, Pie or Area. Here, the sparkline type is set to Area.

<EjsSparkline XName="Month"
              YName="Celsius"
              ValueType="SparklineValueType.Category"
              Type="SparklineType.Area"
              TValue="WeatherReport"
              DataSource="@climateData"
              Height="80px"
              Width="150px">
</EjsSparkline>

Refer code block to know the property value of climateData.

Sparkline Sample

Add data label

You can add data labels to improve the readability of the sparkline chart. This can be achieved by setting the Visible property in the SparklineDataLabelSettings. Available types are:

  • Start
  • End
  • All
  • High
  • Low
  • Negative
<EjsSparkline 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>
</EjsSparkline>

Refer code block to know the property value of climateData.

Sparkline with data label

Enable tooltip

The tooltip is useful when you cannot display information by using the data labels due to space constraints. You can enable tooltip by setting the Visible property value to true in SparklineTooltipSettings.

<EjsSparkline 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 Visible="true"></SparklineTooltipSettings>
</EjsSparkline>

Refer code block to know the property value of climateData.

Sparkline with tooltip

See also