using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Use this so that we can turn off the effect when changing points
        private PointMark highlightedPointMark;

        public MainWindow()
        {
            InitializeComponent();

            RadChart1.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Inside;
            RadChart1.DefaultView.ChartArea.ItemClick += new EventHandler<ChartItemClickEventArgs>(ChartArea_ItemClick);

            // Create three data series to prove this works for multiple series
            Random rnd = new Random();

            for (int i = 0; i < 3; i++)
            {
                DataSeries ds = new DataSeries();
                ds.Definition = new LineSeriesDefinition();

                for (int x = 0; x < 10; x++)
                {
                    ds.Add(new DataPoint(rnd.Next(1, 10)));
                }

                RadChart1.DefaultView.ChartArea.DataSeries.Add(ds);
            }

        }

        private void ChartArea_ItemClick(object sender, ChartItemClickEventArgs e)
        {
            // Always clear state first, since we re-use highlightedPointMark
            this.ClearHighlightedState();

            // Grab all pointmarks within the RadChart default ChartArea
            var pointMarks = RadChart1.DefaultView.ChartArea
                .ChildrenOfType<PointMark>()
                .ToList<PointMark>();

            // Grab the pointmark that matches the selected item DataPoint
            PointMark pointMark1 = pointMarks.Where(x => x.DataContext == e.DataPoint).SingleOrDefault();

            PointMark pointMark2 = null;

            // If this is not the last pointmark, grab the next one by utilizing the dataseries associated
            //  with the click event as well as the point associated with the event
            if (e.ItemIndex < e.DataSeries.Count - 1)
            {
                pointMark2 = pointMarks.Where(x => x.DataContext == e.DataSeries[e.DataSeries.IndexOf(e.DataPoint) + 1]).SingleOrDefault();
            }

            // Grab points so we can do a mathematic comparison
            Point point1 = e.MouseData.GetPosition(pointMark1);
            Point point2 = e.MouseData.GetPosition(pointMark2);
            if (Math.Abs(point1.X) <= Math.Abs(point2.X))
                this.highlightedPointMark = pointMark1;
            else
                this.highlightedPointMark = pointMark2;

            // Highlight the winner :)
            this.HighlightPointMark();
        }

        /// <summary>
        /// Highlight the given pointmark with a new shape and fill color
        /// </summary>
        private void HighlightPointMark()
        {
            if (this.highlightedPointMark == null)
                return;

            this.highlightedPointMark.Fill = new SolidColorBrush(Colors.Red);
            this.highlightedPointMark.Shape = MarkerShape.Triangle;
        }

        /// <summary>
        /// Clear the highlighted pointmark back to the initial state (white fill with circle shape)
        /// </summary>
        private void ClearHighlightedState()
        {
            if (this.highlightedPointMark == null)
                return;

            this.highlightedPointMark.Fill = new SolidColorBrush(Colors.White);
            this.highlightedPointMark.Shape = MarkerShape.Circle;
        }
    }
}
