Capturing the DataPoint which triggered a mouse event in a Stacked BarChart

2 posts, 0 answers
  1. Tanja
    Tanja avatar
    2 posts
    Member since:
    Mar 2013

    Posted 14 Nov 2013 Link to this post

    Hi,

    We are trying to display data for a particular DataPoint which triggered a mouse event in a label but we are not having too much luck with it as we keep getting the entire BarSeries, not the actual DataPoint, passed into the event.

    Here is a recap of what we are trying to achieve:

    - a Stacked BarChart has two bar series ("Events" and "NonEvents" in the example) which are populated through a binding.
    - there are three categories displayed: "G1", "G2" and "G3".
    - on mouse over event of "G3"  bar we would like to display text "G3" on a label which is not a part of the chart.

    Any ideas on this?

    Best regards,
    Tanja Risovic


    Code:

      public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                
                var dataModel = new List<ViewDataModel>();

                var dataModel1 = new ViewDataModel();
                dataModel1.Events = 151;
                dataModel1.NonEvents = 24;
                dataModel1.GroupID = "1";
                dataModel1.GroupLabel = "G1";
               

                dataModel.Add(dataModel1);


                var dataModel2 = new ViewDataModel();
                dataModel2.Events = 4132;
                dataModel2.NonEvents = 529;
                dataModel2.GroupID = "2";
                dataModel2.GroupLabel = "G2";
              

                dataModel.Add(dataModel2);

                var dataModel3 = new ViewDataModel();
                dataModel3.Events = 1832;
                dataModel3.NonEvents = 109;
                dataModel3.GroupID = "3";
                dataModel3.GroupLabel = "G3";
               

                dataModel.Add(dataModel3);

                BarChartPercentage.DataContext = dataModel;         

                        }

            private void BarSeries_MouseEnter(object sender, MouseEventArgs e)
            {

                this.label.Content = ???; //We would like to show the GroupLabel of the DataPoint which triggered the mouse event, for example "G3"
          
            }

        }
  2. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 19 Nov 2013 Link to this post

    Hi Tanja,

    There are a few options you have. One is to set a PointTemplate for the bars of the series and set an element in the template with a MouseEnter handler. Another option is to use the series' MouseEnter and find the bar underneath the mouse by hit testing:
    var result = VisualTreeHelper.HitTest((Visual)sender, e.GetPosition(null));
    var bar = result.VisualHit as Border;
    var dp = bar.DataContext as CategoricalDataPoint;

    I hope this helps.

    Regards,
    Petar Marchev
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top