Friday, May 30, 2014

Simple Bar Graph and Pie Chart


namespace SimpleBarGraph
{
    public partial class Form1 : Form
    {

        int[] stokevalues = { 85, 125, 40, 60, 90, 50, 70 };

        Bitmap bmap1;
        Graphics g1;

        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        int ymax, xmax;
        private void Form1_Load(object sender, EventArgs e)
        {
            bmap1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g1 = Graphics.FromImage(bmap1);

            xmax = 9 * bmap1.Width / 10; //95% of the bitmap width
            ymax = 9 * bmap1.Height / 10;
            Pen pen = new Pen(Color.White,2);
            Point p1 = new Point(0, 0);
            Point px2 = new Point(xmax, 0);
            Point py2 = new Point(0, ymax);

            //Create a Matrix to transform the graphics object
            int dx = bmap1.Width / 10;
            int dy = 9 * bmap1.Height / 10;


            Matrix matrix = new Matrix(1, 0, 0, -1, dx, dy);
            //apply to this graphics transform
            g1.Transform = matrix;
            //g2.TranslateTransform(dx, dy);
            //draw x-y axis
            g1.DrawLine(pen, p1, px2);
            g1.DrawLine(pen, p1, py2);

            pictureBox1.Image = bmap1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //space between bars
            //distance width
            int dw = xmax / stokevalues.Length;
            //width of the bar
           int bw = dw / 3;
           int x = bw;
           Point p = new Point(x, 0);
           Brush brush = Brushes.Red;
            int maxstock = stokevalues.Max();
            float sf = ymax / maxstock;

           foreach (int v in stokevalues)
           {
               float h = sf * v;
               //fill rectangle
               g1.FillRectangle(brush, x, 0, bw, h);
               x += dw;
               p = new Point(x, 0);
           }
           //add to picture box.
           pictureBox1.Image = bmap1;
        }


        private void btnDrawPieChart_Click(object sender, EventArgs e)
        {
            Point p = new Point(pictureBox1.Width / 4, pictureBox1.Height / 3);
            Brush brush = Brushes.Aqua;
            float diameter = 100;
            float startAngle = 0;
            float sweepAngle;
            float scaleFactor = 360f / stokevalues.Sum();

            foreach (int v in stokevalues)
            {
                sweepAngle = scaleFactor * v;
                g1.FillPie(brush, p.X, p.Y, diameter, diameter, startAngle, sweepAngle);

                if (brush.Equals(Brushes.Aqua))
                    brush = Brushes.Beige;
                else if (brush.Equals(Brushes.Beige))
                    brush = Brushes.Yellow;
                else if (brush.Equals(Brushes.Yellow))
                    brush = Brushes.Green;
                else if (brush.Equals(Brushes.Green))
                    brush = Brushes.Aqua;

                startAngle += sweepAngle;

            }
            pictureBox1.Image = bmap1;
        }


        private void btnPieChart_Click(object sender, EventArgs e)
        {
            Point p = new Point(10, 10);
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = 100;
            int height = width;
            Rectangle rect = new Rectangle(x, y, width, height);

            SolidBrush brush = new SolidBrush(Color.AliceBlue);
            float startAngle = 0;
            float sweepAngle = 85 * 360f / 520;
            g1.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Green);
            startAngle += sweepAngle;
            sweepAngle = 125 * 360f / 520;
            g1.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Yellow);
            startAngle += sweepAngle;
            sweepAngle = 40 * 360f / 520;
            g1.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Blue);
            startAngle += sweepAngle;
            sweepAngle = 60 * 360f / 520;
            g1.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Red);
            startAngle += sweepAngle;
            sweepAngle = 90 * 360f / 520;
            g1.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Orange);
            startAngle += sweepAngle;
            sweepAngle = 50 * 360f / 520;
            g1.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.White);
            startAngle += sweepAngle;
            sweepAngle = 70 * 360f / 520;
            g1.FillPie(brush, rect, startAngle, sweepAngle);

            pictureBox1.Image = bmap1;
        }
    }
}

No comments:

Post a Comment