Advertisement
Guest User

Untitled

a guest
Jun 5th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.77 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.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using MySql.Data.MySqlClient;
  11.  
  12. namespace WindowsFormsApp3
  13. {
  14.     public partial class График : Form
  15.     {
  16.         public График()
  17.         {
  18.             InitializeComponent();
  19.             Draw();
  20.         }
  21.         private void Draw()
  22.         {
  23.             Random rnd = new Random();
  24.             int countOfRectangles = 0;
  25.             int i = 0;
  26.             string connectionString = "datasource=127.0.0.1;port=3306;username=root;password=password;database=nums";
  27.             MySqlConnection databaseConnection = new MySqlConnection(connectionString);
  28.             MySqlCommand commandDatabase = new MySqlCommand("SELECT * FROM numbers", databaseConnection);
  29.             databaseConnection.Open();
  30.             MySqlDataReader counter = commandDatabase.ExecuteReader();
  31.             if (counter.HasRows)
  32.             {
  33.                 while (counter.Read())
  34.                 {
  35.                     countOfRectangles++;
  36.                 }
  37.             }
  38.             databaseConnection.Close();
  39.             int[] array = new int[countOfRectangles];
  40.             databaseConnection.Open();
  41.             MySqlDataReader reader = commandDatabase.ExecuteReader();
  42.             if (reader.HasRows)
  43.             {
  44.                 while (reader.Read())
  45.                 {
  46.                     array[i] = Convert.ToInt32(reader.GetString(1));
  47.                     i++;
  48.                 }
  49.             }
  50.  
  51.             Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  52.             Graphics graph = Graphics.FromImage(bmp);
  53.             Pen pen = new Pen(Color.Red);
  54.             Pen pens = new Pen(Color.Black);
  55.             Pen pen1 = new Pen(Color.Green);
  56.             pictureBox1.Image = bmp;
  57.            
  58.             int x = 0;
  59.             int width = pictureBox1.Width/countOfRectangles/2; //ширина для каждого прямоугольника
  60.            
  61.             SolidBrush brush1 = new SolidBrush(Color.Black);
  62.             int minOfArray = array.Min();
  63.             int maxOfArray = array.Max();
  64.             int maxCount = 1;
  65.             if (maxOfArray >= Math.Abs(minOfArray))
  66.             {
  67.                 maxCount = maxOfArray;
  68.             }
  69.             else
  70.             {
  71.                 maxCount =  Math.Abs(minOfArray);
  72.             }
  73.             double height = pictureBox1.Height;
  74.             double coefficient = height/maxCount;
  75.             Font font;
  76.             StringFormat stringFormat = new StringFormat();
  77.             stringFormat.Alignment = StringAlignment.Center;
  78.             stringFormat.LineAlignment = StringAlignment.Center;
  79.             if (countOfRectangles >= 15)
  80.             {
  81.                font = new Font("Arial", 7);
  82.             }
  83.             else
  84.             {
  85.                 font = new Font("Arial", 10);
  86.             }
  87.             for (i = 0; i<countOfRectangles; i++)
  88.             {
  89.                 int height_of_rectangle = Convert.ToInt32(array[i] * coefficient*0.4);
  90.                 if (array[i]>=0)
  91.                 {
  92.                     string symbol = Convert.ToString(array[i]);
  93.                     int countOfSymbol = symbol.Length;
  94.                     Rectangle rect = new Rectangle(x, pictureBox1.Height / 2 - height_of_rectangle, width,height_of_rectangle);
  95.                     SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
  96.                     graph.FillRectangle(brush, rect);
  97.                     graph.DrawString(array[i].ToString(), font, brush1, x+width/2 - countOfSymbol*4,  pictureBox1.Height / 2 - height_of_rectangle - 15);
  98.                 }
  99.                 if(array[i]<0)
  100.                 {
  101.                     string symbol = Convert.ToString(array[i]);
  102.                     int countOfSymbol = symbol.Length;
  103.                     Rectangle rect = new Rectangle(x, pictureBox1.Height / 2, width, Math.Abs(height_of_rectangle));
  104.                     SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
  105.                     graph.FillRectangle(brush, rect);
  106.                     graph.DrawString(array[i].ToString(), font, brush1, x+width/2-countOfSymbol*4, pictureBox1.Height / 2 + Math.Abs(height_of_rectangle));
  107.                 }
  108.                
  109.                 x = x + 2 * width; //расстояние между соседними значениями
  110.             }
  111.             graph.DrawLine(pens, 0, pictureBox1.Height / 2, width*countOfRectangles*2-width-1, pictureBox1.Height / 2);
  112.  
  113.         }
  114.  
  115.         private void pictureBox1_Click(object sender, EventArgs e)
  116.         {
  117.  
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement