using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace Histogram
{
    public partial class frm7Histogram : Form
    {
        public frm7Histogram()
        {
            InitializeComponent();
        }

        private const int MinValue = 0;
        private const int MaxValue = 100;
        private float[] DataValues = new float[10];

        // Make some random data.
        private void frm7Histogram_Load(object sender, EventArgs e)
        {
            Random rnd = new Random();

            // Create data.
            for (int i = 0; i < DataValues.Length; i++)
                DataValues[i] = rnd.Next(MinValue + 5, MaxValue - 5);
        }

        // Draw the histogram.
        private void histogramPictureBox_Paint(object sender, PaintEventArgs e)
        {
            DrawHistogram(e.Graphics, histogramPictureBox.BackColor, DataValues,
                histogramPictureBox.ClientSize.Width, histogramPictureBox.ClientSize.Height);
        }

        // Redraw.
        private void histogramPictureBox_Resize(object sender, EventArgs e)
        {
            histogramPictureBox.Refresh();
        }

        // Draw a histogram.
        private void DrawHistogram(Graphics gr, Color bgcolor, float[] values, int width, int height)
        {
            Color[] colors = new Color[] {
                Color.Red, Color.LightGreen, Color.Blue,
                Color.Pink, Color.Green, Color.LightBlue,
                Color.Orange, Color.Yellow, Color.Purple
            };

            gr.Clear(bgcolor);

            // Make a transformation to the PictureBox.
            RectangleF dataBounds = new RectangleF(0, 0, values.Length, MaxValue);
            PointF[] points =
            {
                new PointF(0, height),
                new PointF(width, height),
                new PointF(0, 0)
            };
            Matrix transformation = new Matrix(dataBounds, points);
            gr.Transform = transformation;

            // Draw the histogram.
            using (Pen pen = new Pen(Color.Black, 0))
            {
                for (int i = 0; i < values.Length; i++)
                {
                    RectangleF rect = new RectangleF(i, 0, 1, values[i]);
                    using (Brush brush = new SolidBrush(colors[i % colors.Length]))
                    {
                        gr.FillRectangle(brush, rect);
                        gr.DrawRectangle(pen, rect);
                    }
                }
            }

            gr.ResetTransform();
            gr.DrawRectangle(Pens.Black, 0, 0, width - 1, height - 1);
        }

        // Display the value clicked.
        private void picHisto_MouseDown(object sender, MouseEventArgs e)
        {
            // Determine which data value was clicked.
            float bar_wid = histogramPictureBox.ClientSize.Width / (int)DataValues.Length;
            int i = (int)(e.X / bar_wid);
            MessageBox.Show("Item " + i + " has value " + DataValues[i],
                "Value", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.histogramPictureBox = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.histogramPictureBox)).BeginInit();
            this.SuspendLayout();
            // 
            // histogramPictureBox
            // 
            this.histogramPictureBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.histogramPictureBox.BackColor = System.Drawing.Color.White;
            this.histogramPictureBox.Cursor = System.Windows.Forms.Cursors.Cross;
            this.histogramPictureBox.Location = new System.Drawing.Point(8, 6);
            this.histogramPictureBox.Name = "histogramPictureBox";
            this.histogramPictureBox.Size = new System.Drawing.Size(418, 248);
            this.histogramPictureBox.TabIndex = 2;
            this.histogramPictureBox.TabStop = false;
            this.histogramPictureBox.Paint += new System.Windows.Forms.PaintEventHandler(this.histogramPictureBox_Paint);
            this.histogramPictureBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.picHisto_MouseDown);
            this.histogramPictureBox.Resize += new System.EventHandler(this.histogramPictureBox_Resize);
            // 
            // frm7Histogram
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(434, 261);
            this.Controls.Add(this.histogramPictureBox);
            this.Name = "frm7Histogram";
            this.Text = "Histogram";
            this.Load += new System.EventHandler(this.frm7Histogram_Load);
            ((System.ComponentModel.ISupportInitialize)(this.histogramPictureBox)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        internal System.Windows.Forms.PictureBox histogramPictureBox;
    }
}