this.UltraSparkline1.DataSource = new TestData();
this.UltraSparkline1.ValueMemberPath = "Value";
This topic explains, with code examples, how to bind the UltraSparkline™ control to data.
The following table lists the topics required as a prerequisite to understanding this topic.
This topic contains the following sections:
The UltraSparkline control requires one-dimensional data. The data set must contain at least two numeric fields. If these data type requirements are not met UltraSparkline renders a blank chart (no error is returned). The text in the data source fields can be used to display the first and the last label on the X axis.
As a minimum, UltraSparkline requires the DataSource property which is used to bind the control to an instance of the data collection and the ValueMemberPath property to identify what data to should be displayed.
The following table lists the data binding property settings.
The following code demonstrates the data binding.
In C#:
this.UltraSparkline1.DataSource = new TestData();
this.UltraSparkline1.ValueMemberPath = "Value";
In Visual Basic:
Me.UltraSparkline1.DataSource = New TestData()
Me.UltraSparkline1.ValueMemberPath = "Value"
In C#:
public class TestData : ObservableCollection<TestDataItem>
{
public TestData()
{
Add(new TestDataItem { Label = "Label1", Value = 1 });
Add(new TestDataItem { Label = "Label2", Value = 2 });
Add(new TestDataItem { Label = "Label3", Value = 3 });
Add(new TestDataItem { Label = "Label4", Value = -4 });
Add(new TestDataItem { Label = "Label5", Value = 5 });
Add(new TestDataItem { Label = "Label6", Value = -6 });
Add(new TestDataItem { Label = "Label7", Value = 7 });
Add(new TestDataItem { Label = "Label8", Value = 8 });
Add(new TestDataItem { Label = "Label9", Value = 9 });
}
}
public class TestDataItem : INotifyPropertyChanged
{
private string _label;
public string Label
{
get { return _label; }
set { _label = value; RaisePropertyChanged("Label"); }
}
private double? _value;
public double? Value
{
get { return _value; }
set { _value = value; RaisePropertyChanged("Value"); }
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
In Visual Basic:
Public Class TestData
Inherits ObservableCollection(Of TestDataItem)
Public Sub New()
Add(New TestDataItem() With {.Label = "Label1", .Value = 1})
Add(New TestDataItem() With {.Label = "Label2", .Value = 2})
Add(New TestDataItem() With {.Label = "Label3", .Value = 3})
Add(New TestDataItem() With {.Label = "Label4", .Value = -4})
Add(New TestDataItem() With {.Label = "Label5", .Value = 5})
Add(New TestDataItem() With {.Label = "Label6", .Value = -6})
Add(New TestDataItem() With {.Label = "Label7", .Value = 7})
Add(New TestDataItem() With {.Label = "Label8", .Value = 8})
Add(New TestDataItem() With {.Label = "Label9", .Value = 9})
End Sub
End Class
Public Class TestDataItem
Implements INotifyPropertyChanged
Private _label As String
Public Property Label() As String
Get
Return _label
End Get
Set
_label = value
RaisePropertyChanged("Label")
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
RaisePropertyChanged("Value")
End Set
End Property
Private Sub RaisePropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler
End Class
The following topics provide additional information related to this topic.