Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
45
Setting tooltip for programmatically added series
posted

I am programmatically adding series to my XamDataChart; these data come from the server and hence I can't put them in xaml since I wouldn't know how many series I will get back.

When I tried to add tooltip using a tooltip template I kept getting: 'ToolTip' cannot have a logical or visual parent. Same problem when I tried to wrap this in a markertemplate instead.

Other than this the series display fine. Can you advise?

My code:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

        private void AddSpreads(SomeDataCollection panelVMs)
        {
            SpreadChart.Series.Clear();
            var toolTipTemplate = SpreadChart.Resources["ToolTipTemplate"] as DataTemplate;
            for (int i = 0; i < panelVMs.Count; i++)
            {
                RolledATMVolPanelViewModel panelVM = panelVMs[i];
                string pair = panelVM.CcyPair;

                // add to spreads chart
                var series = new ScatterLineSeries
                                 {
                                     Title = pair,
                                     MarkerType = MarkerType.Circle,
                                     Brush = CommonSettings.Colours[i % CommonSettings.Colours.Length],
                                     XAxis = XAxis,
                                     YAxis = YAxis,
                                     XMemberPath = "DaysToMaturity",
                                     YMemberPath = "Spread",
                                 };
                series.MarkerBrush = series.MarkerOutline = series.Brush;
                if (toolTipTemplate != null)
                {
                    var toolTip = new ToolTip();
                    toolTip.ContentTemplate = toolTipTemplate;
                    series.ToolTip = toolTip;
                }

                var spreads = new List<SpreadItem>();
//... here I populate spreads with data from teh view model

                series.ItemsSource = spreads;
                SpreadChart.Series.Add(series);
            }
        }

My xaml:

              <Controls:XamDock Margin="0">
                <Charts:XamDataChart Controls:XamDock.Edge="Central" Margin="3" Legend="{Binding ElementName=Legend}"
                                     VerticalZoomable="True" VerticalZoombarVisibility="Collapsed" Background="Transparent"
                                     HorizontalZoomable="True" HorizontalZoombarVisibility="Collapsed" Name="SpreadChart">
                  <Charts:XamDataChart.Resources>
                    <DataTemplate x:Key="ToolTipTemplate">
                      <StackPanel Orientation="Vertical">
                        <StackPanel>
                          <TextBlock FontWeight="Bold" Text="{Binding Series.Title}"/>
                        </StackPanel>
                        <StackPanel>
                          <TextBlock FontWeight="Bold" Text="Mty: "/>
                          <TextBlock Text="{Binding Item.DaysToMaturity}"/>
                        </StackPanel>
                        <StackPanel>
                          <TextBlock FontWeight="Bold" Text="Spread: "/>
                          <TextBlock Text="{Binding Item.Spread, StringFormat=0.00%}"/>
                        </StackPanel>
                      </StackPanel>
                    </DataTemplate>
                  </Charts:XamDataChart.Resources>
                 
                  <Charts:XamDataChart.Axes>
                    <Charts:NumericXAxis x:Name="XAxis" Label="{}{:0}" MajorStroke="Transparent" MinorStroke="Transparent"
                                         IsLogarithmic="True" LogarithmBase="10" >
                      <Charts:NumericXAxis.LabelSettings>
                        <Charts:AxisLabelSettings Location="OutsideBottom" Extent="35" />
                      </Charts:NumericXAxis.LabelSettings>
                    </Charts:NumericXAxis>

                    <Charts:NumericYAxis x:Name="YAxis" Label="{}{:0.00%}" >
                      <Charts:NumericYAxis.LabelSettings >
                        <Charts:AxisLabelSettings Location="OutsideLeft" Extent="55" />
                      </Charts:NumericYAxis.LabelSettings>
                    </Charts:NumericYAxis>
                  </Charts:XamDataChart.Axes>
                 
                </Charts:XamDataChart>

                <Charts:Legend Name="Legend" Controls:XamDock.Edge="InsideTop" Controls:XamDock.VerticalDockAlignment="Top"
                               Controls:XamDock.HorizontalDockAlignment="Right"/>
               
              </Controls:XamDock>