Advertisement
csaki

Barchart in GraphicsContainer (rotate)

Oct 8th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace grafika_oszlopdiagram
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         Random rand;
  17.         Graphics g;
  18.         int[] ertekek;
  19.         GraphicsContainer container;
  20.         LinearGradientBrush linearBrush;
  21.         Rectangle area;
  22.         int height;
  23.         int width;
  24.         int distance;
  25.         int bar_width;
  26.         int bar_height;
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.             rand = new Random();
  32.             g = CreateGraphics();
  33.             ertekek = new int[5];
  34.             g.SmoothingMode = SmoothingMode.AntiAlias;
  35.             for (int i = 0; i < ertekek.Length; i++)
  36.             {
  37.                 ertekek[i] = rand.Next(2, 10);
  38.             }
  39.             container = g.BeginContainer();
  40.         }
  41.  
  42.         private void trackBar1_Scroll(object sender, EventArgs e)
  43.         {
  44.             Invalidate();
  45.             Update();
  46.            
  47.             // Trackbar maximum = 300;
  48.             height = trackBar1.Value;
  49.             width = trackBar1.Value;
  50.             distance = 10;
  51.             bar_height = ((area.Height / ertekek.Length - 1) - distance) < 50 ?
  52.                 ((area.Height / ertekek.Length - 1) - distance) : 50;
  53.             area = new Rectangle(20, 20, width, height);
  54.  
  55.             // Area
  56.             g.DrawRectangle(new Pen(Color.Chocolate, 4), area);
  57.  
  58.  
  59.             for (int i = 0; i < ertekek.Length; i++)
  60.             {
  61.                 float sum = ertekek.Sum();
  62.                 bar_width = (int)((ertekek[i] / sum) * area.Width) * ertekek.Length / 2;
  63.                
  64.                 if (bar_height == 0)
  65.                 {
  66.                     bar_height = 1;
  67.                 }
  68.                 if (bar_width == 0)
  69.                 {
  70.                     bar_width = 1;
  71.                 }
  72.                 Rectangle rect = new Rectangle(area.X + 10, area.Y + (int)distance, bar_width, bar_height);
  73.  
  74.                 linearBrush = new LinearGradientBrush(rect, Color.Tan, Color.Peru, LinearGradientMode.ForwardDiagonal);
  75.  
  76.                 distance += bar_height + 8;
  77.                 g.FillRectangle(linearBrush, rect);
  78.                 g.DrawRectangle(Pens.Brown, rect);
  79.             }
  80.  
  81.             g.EndContainer(container);
  82.         }
  83.  
  84.         private void trackBar2_Scroll(object sender, EventArgs e)
  85.         {
  86.             Invalidate();
  87.             Update();
  88.             g.RotateTransform(trackBar2.Value);
  89.             g.Save();
  90.             trackBar1_Scroll(this, e);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement