Page 1 of 1

Legend with grouped series

PostPosted: Thu May 16, 2019 10:30 am
by giryhepai
Hi everyone,

I have a Chart with FreeformPointLineSeries.
My representation of data is like this:
Serie1: ColorCode1, XYCoordinates1
Serie2: ColorCode2, XYCoordinates2
Serie3: ColorCode3, XYCoordinates3
Serie4: ColorCode4, XYCoordinates4
Serie5: ColorCode1, XYCoordinates5
Serie6: ColorCode2, XYCoordinates6
Serie7: ColorCode3, XYCoordinates7
Serie8: ColorCode4, XYCoordinates8

My question is: is there any way I can group by ColorCode when displaying the legend?

I want all series to be displayed as individual Curves on the Chart, but I want the possibility to group by a criteria in the legend and also keep the link between the Legend items and Chart curves.
For the example above, the behavior I'm looking for is a Legend with 4 items (ColorCode1-4). When I Select only ColorCode1, on the Chart I can see Serie1 and Serie5.

Any ideas on how I could achieve this?


Re: Legend with grouped series

PostPosted: Thu May 16, 2019 12:42 pm
by Arction_LasseP

I think the easiest way to do this is to have an extra FreeformPointLineSeries with no points for each color code. Then you check inside CheckBoxStateChanged -event which color code series is turned on/off, and turn the visibility of the respective series true/false. Set ShowInLegendBox to false for the series which actually have some datapoints, in this case Serie1, Serie2 etc.

Code: Select all
Serie1.ShowInLegendBox = false;

To create blank series:

Code: Select all
FreeformPointLineSeries greens = new FreeformPointLineSeries(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
greens.Title.Text = "Greens";
greens.LineStyle.Color = Colors.Green;

Subscribing to the CheckBoxStateChanged -event, and what it contains:

Code: Select all
_chart.ViewXY.LegendBoxes[0].CheckBoxStateChanged += Legend_CheckBoxStateChanged;

private void Legend_CheckBoxStateChanged(object sender, Arction.Wpf.Charting.Views.CheckBoxStateChangedEventArgs e)

            var series = e.Series as FreeformPointLineSeries;
            Color color = series.LineStyle.Color;

            foreach (FreeformPointLineSeries fpls in _chart.ViewXY.FreeformPointLineSeries)
                if (fpls.LineStyle.Color == color) // Turn off all series having the same color
                    fpls.Visible = e.IsChecked;

Alternatively, you can check the series title inside the event and manually turn off the respective series. For example:

Code: Select all
if (series.Title.Text == "Greens")
                _chart.ViewXY.FreeformPointLineSeries[0].Visible = e.IsChecked;
                _chart.ViewXY.FreeformPointLineSeries[4].Visible = e.IsChecked;

Hope this is helpful.

Re: Legend with grouped series

PostPosted: Thu May 16, 2019 2:20 pm
by giryhepai
Hi LasseP,

This was exactly what I was looking for.

Worked like a charm!

Thanks a bunch,