From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Graph Legend current Value

Hello,

can i change the Legend that it shows the current Value of the Plot ?

It works with Windows Forms and Labview.

Please have a look at the Image as Example:

wpfGraphLegend.png

0 Kudos
Message 1 of 10
(6,511 Views)

The default data template used to display plots in the WPF Legend use a glyph and a text box to show the Renderer and Label for a plot.


You can either add custom objects that associate each plot with a current value, or use a property like Tag on the plot itself. Below is an example of the latter approach, using a data template targeted at Plot and displaying Tag:


    <DataTemplate DataType="{x:Type ni:Plot}">
        <StackPanel Orientation="Horizontal">
            <niPrimitives:LegendGlyph Margin="3"
                    Renderer="{Binding ActualRenderer}"
                    Background="{Binding ItemBackground, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
                    Width="{Binding GlyphSize.Width, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
                    Height="{Binding GlyphSize.Height, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}" />

            <ContentPresenter Margin="3"
                    VerticalAlignment="Center"
                    Content="{Binding Tag}" />
        </StackPanel>
    </DataTemplate>

~ Paul H
0 Kudos
Message 2 of 10
(6,502 Views)

Hi Paul,

im sorry but it didn't work in my case.

Here is my code. Did i made something wrong ?

<Border Style="{StaticResource ControlsBorderStyle}" Grid.Row="1" Grid.ColumnSpan="2">
                        <Border.Resources>
                            <DataTemplate DataType="{x:Type ni:Plot}">
                                <StackPanel Orientation="Horizontal">
                                    <niPrimitives:LegendGlyph Margin="3" 
                    Renderer="{Binding ActualRenderer}" 
                    Background="{Binding ItemBackground, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
                     Width="{Binding GlyphSize.Width, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
                     Height="{Binding GlyphSize.Height, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}" />

                                    <ContentPresenter Margin="3" 
                    VerticalAlignment="Center" 
                    Content="{Binding Tag}" />
                                </StackPanel>
                            </DataTemplate>
                        </Border.Resources>
                        <ni:Graph 
                    x:Name="graph"
                    DataSource="{Binding chartCollection}" 
                    RenderMode="Raster" 
                    PreferIndexData="True"
                    SuppressScaleLayout="True"
                   nix:GraphExtensions.PlotsSource="{Binding Plots2}"
                   nix:GraphExtensions.AxesSource="{Binding GraphAxes}">
                            <ni:Graph.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Konfiguration" Command="{Binding ShowConfigurationCommand}" >
                                    </MenuItem>
                                    <MenuItem Header="Entfernen" Command="{Binding RemoveMeCommand}" >
                                    </MenuItem>
                                    <MenuItem Header="Ebene nach vorne" Command="{Binding MoveOneLayerUpCommand}" >
                                    </MenuItem>
                                    <MenuItem Header="Ebene nach hinten" Command="{Binding MoveOneLayerDownCommand}" >
                                    </MenuItem>
                                </ContextMenu>
                            </ni:Graph.ContextMenu>
                        </ni:Graph>
                    </Border>
                    <Border BorderBrush="White" BorderThickness="0,1,1,1" Grid.Row="1" Grid.RowSpan="2" Grid.Column="2">
                        <ni:Legend    x:Name="legend" ItemsSource="{Binding ElementName=graph, Path=Plots}" Margin="0,0,5,0" Padding="5"
                   Background="Transparent" BorderThickness="0">
                        </ni:Legend>
                    </Border>
                    <Border BorderBrush="White" BorderThickness="1,0,1,1" Grid.Row="2" Grid.ColumnSpan="2"  >
                        <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
                            <ni:GraphInteractionPalette  Graph="{Binding ElementName=graph}" />
                            <Button Content="Start" Width="30"  Command="{Binding StartCommand}"/>
                            <Button Content="Stop" Width="30"  Command="{Binding StopCommand}"/>
                        </StackPanel>
                    </Border>

 

0 Kudos
Message 3 of 10
(6,492 Views)

