using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Telerik.Charting;

namespace RadControlsWebApp1 {
	public partial class Chart : System.Web.UI.Page {
		private int stepCount = 10;
		private int xSteps = 8;

		protected void Page_Load(object sender, EventArgs e) {
			if (!IsPostBack) {
				ChartSeriesItem[] items = GetItems(20);
				RadChart1.Series[0].Items.AddRange(items);
				RadChart1.DataBind();
				double minX = RadChart1.PlotArea.XAxis.MinValue = items[0].XValue;
				double maxX = RadChart1.PlotArea.XAxis.MaxValue = items[items.Length - 1].XValue;

				double stepSize = (maxX - minX) / stepCount;
				RadSliderItem[] ticks = new RadSliderItem[stepCount + 1];
				for (int i = 0; i <= stepCount; i++) {
					double val = minX + stepSize * i;
					ticks[i] = new RadSliderItem();
					ticks[i].Value = val.ToString();
					ticks[i].Text = DateTime.FromOADate(val).ToString("MMM yy");
					ticks[i].Style[HtmlTextWriterStyle.PaddingTop] = "10px";
				}
				RadSlider1.Items.AddRange(ticks);
				RadSlider1.SelectionEnd = stepCount;
				RadSlider1.SelectionStart = 0;

				SetZoom(sender, e);
			}
		}

		private ChartSeriesItem[] GetItems(int count) {
			DateTime startDate = new DateTime(2000, 1, 1);
			ChartSeriesItem[] items = new ChartSeriesItem[count];
			for (int i = 0; i < count; i++)
				items[i] = new ChartSeriesItem(startDate.AddDays(i * 30).ToOADate(), i);

			return items;
		}

		protected void ResetButton_Click(object sender, EventArgs e) {
			RadSlider1.SelectionStart = 0;
			RadSlider1.SelectionEnd = stepCount;
			SetZoom(sender, e);
		}

		protected void SetZoom(object sender, EventArgs e) {
			decimal start = RadSlider1.SelectionStart;
			decimal end = RadSlider1.SelectionEnd;
			if (start == end) {
				if (start == 0)
					end = RadSlider1.SelectionEnd = 1;
				else
					start = RadSlider1.SelectionStart = end - 1;
			}

			var ticks = RadSlider1.Items;
			double maxX = double.Parse(ticks[(int)end].Value);
			double minX = double.Parse(ticks[(int)start].Value);

			RadChart1.PlotArea.XAxis.Step = (maxX - minX) / xSteps;

			RadChart1.ClientSettings.XScale = stepCount / (float)(end - start);
			RadChart1.ClientSettings.XScrollOffset = (float)start / stepCount;
		}

	}
}