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

Creating LineSeries programmatically with custom TrackBallInfoTemplate

6 Answers 565 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 13 Sep 2012, 08:43 PM
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.

6 Answers, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 18 Sep 2012, 01:34 PM
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.

0
Brian
Top achievements
Rank 1
answered on 18 Sep 2012, 02:01 PM
That did the trick, thanks for your help.

BP
0
codeputer
Top achievements
Rank 2
answered on 04 Nov 2012, 01:16 AM
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
0
Tsvetie
Telerik team
answered on 07 Nov 2012, 11:10 AM
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.

0
codeputer
Top achievements
Rank 2
answered on 07 Nov 2012, 08:07 PM
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
0
Tsvetie
Telerik team
answered on 13 Nov 2012, 08:24 AM
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.

Tags
ChartView
Asked by
Brian
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Brian
Top achievements
Rank 1
codeputer
Top achievements
Rank 2
Tsvetie
Telerik team
Share this question
or