Using RadCartesianChart events

2 posts, 0 answers
  1. Alberto
    Alberto avatar
    26 posts
    Member since:
    Nov 2012

    Posted 10 Oct 2013 Link to this post

    Hi,

    I have a chart like the one in the attached image, this is the code:

    <telerik:RadCartesianChart Name="CartesianChartDemo" Margin="10">
                <telerik:RadCartesianChart.Behaviors>
                    <telerik:ChartPanAndZoomBehavior PanMode="Both" ZoomMode="Both"/>
                </telerik:RadCartesianChart.Behaviors>
            </telerik:RadCartesianChart>

    CartesianChartDemo.VerticalAxis = new LinearAxis();
    CartesianChartDemo.HorizontalAxis = new CategoricalAxis();
     
    CartesianChartDemo.Grid = new CartesianChartGrid() { MajorLinesVisibility = GridLineVisibility.XY };
     
    DataTable dtAlloys = new DataTable("DATA");
     
    dtAlloys.Columns.Add(new DataColumn("Name", typeof(string)));
    dtAlloys.Columns.Add(new DataColumn("Value1", typeof(double)));
    dtAlloys.Columns.Add(new DataColumn("Value2", typeof(double)));
     
    dtAlloys.Rows.Add(new object[] { "Value1", 70, 30 });
    dtAlloys.Rows.Add(new object[] { "Value2", 15, 85 });
    dtAlloys.Rows.Add(new object[] { "Value3", 50, 50 });
     
    BarSeries barSer = new BarSeries();
    barSer.ShowLabels = true;
    barSer.CombineMode = ChartSeriesCombineMode.Stack100;
     
    BarSeries barSer2 = new BarSeries();
    barSer2.ShowLabels = true;
    barSer2.CombineMode = ChartSeriesCombineMode.Stack100;
     
    foreach (DataRow drAlloy in dtAlloys.Rows)
    {
        barSer.DataPoints.Add(new CategoricalDataPoint() { Category = drAlloy["Name"], Label = string.Format("{0:N}", drAlloy["Value1"]), Value = double.Parse(drAlloy["Value1"].ToString()) });
        barSer2.DataPoints.Add(new CategoricalDataPoint() { Category = drAlloy["Name"], Label = string.Format("{0:N}", drAlloy["Value2"]), Value = double.Parse(drAlloy["Value2"].ToString()) });
        
    }
     
    CartesianChartDemo.Series.Clear();
    CartesianChartDemo.Series.Add(barSer);
    CartesianChartDemo.Series.Add(barSer2);

    I am trying to use the MouseDown event to do something like show an alert with bar info or open a new window and pass it data from that bar that I am clicking.

    How can I obtain the data of the bar that I am clicking?

    It is ok to use that event or should I use another one?

    Thanks,

    Alberto



  2. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 16 Oct 2013 Link to this post

    Hi Alberto,

    By default most chart elements do not receive mouse events because their property IsHitTestVisible is set to false. There are two ways you approach the problem - you can either attach to a mouse event for the whole series like this:
    barSer2.IsHitTestVisible = true;
    barSer2.MouseDown += barSer2_MouseDown;
     
    void barSer2_MouseDown(object sender, MouseButtonEventArgs e)
    {
        MessageBox.Show("barSer2 was clicked!");
    }

    Or you can use the built-in ChartSelectionBehavior (which automatically enables the hit test visibility of the series) to click on individual data points. You can find more information on the matter in this article.

    Regards,
    Petar Kirov
    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