using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.Charting;
using Telerik.WinControls.UI;

namespace RadChartview___Resize
{
    public partial class Form1 : Form
    {
        DataTable table;

        public Form1()
        {
            InitializeComponent();

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            table = new DataTable();
            table.Columns.Add("Value", typeof(double));
            table.Columns.Add("Name", typeof(string));
            table.Rows.Add(1, "A");
            table.Rows.Add(2, "B");
            table.Rows.Add(3, "C");
            table.Rows.Add(4, "D");
            table.Rows.Add(5, "E");

            LineSeries lineSeria = new LineSeries();
            sChartView.Series.Add(lineSeria);
            lineSeria.ValueMember = "Value";
            lineSeria.CategoryMember = "Name";
            lineSeria.DataSource = table;

            InitializeAnnotations();

            sChartView.SizeChanged += SChartView_SizeChanged;
        }

        /// <summary>
        /// SizeChanged of RadChartView
        /// </summary>
        private void SChartView_SizeChanged(object sender, EventArgs e)
        {
            if (GetOrdonnee() != null)
            {
                UpdateTickGraph();
            }
        }

        /// <summary>
        /// Update numbers of ticks depending of the RadChartView size
        /// </summary>
        private void UpdateTickGraph()
        {
            var axeY = GetOrdonnee();

            if (axeY == null)
                return;

            var hauteurGraphique = sChartView.ChartElement.Wrapper.Size.Height - 100;

            if (hauteurGraphique < 200)
                axeY.DesiredTickCount = 4;
            else if (hauteurGraphique < 400)
                axeY.DesiredTickCount = 5;
            else if (hauteurGraphique < 600)
                axeY.DesiredTickCount = 6;
            else if (hauteurGraphique < 800)
                axeY.DesiredTickCount = 7;
            else
                axeY.DesiredTickCount = 8;

            InitializeAnnotations();
        }

        /// <summary>
        /// Create annotation for each Y axis tick
        /// </summary>
        protected void InitializeAnnotations()
        {
            sChartView.Annotations.Clear();

            ///The issue is here : 
            /// ActualRange seems not updated when the SizeChanged was fired due to the double click on the form
            /// ActualRange is updated when we are resizing the form with the mouse drag

            var axeMin = (decimal)GetOrdonnee().ActualRange.Minimum;
            var axeMax = (decimal)GetOrdonnee().ActualRange.Maximum;
            var step = (decimal)GetOrdonnee().MajorStep;

            for (decimal compteur = axeMin; compteur <= axeMax; compteur = compteur + step)
            {
                CreateAnnotation(compteur);
            }
        }

        /// <summary>
        /// Return the Y axis if valid
        /// </summary>
        protected LinearAxis GetOrdonnee()
        {
            if (sChartView.Axes.Count == 0)
            {
                return null;
            }
            return GetLinearAxe(sChartView.Axes[1]);
        }

        /// <summary>
        /// Cast the ChartAxis into LinearAxis
        /// </summary>
        protected LinearAxis GetLinearAxe(Axis pAxe)
        {
            if (sChartView.AreaType == ChartAreaType.Cartesian && pAxe != null && pAxe is LinearAxis vAxe)
            {
                return vAxe;
            }
            return null;
        }

        /// <summary>
        /// Create annotation with the desired value
        /// </summary>
        protected void CreateAnnotation(decimal pValue)
        {
            CartesianGridLineAnnotation annotation1 = new CartesianGridLineAnnotation();
            annotation1.Axis = sChartView.Axes[1] as CartesianAxis;
            annotation1.Value = pValue;
            annotation1.BorderColor = Color.Red;
            annotation1.BorderDashStyle = DashStyle.Solid;
            annotation1.BorderWidth = 1;
            sChartView.Annotations.Add(annotation1);
        }
    }
}
