Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

WPF Plot Color

Hi,

 

I will just like to know if there is way to get (and even set) the color of WPF plot? I have looked through all the properties of the plots and can't seem to find a suitable one that will give the colour.

 

Thanks.

0 Kudos
Message 1 of 6
(6,905 Views)

The display of a Plot is controlled by the Renderer property. For example, you can set the Stroke on a LinePlotRenderer to display a plot with a line of a specific color:


    <ni:Plot>
        <ni:LinePlotRenderer Stroke="Green" />
    </ni:Plot>


You can also use the DefaultPlotRenderers collection to specify renderers for plots without declaring the plots individually.

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

Yes I know I can do that but wouldn't it be useful if I can get the color of the graph after plotting for instance if there is another control that need to have the same color (in my case another plot). I am currently setting the color as per your suggestion but need to get that color and use it for another control in the program. Thanks.

0 Kudos
Message 3 of 6
(6,831 Views)

I am not quite sure what your scenario is, but Stroke is a dependency property, so you could use a binding to tie the it to another control. You could do so directly, by giving the renderer a name:


    <ni:LinePlotRenderer x:Name="renderer" Stroke="Green" />
    ...
    <TextBlock Text="Another control" Foreground="{Binding ElementName=renderer, Path=Stroke}" />


Or you could use a relative path to get to a particular plot:


    <TextBlock Text="Another control" Foreground="{Binding ElementName=graph, Path=Plots[0].Renderer.Stroke}" />


Or you could access it in code:


    var renderer = (LinePlotRenderer)graph.Plots[0].Renderer;
    anotherControl.Foreground = renderer.Stroke;

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

hello paul. In my code, i set the plot color like this:

 

            var renderGroup = new PlotRendererGroup();
            renderGroup.PlotRenderers.Add(
                new LinePlotRenderer()
                {
                    Stroke = brush,
                    StrokeThickness = plotWidth[graphIdx],
                }
            );
            plot.Renderer = renderGroup;

 

And my legend DataTemplate as follow:

<DataTemplate DataType="{x:Type ni:Plot}">
            <StackPanel Orientation="Horizontal">
                <ToggleButton Margin="3" 
                              IsChecked="{Binding Visibility, Converter={StaticResource VisibilityToBoolean}}"
                              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}}">
                    <niPrimitives:LegendGlyph Renderer="{Binding ActualRenderer}" 
                                              Background="{Binding ItemBackground, RelativeSource={RelativeSource AncestorType={x:Type ni:Legend}, Mode=FindAncestor}}" />
                </ToggleButton>
                <TextBlock Margin="3" 
                           VerticalAlignment="Center" 
                           Opacity="{Binding Visibility, Converter={StaticResource VisibilityToOpacity}}"
                           Text="{Binding Label}">
                </TextBlock>
            </StackPanel>
        </DataTemplate>

How can i binding the plot color to TextBlock foreground in legend?

0 Kudos
Message 5 of 6
(3,073 Views)

(For future reference, it is usually better to create a new question and link to another one, rather than trying to resurrect an old question : )

 

In your code, it looks like you are adding the LinePlotRenderer as the first element in the PlotRendererGroup. As the plot is the data context for your template, you can use a relative path (similar to the example above) to access the sub-property in a XAML binding:

 

Foreground="{Binding Renderer.PlotRenderers[0].Stroke}"

 

If the line renderer is not always the first, then you would need to use a converter to extract the appropriate value (e.g. bind to the renderer group, and search for the line renderer in the PlotRenderers collection).

~ Paul H
0 Kudos
Message 6 of 6
(3,056 Views)