Detecing which value in a bar series is selected.

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
cwodarczyk82
Posts: 43
Joined: Mon Oct 19, 2015 2:50 am

Detecing which value in a bar series is selected.

Post by cwodarczyk82 » Tue Oct 20, 2015 2:30 am

How could I implement the following:
1) Let's say I have three bar series, and two values in each series...
2) I would like to place a hover text tooltip when over a specific bar to show its value

I know you can place a callback when the mouse enters the series, but I need to know which value in the series I am in..
I've tried to use e.GetPosition(Chart), and then using this value to convert this into the value coordinate on the xaxis (CoordToValue())... however, this isn't enough information... I really need to know the entire bounding box geometry for the bar graph so I detect if a point is within this bounding box... I tried manually calculating the width of each bar in the graph, and then doing a +/- .5 of this width, and this works fine until I get more than about 20 bars on the screen, and it no longer works.

I also considered using a SeriesEventMarker, but I would have to make the width/height of the marker the same as the bar on the graph, but I don't know how to get that information...

Maybe there is an easier way to do this?

cwodarczyk82
Posts: 43
Joined: Mon Oct 19, 2015 2:50 am

Re: Detecing which value in a bar series is selected.

Post by cwodarczyk82 » Tue Oct 20, 2015 2:29 pm

Just as a reference, here is where I am with this currently as a visual:
BarSeriesAnnotation.png
BarSeriesAnnotation.png (15.85 KiB) Viewed 11026 times
And here is the code i cobbled together from various other examples:

Code: Select all

            var series = (BarSeries) sender;
            
            // Get the current mouse position in screen coordinates....
            var screenPosition = e.GetPosition(m_Chart.Chart);

            var tickValue = 0.0;
            // Convert this to the x value in the xy axis
            m_Chart.Chart.ViewXY.XAxes[0].CoordToValue((int)screenPosition.X, out tickValue, false);

            Int32 iBarWidth = (Int32)(m_Chart.Chart.ActualWidth -
                (m_Chart.Chart.ViewXY.Margins.Left + m_Chart.Chart.ViewXY.Margins.Right));
            iBarWidth -= m_Chart.Chart.ViewXY.BarViewOptions.BarSpacing * (m_Chart.Chart.ViewXY.BarSeries.Count - 1);
            iBarWidth /= m_Chart.Chart.ViewXY.BarSeries.Count;

            var found = false;
            var tagString = string.Empty;
            var barValue = 0.0;
            foreach (var item in series.Values.
                Where(item => (item.Location - iBarWidth/2.0) <= tickValue && (item.Location + iBarWidth/2.0) >= tickValue))
            {
                found = true;
                tagString = "Dummy text";//(string)item.Tag;
                barValue = item.Value;
                break;
            }
            // Find the item in this bar series matching this location
            if(found)
            {
                m_DataAnnotationToolTip.TargetAxisValues.X = tickValue;
                m_DataAnnotationToolTip.TargetAxisValues.Y = barValue;
                m_DataAnnotationToolTip.Text = tagString;

                var yMin = m_Chart.Chart.ViewXY.YAxes[0].Minimum;
                var yMax = m_Chart.Chart.ViewXY.YAxes[0].Maximum;

                var midPoint = Math.Abs(yMax + yMin)/2.0;

                if (yMax > midPoint)
                {
                    m_DataAnnotationToolTip.LocationRelativeOffset.Y = 40;
                    m_DataAnnotationToolTip.LocationRelativeOffset.X = 10;
                }
                else
                {
                    m_DataAnnotationToolTip.LocationRelativeOffset.X = 10;
                    m_DataAnnotationToolTip.LocationRelativeOffset.Y = -60;
                }

                m_DataAnnotationToolTip.Visible = true;
However, as I stated before, beyond around 70 bar graphs this no longer seems to work... i must not be precise enough, but not sure how to get more precise...

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: Detecing which value in a bar series is selected.

Post by ArctionPasi » Tue Oct 20, 2015 4:18 pm

I just added an improved IsMouseOver method for BarSeries. It now gives the data item index out.

Code: Select all

void _chart_MouseMove(object sender, MouseEventArgs e)
        {
            foreach (BarSeries bs in _chart.ViewXY.BarSeries)
            {
                Point p = e.GetPosition(_chart);
                int itemIndex = 0; 
                if(bs.IsMouseOver((int)p.X, (int)p.Y, out itemIndex))
                {
                    double value = bs.Values[itemIndex].Value;

                    MessageBox.Show(value.ToString("0.0"));
                }
            }
        }

I also added a method for getting the rendered bar coordinates.

Code: Select all

RectangleF[] rects = barSeries.GetBarRectangles();  
returns array of rectangles.

I hope these will help you get the result you need :)


These will be included in the next assembly pack in couple of weeks.
LightningChart Support Team, PT

cwodarczyk82
Posts: 43
Joined: Mon Oct 19, 2015 2:50 am

Re: Detecing which value in a bar series is selected.

Post by cwodarczyk82 » Wed Oct 21, 2015 2:08 pm

Thank you kindly! That will be amazing once released.. I will look forward to it...!

cwodarczyk82
Posts: 43
Joined: Mon Oct 19, 2015 2:50 am

Re: Detecing which value in a bar series is selected.

Post by cwodarczyk82 » Tue Dec 08, 2015 7:21 pm

HI, Pasi:

Was able to get to this part of our project finally, and everything seems to work perfectly... thank you for adding this support in 6.5.5!

--Chris

Post Reply