Hi,
Is there a way to hide/show data series on a WPF XamChart?
The best would be a checkbox for each series on the Legend. I can do that with a LegendItemTemplate, but how can I connect the checkbox to a series (could hide it by setting the Fill to transparent)?
Thanks in advance,
Szilveszter
Hi Szilvester,
There are two ways to show/hide the series in the XamChart :
1. Setting the Fill property of each Series to Transparent (also the StrokeThickness to 0 so you can remove the outer border) .
2. Manually add/remove the Series objects from the XamChart.Series collection.
Regarding the first option If you use a checkboxes outside the chart you can easily bind the Fill property of the Series to the IsChecked property of the checkbox using converter. The binding is possible because the elements are in the VisualTree so you can use ElementName in the binding syntax e.g.
<Series1" StrokeThickness="0" Fill="{Binding ElementName=check1,Path=IsChecked,Converter={StaticResource customConverter}, ConverterParameter=Green}">
However in your case the checkboxes are in a DataTemplate so your best option is to handle the Checked/Unchecked events, get the Series and set the Fill property in codebehind. Please note that when the Fill property changes the XamChart updates the layout which can probably retrieve the current status of the checkboxes and cause some issues. You can take a look at my sample project showing the both scenarios.
Let me know if you have any questions with this matter.
Vlad
This sample doesn't seem to work well if I am using PRISM. I tried pulling the checkboxes and the chart into a usercontrol and defined a view region in my MainWindow (I collapsed the visibility of the Legend). I register the view into the region, but when the control gets loaded the checkboxes don't work. I get Binding Expression errors as well eg.
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=check1'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'Fill'
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=check2'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'Fill'
Any idea?
Hi Vlad,
In my use-case I get a XamChart already bound to a datasource that I don't know anything about, so the second solution (adding/removing the series) is not a viable option.
I wanted to preserv the Legend colors when the series are reshown, but couldn't get hold of the original color. The Fill property of the Series is null, the Legend.Items[] collection is empty when I get the checked event from the checkbox. The only place I can get the color from is the Fill property in the LegendItemTemplate, but that changes to whatever I change the series.Fill (Transparent here). I also tried to grab that color by assigning it to an attached property on the checkbox with a OneTime binding, but unfortunately when I change the color, the whole chart gets rerendered, including the Legend, therefore the checkboxes get recreated.
One solution could be to define my own palette for the legend and set it explicitly, then I will know the color of each series when I need to reshow it. But I don't want to modify the default palette.
I have an idea. The Series is a DependencyObject, I can attach a property to it, and store the color there, but I can't get hold of the corresponding series object from the datatemplate/checkbox, only in the codebehind, which is triggered when the user clicks the checkbox. So I need to check the Fill color of the template and if it is not transparent store it in an attached property on the series, then next time I can restore it. I'm going to give this a go..
Let me know if you have a better idea.
Thanks,