2X faster development
The ultimate WPF UI toolkit to boost your development speed.
This article describes how to make it possible for end-users to move a chart's legend at runtime.
To change the position of legend at any arbitrary location inside the chart area, set the DockPosition as Floating with subscribing the necessary mouse events to change their X and Y legend offset as per follows.
[XAML]
<chart:SfChart x:Name="sfChart" MouseMove="SfChart_MouseMove" MouseLeftButtonUp= "SfChart_MouseLeftButtonUp"> <chart:SfChart.Legend> <chart:ChartLegend x:Name="legend" DockPosition="Floating" MouseLeftButtonDown="Legend_MouseLeftButtonDown" OffsetX="200"/> </chart:SfChart.Legend> … </chart:SfChart>
[C#]
private bool isDragable = false; private void SfChart_MouseMove(object sender, MouseEventArgs e) { var chart = sender as SfChart; if (isDragable) { var position = e.GetPosition(chart); ChartLegend legend = chart.Legend as ChartLegend; legend.OffsetX = position.X - (chart.SeriesClipRect.Left + legend.DesiredSize.Width / 2); legend.OffsetY = position.Y - (chart.SeriesClipRect.Top + legend.DesiredSize.Height / 2); if ((chart.Legend as ChartLegend).IsMouseOver) { if (Mouse.OverrideCursor == null) Mouse.OverrideCursor = Cursors.Hand; } } } private void SfChart_MouseLeftButtonUP(object sender, MouseButtonEventArgs e) { isDragable = false; } private void Legend_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { isDragable = true; }
Output Download the complete sample here.
See Also
How to set or modify the label of each legend How to add multiple legend items in scroll viewer How to create custom legend items in WPF SfChart |
2X faster development
The ultimate WPF UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.