using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Canvas_LineChart
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private C1.C1Flash.C1FlashCanvas c1FlashCanvas1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.c1FlashCanvas1 = new C1.C1Flash.C1FlashCanvas();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(120, 112);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(168, 32);
			this.button1.TabIndex = 0;
			this.button1.Text = "Click here";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(368, 40);
			this.label1.TabIndex = 1;
			this.label1.Text = "This sample demonstrates the basic methods of drawing lines using the C1FlashCanv" +
				"as class.";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(412, 186);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label1,
																		  this.button1});
			this.Name = "Form1";
			this.Text = "ComponentOne C1Flash Sample - Line Chart";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void LaunchViewer(string filename)
		{
			try
			{
				System.Diagnostics.Process.Start(filename);
			}
			catch (Exception e)
			{
				System.Diagnostics.Process.Start("IEXPLORE.EXE", filename);
			}
		}

		private string tempdir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.c1FlashCanvas1.Clear(Color.White);

			Rectangle clientRect = new Rectangle(0, 0, this.c1FlashCanvas1.Width, this.c1FlashCanvas1.Height);
			Rectangle chartRect = clientRect;
			chartRect.Inflate(-60, -60);

			// Draws the horizontal meter
			Rectangle rectMarker = new Rectangle(chartRect.Left, chartRect.Bottom - 30, chartRect.Width, 30);
			Point pt1 = new Point(rectMarker.Left, rectMarker.Bottom);
			Point pt2 = new Point(rectMarker.Right, rectMarker.Bottom);
			Pen pen = new Pen(Color.Red);
			this.c1FlashCanvas1.DrawLine(pen, pt1, pt2);

			int divisions = rectMarker.Width / 20;
			int x = rectMarker.Left + 20;
			int tenBarSize = rectMarker.Height / 4;
			int fiveBarSize = rectMarker.Height / 6;
			int oneBarSize = rectMarker.Height / 12;
			StringFormat sf = new StringFormat(StringFormat.GenericDefault);
			sf.Alignment = StringAlignment.Center;
			Font font = this.Font;
			for (int i = 1; i < divisions; i++)
			{
				int barsize = oneBarSize;
				int barwidth = 1;
				if (i % 10 == 0)
				{
					barsize = tenBarSize;
					barwidth = 2;

					Rectangle rectMark = new Rectangle(x - 20, rectMarker.Bottom + 2, 40, 40);
					this.c1FlashCanvas1.DrawString(i.ToString(), font, Brushes.Black, rectMark, sf);
				}
				else
				{
					if (i % 5 == 0)
						barsize = fiveBarSize;
				}

				pt1 = new Point(x, rectMarker.Bottom - barsize);
				pt2 = new Point(x, rectMarker.Bottom);

				pen.Width = barwidth;
				this.c1FlashCanvas1.DrawLine(pen, pt1, pt2);

				x += 20;
			}

			// Draws the vertical meter
			rectMarker = new Rectangle(chartRect.Left, chartRect.Top, 30, chartRect.Height);
			pt1 = new Point(rectMarker.Left, rectMarker.Top);
			pt2 = new Point(rectMarker.Left, rectMarker.Bottom);
			pen.Width = 1;
			this.c1FlashCanvas1.DrawLine(pen, pt1, pt2);

			sf.LineAlignment = StringAlignment.Center;
			divisions = rectMarker.Height / 10;
			int y = rectMarker.Bottom - 10;
			for (int i = 1; i < divisions; i++)
			{
				int barwidth = oneBarSize;
				int barsize = 1;
				if (i % 10 == 0)
				{
					barwidth = tenBarSize;
					barsize = 2;

					Rectangle rectMark = new Rectangle(rectMarker.Left - 40, y - 20, 40, 40);
					this.c1FlashCanvas1.DrawString(i.ToString(), font, Brushes.Black, rectMark, sf);
				}
				else
				{
					if (i % 5 == 0)
						barwidth = fiveBarSize;
				}

				pt1 = new Point(rectMarker.Left, y);
				pt2 = new Point(rectMarker.Left + barwidth, y);

				pen.Width = barsize;
				this.c1FlashCanvas1.DrawLine(pen, pt1, pt2);

				y -= 10;
			}

			// Draw the label
			Rectangle rectLabel = new Rectangle(200, 20, 160, 60);
			pen.Color = Color.Black;
			pen.Width = 1;
			this.c1FlashCanvas1.FillRectangle(Brushes.LightSkyBlue, rectLabel);
			this.c1FlashCanvas1.DrawRectangle(pen, rectLabel);

			rectLabel.Inflate(-10, -10);
			Rectangle rectBlue = rectLabel;
			rectBlue.Height = 10;

			Rectangle rectPanel = rectBlue;
			rectPanel.Width = 10;
			Font labelFont = new Font("System", 8);
			this.c1FlashCanvas1.FillRectangle(Brushes.Blue, rectPanel);
			this.c1FlashCanvas1.DrawString("Sample Line - 1", labelFont, Brushes.Black, new Point(rectPanel.Right + 5, rectPanel.Top - 2));

			Rectangle rectGreen = rectBlue;
			rectGreen.Offset(0, rectGreen.Height + 10);
			rectPanel = rectGreen;
			rectPanel.Width = 10;
			this.c1FlashCanvas1.FillRectangle(Brushes.LightGreen, rectPanel);
			this.c1FlashCanvas1.DrawString("Sample Line - 2", labelFont, Brushes.Black, new Point(rectPanel.Right + 5, rectPanel.Top - 2));

			pen.Width = 1;
			pen.Color = Color.Blue;
			Point[] points = new Point[6];
			points[0] = new Point(60, 320);
			points[1] = new Point(150, 260);
			points[2] = new Point(210, 300);
			points[3] = new Point(300, 120);
			points[4] = new Point(350, 230);
			points[5] = new Point(450, 120);
			this.c1FlashCanvas1.DrawLines(pen, points);

			pen.Width = 3;
			pen.Color = Color.LightGreen;
			points[0] = new Point(60, 120);
			points[1] = new Point(150, 240);
			points[2] = new Point(210, 180);
			points[3] = new Point(300, 260);
			points[4] = new Point(350, 260);
			points[5] = new Point(450, 280);
			this.c1FlashCanvas1.DrawLines(pen, points);

			this.c1FlashCanvas1.RenderToFile(tempdir + @"\c1flash_canvas_lines.swf");
			LaunchViewer(tempdir + @"\c1flash_canvas_lines.swf");
		}
	}
}
