This is a migrated thread and some comments may be shown as answers.

Binding radAreaSparkLine to dictionaries

3 Answers 89 Views
Sparkline
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 14 Jun 2012, 12:07 AM
Hello,

I am having some trouble binding a radAreaSparkLine to an ObseravbleCollection of dictionaries.  I am trying to use a radTimeBar using the getting started1 video.  The code I have is below.  When I run parseResourceFile, the resourceItems collection is populated.  However, when I create the radAreaSparkLine, no areaPoints are created, are nothing shows up in the timeBar.  Does it look like I am doing anything incorrectly?

Thanks,
Eric
<telerik:RadTimeBar Grid.Row="2" x:Name="radTimeBar">
    <telerik:RadTimeBar.Intervals>
        <telerik:HourInterval />
        <telerik:MinuteInterval/>
        <telerik:SecondInterval/>
        <telerik:MillisecondInterval/>
    </telerik:RadTimeBar.Intervals>
</telerik:RadTimeBar>

class ResourceItem
{
   public Dictionary<int, double> NumericalResourceValues
    {
        get { return numericalResourceValues ?? (numericalResourceValues = new Dictionary<int, double>()); }
    }
    public Dictionary<int, double> numericalResourceValues;
}

class Resource
{
    public ObservableCollection<ResourceItem> ResourceItems { get; set; }
}

void populateTimeBar()
{
int
startTimeColumnIndex = 1;
int dataColumnIndex = 1;           
var resourceParser = new ResourceParser();
Resource resource = resourceParser.PaserResourceFile(Constants.LogFilePath);
 
var rasl = new RadAreaSparkline
               {
                   ItemsSource = resource.ResourceItems,
                   XValuePath = "NumericalResourceValues[" + startTimeColumnIndex + "]",
                   YValuePath = "NumericalResourceValues[" + dataColumnIndex + "]"
               };
 
this.radTimeBar.Content = rasl;
this.radTimeBar.DataContext = resource.ResourceItems;
}

3 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 18 Jun 2012, 08:17 AM
Hi Eric,

First I should mention, that RadTimeBar is a content control and could display any content, not only RadSparkline. You can test how the sparkline performs outside of the TimeBar and when you reach the desired result you can put it inside.

The Area sparkline can display any IEnumerable similar to a chart control. In order for the sparkline to be able to visualize the data it has to be either directly accessible from the collection, or through a property.

Example 1 (simple data):
Let's say you want to bind your sparkline to a collection of doubles:

sparkline.ItemsSource = new double[] { 1, 2, 3 };

This way the sparkline gats the entire item from the collection and displays it.

Example 2 (custom objects):
If you want to use custom objects that store your data you will have to tell the sparkline how to get the data for its X and Y values:
public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        DateTime today = DateTime.Today;
 
        List<MyCost> data = new List<MyCost>()
        {
            new MyCost() { Cost = 1, UnitCost = 2, MyDate=today },
            new MyCost() { Cost = 2, UnitCost = 4, MyDate= today.AddDays(1)},
            new MyCost() { Cost = 3, UnitCost = 6, MyDate=today.AddDays(2) },
            new MyCost() { Cost = 4, UnitCost = 4, MyDate=today.AddDays(3)},
            new MyCost() { Cost = 5, UnitCost = 8, MyDate=today.AddDays(4)},
        };
        this.sparkline1.ItemsSource = data;
        this.sparkline1.XValuePath = "MyDate";
        this.sparkline1.YValuePath = "UnitCost";
    }
}
 
public class MyCost
{
    public double Cost { get; set; }
    public double UnitCost { get; set; }
    public DateTime MyDate { get; set; }
}

You can find more information on data binding sparklines in this topic in our help system.Greetings,
Yavor
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Eric
Top achievements
Rank 1
answered on 18 Jun 2012, 07:14 PM
So after a little bit of research I found out that binding to a list of dictionaries is not possible in c# :(.   Oh well, thanks for the reply.
Eric
0
Yavor
Telerik team
answered on 19 Jun 2012, 06:44 AM
Hi Eric,

Perhaps if you send us a sample of your data contained in a list of dictionaries we can play with it here in our labs and see if we can extract data for the sparkline from it.

Greetings,
Yavor
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
Sparkline
Asked by
Eric
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Eric
Top achievements
Rank 1
Share this question
or