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
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,
Did you find any resolution to this? I've tried a couple more options unfortunatelly to no avail. The main purpose of the Legend object is just to present some data related to the chart. Therefore the Legend will update each time you are changing the Fill of a DataPoint. So I don't think it is a good idea to use a checkbox in the template because it will return each original state each time the chart is refreshing. Instead I think it is better to place the checkboxes outside the grid.