Legend position setting

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Queue Moderators

tingyunwang120
Posts: 8
Joined: Mon Jun 20, 2022 3:23 pm

Legend position setting

Post by tingyunwang120 » Mon Jun 20, 2022 4:01 pm

Hi,
Is there a way to define the position (Top/bottom/right/left) of the legend box rather than dragging it?

Arction_LasseP
Posts: 139
Joined: Wed Mar 27, 2019 1:05 pm

Re: Legend position setting

Post by Arction_LasseP » Tue Jun 21, 2022 6:54 am

Hello,

Yes there is. You can define the position of the Legend Box by setting its Position property to one of the pre-defined locations such as TopLeft, BottomRight or RightCenter, or to Manual, in which case you can use Offset to set its exact position. Offset is the screen coordinates relative to chart's top-left corner.

Code: Select all

_chart.ViewXY.LegendBoxes[0].Position = LegendBoxPositionXY.Manual;
_chart.ViewXY.LegendBoxes[0].Offset.SetValues(100, 100);
Best regards,
Lasse

tingyunwang120
Posts: 8
Joined: Mon Jun 20, 2022 3:23 pm

Re: Legend position setting

Post by tingyunwang120 » Tue Jun 21, 2022 10:02 am

thanks for your answer, sorry I think I didn't ask properly about my intention.
Is there anyway to have a docking function of the legend box and share it between different charts?

Arction_LasseP
Posts: 139
Joined: Wed Mar 27, 2019 1:05 pm

Re: Legend position setting

Post by Arction_LasseP » Tue Jun 21, 2022 11:20 am

Besides the automatic positioning options (BottomRight, RightCenter etc.) there isn't that kind of docking option ready. However, it is most likely possible to built that yourself using existing methods and properties. How this is done depends on where exactly you want to position the Legend. Try modifying Position and Offset properties to see if you can get the desired location. If you need to update the Legend's position when for example adding or resizing charts, you need to adjust those properties in some event such as chart.SizeChanged or chart.Loaded.

It should be noted that a Legend Box is tied to the chart it belongs to. Therefore in case of multiple charts, the Legend Box can only be on top of its chart, not over other charts. Also it can only display series that belong to that chart. If you want to have one legend box for multiple chart, it is possible to create empty series (series with no data points) as placeholders for the series in other charts.

Kind regards,
Lasse

tingyunwang120
Posts: 8
Joined: Mon Jun 20, 2022 3:23 pm

Re: Legend position setting

Post by tingyunwang120 » Wed Jun 22, 2022 10:25 am

Hi,
Thank you for your answer.
However, I don't quite understand "create an empty series as a placeholder for other series".
Do I put the legend in that empty series and somehow access data from other charts?

Arction_LasseP
Posts: 139
Joined: Wed Mar 27, 2019 1:05 pm

Re: Legend position setting

Post by Arction_LasseP » Wed Jun 22, 2022 10:48 am

Hi,

What I mean by empty series as placeholder is that if you want to show series that are not in the chart the Legend Box belongs to, you can create an empty series with the same Title as the series in other chart. The Legend Box automatically shows all series in the chart unless ShowInLegendBox property is disabled for the series. This allows you to have one Legend Box for multiple charts.

How this works in practice is:
-Whenever you create a series in some other chart, also create a series in the main chart (the one with the Legend)
-Give this series the same Title, and possibly LineColor, but no data points.
-Don't show the Legend Boxes in other charts.
-To apply interaction between the Legend Box empty series and the actual series in other charts, use events such as CheckBoxStateChanged, HighlightedStateChanged, or one of the SeriesTitleMouse events. Note that the SeriesTitleMouse-events do not work unless MoveFromSeriesTitle for the Legend Box is disabled.

A simple example code:

Code: Select all

_chart.ViewXY.LegendBoxes[0].CheckBoxStateChanged += LegendBox_CheckBoxStateChanged;

private void LegendBox_CheckBoxStateChanged(object sender, Arction.Wpf.Charting.Views.CheckBoxStateChangedEventArgs e)
{
    if (e.Series == emptySeries)
    {
        seriesInOtherChart.Visible = e.IsChecked;
    }
}
Best regards,
Lasse

tingyunwang120
Posts: 8
Joined: Mon Jun 20, 2022 3:23 pm

Re: Legend position setting

Post by tingyunwang120 » Tue Jun 28, 2022 4:41 pm

Thank you very much; this is very helpful.

However, while I was implementing, I met another problem.
For example, I have a colour map chart (IntensityMeshSeries) with the lowest value of 0, the highest value of 100 and a shared legend with 0 as blue and 100 as red.
I then created another colour map chart with the maximum value of 500; now, the legend is updated to be 500 as red, and 100 should be greenish.
But my first colour map chart's colour is not updated; Is there a way better than updating the new series to every existing chart every time a new chart is created?

Arction_LasseP
Posts: 139
Joined: Wed Mar 27, 2019 1:05 pm

Re: Legend position setting

Post by Arction_LasseP » Wed Jun 29, 2022 10:07 am

Hello,

If you have multiple series that use color palette, it is often easier to just use a separate palette for each series, at least if the color range is different. In other words, if the color range for one series is 0-100 and for other series 0-500, using one palette for both series might not be the best idea. Otherwise you would need to update the palette steps every time you add a new Intensity series. In this case, it would also be reasonable to show the palette in the chart where the IntensityGridSeries or IntensityMeshSeries is. That is show the palette in an own Legend Box next to the series it refers to.

Best regards,
Lasse

tingyunwang120
Posts: 8
Joined: Mon Jun 20, 2022 3:23 pm

Re: Legend position setting

Post by tingyunwang120 » Fri Jul 01, 2022 12:15 pm

Hi,

That make sense, thank you very much for your time and help, it is been very helpful.