Advertisement
csaki

BarChart

Oct 1st, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7.  
  8. namespace DrawLib_test
  9. {
  10.     class BarChart
  11.     {
  12.         float[] values;
  13.         Pen pen;
  14.         int h, distance;
  15.         float width, height;
  16.  
  17.         public BarChart(params int[] values)
  18.         {
  19.             this.values = new float[values.Length];
  20.             for (int i = 0; i < values.Length; i++)
  21.             {
  22.                 this.values[i] = (float)values[i];
  23.             }
  24.         }
  25.  
  26.         public void DrawChart(Graphics g, Rectangle area)
  27.         {
  28.             h = area.Width;
  29.             distance = 25;
  30.             height = ((area.Height / values.Length - 1) - distance) < 50 ?
  31.                 ((area.Height / values.Length - 1) - distance) : 50;
  32.             pen = new Pen(Color.Red);
  33.             float max = values.Max();
  34.  
  35.             for (int i = 0; i < values.Length; i++)
  36.             {
  37.                 float sum = values.Sum();
  38.                 width = (values[i] / sum) * (h / max);
  39.                
  40.                 Rectangle rect = new Rectangle(20, distance, (int)width, (int)height);
  41.                 distance += (int)height + 10;
  42.                 g.FillRectangle(pen.Brush, rect);
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement