Keep legend bound to inside chart
Moderator: Queue Moderators
Keep legend bound to inside chart
Is there a way to restrict the legend position to prevent the user from dragging it outside the chart?
-
- Posts: 557
- Joined: Mon Mar 14, 2016 9:22 am
Re: Keep legend bound to inside chart
Hi,
You could prevent LegendBox movement altogether with
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:
Hope this helps.
You could prevent LegendBox movement altogether with
Code: Select all
_chart.ViewXY.LegendBoxes[0].MoveByMouse = false;
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;
}
}
Re: Keep legend bound to inside chart
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.
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.
-
- Posts: 557
- Joined: Mon Mar 14, 2016 9:22 am
Re: Keep legend bound to inside chart
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?
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?
Re: Keep legend bound to inside chart
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
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
-
- Posts: 557
- Joined: Mon Mar 14, 2016 9:22 am
Re: Keep legend bound to inside chart
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:
This operation introduce some blinking while dragging Legendbox next to margins. Therefore, I preferred MouseUp event.
Hope this helps.
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();
Hope this helps.