Legend and annotation tooltips

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Arction_LasseP

lukoprovandrey
Posts: 29
Joined: Fri Sep 04, 2015 2:30 pm

Legend and annotation tooltips

Post by lukoprovandrey » Wed May 20, 2020 12:33 pm

Hello,

I have to show tooltip to the user when he hovers a legend or annotation on the chart, but I have no idea if it possible to do?
I tried to handle MouseOverOn/Off events for [sampleDataSeries]. Title object, but it looks like it doesn't work.

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

Re: Legend and annotation tooltips

Post by Arction_LasseP » Mon May 25, 2020 7:49 am

Hello,

It is definitely possible to show a tooltip when hovering mouse over a chart object. This is true for pretty much all chart objects such as all series types, axes, Annotations etc. Usually an Annotation is used for showing the tooltip but in some cases LegendBox can be handy as well (tracking multiple series, having texts with different fonts and colors).

MouseOverOn/Off events are most likely the most useful here. The basic method to implement a tooltip is to have an Annotation whose Visible -property is being modified. In MouseOverOn -event enable Visible and possibly update the Annotation text, and in MouseOverOff you disable Visible. This same method applies to all chart objects, just note that MouseInteraction has to be enabled for the object (in this case SampleDataSeries). Here is a small example:

Code: Select all

_chart.BeginUpdate();

            _chart.ViewXY.LegendBoxes[0].Visible = false;

            Random rnd = new Random();

            SampleDataSeries sds = new SampleDataSeries(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
            sds.LineStyle.Color = Colors.Red;
            sds.SampleFormat = SampleFormat.DoubleFloat;
            sds.SamplingFrequency = 1;
            sds.FirstSampleTimeStamp = 0;
            double[] values = new double[11];
            for (int i = 0; i < 11; i++)
            {
                values[i] = rnd.NextDouble() * 6 + 2;
            }
            sds.AddSamples(values, false);
            sds.MouseOverOn += Sds_MouseOverOn;
            sds.MouseOverOff += Sds_MouseOverOff;
            _chart.ViewXY.SampleDataSeries.Add(sds);

            SampleDataSeries sds2 = new SampleDataSeries(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
            sds2.LineStyle.Color = Colors.Lime;
            sds2.SampleFormat = SampleFormat.DoubleFloat;
            sds2.SamplingFrequency = 1;
            sds2.FirstSampleTimeStamp = 0;
            double[] values2 = new double[11];
            for (int j = 0; j < 11; j++)
            {
                values2[j] = rnd.NextDouble() * 6 + 2;
            }
            sds2.AddSamples(values2, false);
            sds2.MouseOverOn += Sds_MouseOverOn;
            sds2.MouseOverOff += Sds_MouseOverOff;
            _chart.ViewXY.SampleDataSeries.Add(sds2);

            AnnotationXY tooltip = new AnnotationXY(_chart.ViewXY, _chart.ViewXY.XAxes[0], _chart.ViewXY.YAxes[0]);
            tooltip.Visible = false;
            tooltip.LocationCoordinateSystem = CoordinateSystem.ScreenCoordinates;
            tooltip.MouseInteraction = false;
            tooltip.Text = "";
            tooltip.Style = AnnotationStyle.Rectangle;
            _chart.ViewXY.Annotations.Add(tooltip);

            _chart.EndUpdate();
            
            
 private void Sds_MouseOverOff(object sender, MouseEventArgs e)
        {
            _chart.ViewXY.Annotations[0].Visible = false;
        }

private void Sds_MouseOverOn(object sender, MouseEventArgs e)
        {
            _chart.BeginUpdate();

            _chart.ViewXY.Annotations[0].Visible = true;
            _chart.ViewXY.Annotations[0].LocationScreenCoords.SetValues((float)e.GetPosition(_chart).X, (float)e.GetPosition(_chart).Y);
            // Show the current axis values in the annotation
            double xVal = 0, yVal = 0;
            _chart.ViewXY.XAxes[0].CoordToValue((int)e.GetPosition(_chart).X, out xVal, true);
            _chart.ViewXY.YAxes[0].CoordToValue((int)e.GetPosition(_chart).Y, out yVal, true);
            _chart.ViewXY.Annotations[0].Text = "X: " + xVal.ToString("0.00") + "\nY: " + yVal.ToString("0.00");

            _chart.EndUpdate();
        }
Hope this is helpful.
Best regards,
Lasse

lukoprovandrey
Posts: 29
Joined: Fri Sep 04, 2015 2:30 pm

Re: Legend and annotation tooltips

Post by lukoprovandrey » Tue May 26, 2020 1:31 pm

Thank, I forget to set mouse interaction to true.

One more question:
How can I determine using mouse coordinates what is the title hovered in the current time in the legend box?

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

Re: Legend and annotation tooltips

Post by Arction_LasseP » Wed May 27, 2020 11:15 am

Hello,

We actually have separate events to use when moving mouse over a series title in the LegendBox. SeriesTitleMouseMoveOverOn/Off events for the LegendBox also give information about the series the mouse is currently over.

Code: Select all

_chart.ViewXY.LegendBoxes[0].SeriesTitleMouseMoveOverOn += Example_SeriesTitleMouseMoveOverOn;
_chart.ViewXY.LegendBoxes[0].SeriesTitleMouseMoveOverOff += Example_SeriesTitleMouseMoveOverOff;


private void Example_SeriesTitleMouseMoveOverOff(object sender, SeriesTitleMouseMovedEventArgs e)
{
    (e.Series as SampleDataSeries).LineStyle.Color = Colors.Red;
}

private void Example_SeriesTitleMouseMoveOverOn(object sender, SeriesTitleMouseMovedEventArgs e)
{
    (e.Series as SampleDataSeries).LineStyle.Color = Colors.Green;
}
In the above example, e.Series gets the current series though it doesn't automatically detect the series type. You can of course update the Annotation tooltip as well inside these events if needed.

If you need to get the mouse coordinates inside these events, you can use:
Point mousePos = Mouse.GetPosition(_chart); (in WPF)
Point mousePos = PointToClient(MousePosition); (in WinForms)

This gives you the mouse position in screen coordinates.

Hope this helps.
Kind regards,
Lasse