Keep legend bound to inside chart

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Arction_LasseP

SESruss
Posts: 22
Joined: Wed Mar 12, 2014 6:36 pm

Keep legend bound to inside chart

Post by SESruss » Tue Jul 10, 2018 3:09 pm

Is there a way to restrict the legend position to prevent the user from dragging it outside the chart?

ArctionKestutis
Posts: 383
Joined: Mon Mar 14, 2016 9:22 am

Re: Keep legend bound to inside chart

Post by ArctionKestutis » Wed Jul 11, 2018 12:01 pm

Hi,

You could prevent LegendBox movement altogether with

Code: Select all

_chart.ViewXY.LegendBoxes[0].MoveByMouse = false;
If you want to restrict LegendBox's position inside some area, then use Offset property and some of mouse events. For example, following code will return LegendBox back to graph-area of chart on Mouse release:

Code: Select all

            _chart.ViewXY.LegendBoxes[0].MouseUp += Legend_MouseUp;

        private void Legend_MouseUp(object sender, MouseEventArgs e)
        {
            LegendBoxXY legendBox = (LegendBoxXY)sender;
            System.Windows.Rect marginsRect = _chart.ViewXY.GetMarginsRect();

            // DPI to PX conversion is required for WPF applications

            if ( legendBox.Offset.Y < DpiHelper.DipToPx(marginsRect.Top) )
            {
                legendBox.Offset.Y = (int)DpiHelper.DipToPx(marginsRect.Top);
            }
            else if ((legendBox.Offset.Y + legendBox.Height) > DpiHelper.DipToPx(marginsRect.Bottom) )
            {
                legendBox.Offset.Y = (int)DpiHelper.DipToPx(marginsRect.Bottom) - legendBox.Height;
            }

            if (legendBox.Offset.X < DpiHelper.DipToPx(marginsRect.Left))
            {
                legendBox.Offset.X = (int)DpiHelper.DipToPx(marginsRect.Left);
            }
            else if ((legendBox.Offset.X + legendBox.Width) > DpiHelper.DipToPx(marginsRect.Right))
            {
                legendBox.Offset.X = (int)DpiHelper.DipToPx(marginsRect.Right) - legendBox.Width;
            }

        }
Hope this helps.

SESruss
Posts: 22
Joined: Wed Mar 12, 2014 6:36 pm

Re: Keep legend bound to inside chart

Post by SESruss » Thu Jul 12, 2018 2:45 pm

This works if the user releases the mouse button with the mouse pointer in the chart. We are using binding for everything in the chart so in your example above I had to switch the changes to the legend box offset values to do these on the datacontext of the chart to get it to work.

Unfortunately it does not work if the legend is dragged and the mouse button is released outside the chart. I've been trying all sorts of things using other events but I cannot get it to work for this case.

ArctionKestutis
Posts: 383
Joined: Mon Mar 14, 2016 9:22 am

Re: Keep legend bound to inside chart

Post by ArctionKestutis » Fri Jul 13, 2018 1:05 pm

It works for me, at least with latest assemblies and non-bindable WPF. That is, if mouse was pressed above the Chart, then Chart continues to receive mouse event, even outside of it. You could subscribe to _chart.MouseUp event and read mouse cursor's coordinates in event handler.

Which version of LightningChart you are using and which edition/platform?
Could you send example of your project (e.g. to Support account), so we can better understand what kind of binding you have in mind and why this approach is not working for you?

SESruss
Posts: 22
Joined: Wed Mar 12, 2014 6:36 pm

Re: Keep legend bound to inside chart

Post by SESruss » Fri Jul 13, 2018 2:25 pm

I would like to correct an error in my last post. When using the event to alter the legend box offset I can alter it using either the chart itself or the charts viewmodel ViewXY.LegendBox.Offset X & Y properties.

When dragging the legend and the mouseup event happens with the cursor off the chart the event is still fired and the offset values are corrected. The legend itself does not move to these new offset values. I've looked at our code and there is nothing setting the offset X & Y properties anywhere. It seems like something behind the scenes in this case is immediately setting the offset values back to their pre-corrected values because as soon as the legend is dragged again just a little the offset X & Y values are the same as they were initially.

Version 7.1.3.4006

ArctionKestutis
Posts: 383
Joined: Mon Mar 14, 2016 9:22 am

Re: Keep legend bound to inside chart

Post by ArctionKestutis » Fri Jul 13, 2018 2:59 pm

Could you try with 8.3.1.4001 assemblies? In addition mention platform (WPF non-bindable, semibindable, fully-bindable)?

If you trying to modify Offset while dragging Legendbox, then it is slightly different thing than on MouseUp event. First, you would need to reset location and only then modify offset of LegendBox:

Code: Select all

legendBox.ResetLocation();
This operation introduce some blinking while dragging Legendbox next to margins. Therefore, I preferred MouseUp event.

Hope this helps.