Creating LineSeries programmatically with custom TrackBallInfoTemplate

7 posts, 1 answers
  1. Brian
    Brian avatar
    10 posts
    Member since:
    Jul 2012

    Posted 13 Sep 2012 Link to this post

    I am trying to change the track ball info, but I cannot figure out the right combination to get the data to come out the way I need it to come out. I want to show an identifier for a line series in much the same way that the ChartView demo does, but in that demo, the series and format strings are encoded into the XAML. In my app, there can be any number of line series, so they need to be created programmatically.

    Here is the class that holds the data:
    public class SalesInfo
    {
        public string Employee { get; set; }
        public DateTime Time { get; set; }
        public int Value { get; set; }
    }

    And here is where I am creating the data and setting the item source of my line series:
    data = new RadObservableCollection<SalesInfo>(data.OrderBy(x => x.Time));
     
    Color dataColor = colorArray[loopCounter % 4];
     
    LineSeries line = new LineSeries();
    line.Stroke = new SolidColorBrush(dataColor);
    line.StrokeThickness = 2;
    line.CategoryBinding = new PropertyNameDataPointBinding() { PropertyName = "Time" };
    line.ValueBinding = new PropertyNameDataPointBinding() { PropertyName = "Value" };
    line.ItemsSource = data;
    var tbiTemplate = this.Resources["theTemplate"] as DataTemplate;
    line.TrackBallInfoTemplate = tbiTemplate;
    activitiesAddedChart.Series.Add(line);

    And finally the template in the resources:
    <DataTemplate x:Key="theTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding DataPoint.SalesInfo.Employee}" />
            <TextBlock Text=": " />
            <TextBlock Text="{Binding DataPoint.Value}" />
        </StackPanel>
    </DataTemplate>

    I cannot figure out how to get this bound correctly so that I can see the Employee name in the track ball info. I get the Value fine, and if I use DataPoint.Category, I can get the date, but I just cannot figure out how to get the Employee to show up in that first TextBlock in the template. Thanks.

  2. Answer
    Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 18 Sep 2012 Link to this post

    Hi Brian,

    We have exposed the original object that created the data point through the DataItem property. So you will receive an instance of SalesInfo in your DataPoint.DataItem. So you can rewrite bindings to this:

    <DataTemplate x:Key="theTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding DataPoint.DataItem.Employee}" />
            <TextBlock Text=": " />
            <TextBlock Text="{Binding DataPoint.DataItem.Value}" />
        </StackPanel>
    </DataTemplate>

    Hope this helps!
    All the best,
    Yavor
    the Telerik team

    Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

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

    Posted 18 Sep 2012 Link to this post

    That did the trick, thanks for your help.

    BP
  5. codputer
    codputer avatar
    45 posts
    Member since:
    Apr 2007

    Posted 03 Nov 2012 Link to this post

    The only thing that I'm now missing is the legend - how can I produce that?

    The legend is required as the color of the line graph must be matched to the statistic - can I bind in a rectangle or line with the color of the series?  I'm using the Metro Palette on the RadCartesianChart.  Did a Legend make it to the chart?  Can I do this in the trackballinfotemplate as well?

    Richard
  6. Tsvetie
    Admin
    Tsvetie avatar
    1517 posts

    Posted 07 Nov 2012 Link to this post

    Hello Richard,

    RadChartView for WPF has no built-in support for a chart legend yet. The feature is on our ToDo list, but I cannot commit to a time frame for availability. You can use, however, one of the approaches that we use in our demos to add a legend.

    Regards,
    Tsvetie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  7. codputer
    codputer avatar
    45 posts
    Member since:
    Apr 2007

    Posted 07 Nov 2012 Link to this post

    I've reviewed the demos, and it only has a static list
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,20,0">
        <Rectangle Fill="#FF8EC441" Height="2" Width="15" Margin="5,0" />
        <TextBlock Text="International market" Margin="0,0,9,0" />
        <Rectangle Fill="#FF1B9DDE" Height="2" Width="15" Margin="0,0,5,0" />
        <TextBlock Text="Domestic market" />
    </StackPanel>

    As this forum question is dynamically adding each series, how would you suggest that I dynamically create the legend?  I'm assuming the legend could be a list bound to the series collection, but I need a little help with that.  Especially around binding the stroke of the series to the rectangle color!  I'm using the Metro palette so, so the color is determined at run time as well.

    R
  8. Tsvetie
    Admin
    Tsvetie avatar
    1517 posts

    Posted 13 Nov 2012 Link to this post

    Hi Richard,

    Yes, you can build your legend dynamically. You can use a ListBox control, for example, and bind it to your list of legend items. I have attached a simple page, demonstrating my idea.

    Regards,
    Tsvetie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
UI for WPF is Visual Studio 2017 Ready