01-11-2014 05:53 AM
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.
01-13-2014 09:51 AM
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.
01-13-2014 10:46 AM
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.
01-13-2014 04:21 PM
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;
09-22-2019 10:17 PM - edited 09-22-2019 10:23 PM
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?
09-23-2019 10:20 AM
(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).