Binding radAreaSparkLine to dictionaries

4 posts, 0 answers
  1. Eric
    Eric avatar
    48 posts
    Member since:
    Jul 2012

    Posted 13 Jun 2012 Link to this post

    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;
    }
  2. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 18 Jun 2012 Link to this post

    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 >>

  3. UI for WPF is Visual Studio 2017 Ready
  4. Eric
    Eric avatar
    48 posts
    Member since:
    Jul 2012

    Posted 18 Jun 2012 Link to this post

    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
  5. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 19 Jun 2012 Link to this post

    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 >>

Back to Top