Looking at the structure of your XAML:


  • Border
    • Resources
      • DataTemplate for legend
    • Graph
  • Border
    • Legend
  • Border
    • StackPanel
      • GraphInteractionPalette
      • Button
      • Button


Since WPF resources are found by traversing parent elements, the legend will not see the data template defined under the graph's border. If you move the resources to the legend's border, or move the resources up to the parent grid, then the legend should be able to resolve the resource.

~ Paul H
0 Kudos
Message 4 of 10
(6,488 Views)

Hi Paul,

i changed it to:

<StackPanel>
                            <StackPanel.Resources>
                                <DataTemplate DataType="{x:Type ni:Plot}">
                                    <StackPanel Orientation="Horizontal">
                                        <niPrimitives:LegendGlyph Margin="3" 
                    Renderer="{Binding ActualRenderer}" 
                    Background="{Binding ItemBackground, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
                     Width="{Binding GlyphSize.Width, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}"
                     Height="{Binding GlyphSize.Height, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}" />

                                        <ContentPresenter Margin="3" 
                    VerticalAlignment="Center" 
                    Content="{Binding Tag}" />
                                    </StackPanel>
                                </DataTemplate>
                            </StackPanel.Resources>
                        <ni:Graph 
                    x:Name="graph"
                    DataSource="{Binding chartCollection}" 
                    RenderMode="Vector" 
                    PreferIndexData="True"
                    SuppressScaleLayout="True"
                   nix:GraphExtensions.PlotsSource="{Binding Plots2}"
                   nix:GraphExtensions.AxesSource="{Binding GraphAxes}">
                            <ni:Graph.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Konfiguration" Command="{Binding ShowConfigurationCommand}" >
                                    </MenuItem>
                                    <MenuItem Header="Entfernen" Command="{Binding RemoveMeCommand}" >
                                    </MenuItem>
                                    <MenuItem Header="Ebene nach vorne" Command="{Binding MoveOneLayerUpCommand}" >
                                    </MenuItem>
                                    <MenuItem Header="Ebene nach hinten" Command="{Binding MoveOneLayerDownCommand}" >
                                    </MenuItem>
                                </ContextMenu>
                            </ni:Graph.ContextMenu>
                        </ni:Graph>
                        <ni:Legend    x:Name="legend" ItemsSource="{Binding ElementName=graph, Path=Plots}" Margin="0,0,5,0" Padding="5"
                   Background="Transparent" BorderThickness="0">
                        </ni:Legend>
                        </StackPanel>

 and i got the same Result.

Legend.png

0 Kudos
Message 5 of 10
(6,474 Views)

From the screenshot, it looks like the custom data template is being used. You can confirm this by changing the {Binding Tag} in the data template to {Binding Tag, TargetNullValue=NULL}, or setting the background on the stack panel in the data template, to confirm that updating the data template updates the legend items.

~ Paul H
0 Kudos
Message 6 of 10
(6,467 Views)

hi Paul,

i cannot get it working. Please see the Demo-Projekt. It shows always NULL if i do this.

 

0 Kudos
Message 7 of 10
(6,461 Views)

Since the legend now shows the "NULL" fallback value, that indicates the custom data template is being used, but the value of Tag is always null.


With the custom data template, you can set the Tag property on your plots just as you would assign the Text property on legend items in Windows Forms. For example, in the Model.cs file in your attached project, if you add p.Tag = "test value"; after creating the plot in the GraphControl constructor (line 155), you will see that value instead of "NULL" appear in the legend.

~ Paul H
0 Kudos
Message 8 of 10
(6,452 Views)

You mentioned the the current values display in the Winforms legend.  How did you get that to work.  I didn't find any option for that.  I'm using Measurement Studio 2010.  Was that a new addition to a later version?

0 Kudos
Message 9 of 10
(6,399 Views)

There is no concept of a "current value" in the WPF or Windows Forms graphs. My understanding from the screenshot was that the Text property on the Windows Forms LegendItem was being used to reflect the application's concept of "current value".

~ Paul H
0 Kudos
Message 10 of 10
(6,396 Views)