Version

Adding UltraSparkline to a Page

Purpose

This topic demonstrates, with code examples, how to add the UltraSparkline™ control to a page. If you want to display UltraSparkline inside grid cells, refer to the Sparkline Column topic.

Required background

The following table lists the materials required as a prerequisite to understanding this topic.

Topic Purpose

This topic provides an overview of the UltraSparkline control, its benefits, and the supported chart types.

This topic provides an overview of the possible ways to configure the UltraSparkline control. Links to the detailed configurations (available in separate topics) are provided as well.

Adding a UltraSparkline to a Page – Conceptual Overview

Introduction

Though UltraSparkline is designed with the idea to be inserted in much small spaces such as grid cells or in-line with text, it has the capability to be displayed as a stand-alone control, e.g. in web pages.

With the default settings, the Sparkline will display the Line type with gray color and line thickness of 1 pixel. The line color or thickness is customizable.

The Sparkline will take up the entire space of the container it resides on unless the width and height are specified, which are optional. Resizing the container or the browser window will automatically resize the Sparkline that is hosted in the container.

Requirements

The following table summarizes the requirements for adding the UltraSparkline control.

Assembly references

See the following topic for more information, Add references through NuGet Packages

Namespaces

In XAML:

Data

One-dimensional data that contains numeric values.

Note
Note

With the Win/Loss type of Sparkline, only two values will be used for the chart – the highest one and the lowest one. The rest of the values in the data set will be ignored, but the number of data points will be considered. In other words, if the data consists of 5 positive values and 2 negative values, then WinLoss will display 5 bars above zero (all identical size matching the highest value) and 2 negative bars below zero (all identical size matching the lowest value).

Overview

Following is a conceptual overview of the process.

Referencing the data model

Adding the UltraSparkline control

Adding a Sparkline to a Page – Code Example

Description

The procedure that follows demonstrates how to add a Sparkline on a page using the Line type of Sparkline.

For this purpose you would need to create a class data model populated with numeric values for the data points and string field for displaying the first and last labels.

Preview

Following is a preview of the final result.

Adding Sparkline to your page 1.png

Prerequisites

To complete the procedure, you need the following:

  • a Windows Forms project

Overview

Following is a conceptual overview of the process.

Defining the data model

Populating the data

Referencing the data model

Adding the UltraSparkline control

Verifying the result

Steps

The following steps demonstrate how to add a Sparkline to the page.

  1. Define the data model.

Create a class definition to represent the data model for the Sparkline. At least two numeric fields is needed by UltraSparkline control in order to render the Sparkline.

In C#:

public class TestDataItem
    {
        private string _label;
        public string Label
        {
            get { return _label; }
            set { _label = value; }
        }
        private double? _value;
        public double? Value
        {
            get { return _value; }
            set { _value = value; }
        }
    }

In Visual Basic:

Public Class TestDataItem
      Private _label As String
      Public Property Label() As String
            Get
                  Return _label
            End Get
            Set
                  _label = value
            End Set
      End Property
      Private _value As System.Nullable(Of Double)
      Public Property Value() As System.Nullable(Of Double)
            Get
                  Return _value
            End Get
            Set
                  _value = value
            End Set
      End Property
End Class
  1. Populate the data.

The following code creates a list of records containing numeric and string values. The chart will use the numeric field to plot data, and the string can be used to display the first and last labels on the horizontal (X) axis. The numeric values can also be displayed as labels.

In C#:

public class TestData : ObservableCollection<TestDataItem>
    {
        public TestData()
        {
            Add(new TestDataItem { Label = "Label1", Value = 3 });
            Add(new TestDataItem { Label = "Label2", Value = 1 });
            Add(new TestDataItem { Label = "Label3", Value = 4 });
            Add(new TestDataItem { Label = "Label4", Value = 2 });
            Add(new TestDataItem { Label = "Label5", Value = 7 });
            Add(new TestDataItem { Label = "Label6", Value = -3 });
            Add(new TestDataItem { Label = "Label7", Value = 4 });
            Add(new TestDataItem { Label = "Label8", Value = 1 });
            Add(new TestDataItem { Label = "Label9", Value = 3 });
        }
    }

In Visual Basic:

Public Class TestData
   Inherits ObservableCollection(Of TestDataItem)
      Public Sub New()
      Add(New TestDataItem() With {.Label = "Label1", .Value = 3})
      Add(New TestDataItem() With {.Label = "Label2", .Value = 1})
      Add(New TestDataItem() With {.Label = "Label3", .Value = 4})
      Add(New TestDataItem() With {.Label = "Label4", .Value = 2})
      Add(New TestDataItem() With {.Label = "Label5", .Value = 7})
      Add(New TestDataItem() With {.Label = "Label6", .Value = -3})
      Add(New TestDataItem() With {.Label = "Label7", .Value = 4})
      Add(New TestDataItem() With {.Label = "Label8", .Value = 1})
      Add(New TestDataItem() With {.Label = "Label9", .Value = 3})
   End Sub
End Class
  1. Reference the data model.

Build (compile) the application at this point so the data model can be referenced.

  1. Add the UltraSparkline control.

The following code demonstrates adding the UltraSparkline with the minimum code and property settings required for display.

In C#:

this.UltraSparkline1.DataSource = new TestData();
this.UltraSparkline1.DisplayType = Infragistics.Win.DataVisualization.SparklineDisplayType.Line;
this.UltraSparkline1.ValueMemberPath = "Value";

In Visual Basic:

Me.UltraSparkline1.DataSource = new TestData()
Me.UltraSparkline1.DisplayType = Infragistics.Win.DataVisualization.SparklineDisplayType.Line
Me.UltraSparkline1.ValueMemberPath = "Value"
  1. Verify the result.

Build and run your project to verify the result. If you have implemented the steps correctly, the displayed Sparkline should look like the one in the Preview above.

Related Content

The following topics provide additional information related to this topic.

Topic Purpose

This topic explains the featured properties of the UltraSparkline control.