Legend with grouped series

A forum dedicated to WPF version of LightningChart Ultimate.

Legend with grouped series

Postby giryhepai » Thu May 16, 2019 10:30 am

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?

Regards,
giryhepai
giryhepai
 
Posts: 3
Joined: Thu May 16, 2019 9:43 am

Re: Legend with grouped series

Postby Arction_LasseP » Thu May 16, 2019 12:42 pm

Hello,

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;
_chart.ViewXY.FreeformPointLineSeries.Add(greens);


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)
        {
            _chart.BeginUpdate();

            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;
            }
            _chart.EndUpdate();
        }


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.
Arction_LasseP
 
Posts: 15
Joined: Wed Mar 27, 2019 1:05 pm

Re: Legend with grouped series

Postby giryhepai » Thu May 16, 2019 2:20 pm

Hi LasseP,

This was exactly what I was looking for.

Worked like a charm!

Thanks a bunch,
giryhepai
giryhepai
 
Posts: 3
Joined: Thu May 16, 2019 9:43 am


Return to LightningChart Ultimate WPF

Who is online

Users browsing this forum: No registered users and 7 guests

cron