Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 MySql.Data.MySqlClient;
- namespace WindowsFormsApp3
- {
- public partial class График : Form
- {
- public График()
- {
- InitializeComponent();
- Draw();
- }
- private void Draw()
- {
- Random rnd = new Random();
- int countOfRectangles = 0;
- int i = 0;
- string connectionString = "datasource=127.0.0.1;port=3306;username=root;password=password;database=nums";
- MySqlConnection databaseConnection = new MySqlConnection(connectionString);
- MySqlCommand commandDatabase = new MySqlCommand("SELECT * FROM numbers", databaseConnection);
- databaseConnection.Open();
- MySqlDataReader counter = commandDatabase.ExecuteReader();
- if (counter.HasRows)
- {
- while (counter.Read())
- {
- countOfRectangles++;
- }
- }
- databaseConnection.Close();
- int[] array = new int[countOfRectangles];
- databaseConnection.Open();
- MySqlDataReader reader = commandDatabase.ExecuteReader();
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- array[i] = Convert.ToInt32(reader.GetString(1));
- i++;
- }
- }
- Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
- Graphics graph = Graphics.FromImage(bmp);
- Pen pen = new Pen(Color.Red);
- Pen pens = new Pen(Color.Black);
- Pen pen1 = new Pen(Color.Green);
- pictureBox1.Image = bmp;
- int x = 0;
- int width = pictureBox1.Width/countOfRectangles/2; //ширина для каждого прямоугольника
- SolidBrush brush1 = new SolidBrush(Color.Black);
- int minOfArray = array.Min();
- int maxOfArray = array.Max();
- int maxCount = 1;
- if (maxOfArray >= Math.Abs(minOfArray))
- {
- maxCount = maxOfArray;
- }
- else
- {
- maxCount = Math.Abs(minOfArray);
- }
- double height = pictureBox1.Height;
- double coefficient = height/maxCount;
- Font font;
- StringFormat stringFormat = new StringFormat();
- stringFormat.Alignment = StringAlignment.Center;
- stringFormat.LineAlignment = StringAlignment.Center;
- if (countOfRectangles >= 15)
- {
- font = new Font("Arial", 7);
- }
- else
- {
- font = new Font("Arial", 10);
- }
- for (i = 0; i<countOfRectangles; i++)
- {
- int height_of_rectangle = Convert.ToInt32(array[i] * coefficient*0.4);
- /*if (array[i] < 0 && Math.Abs(array[i]) > 1000)
- {
- Rectangle rect = new Rectangle(x, pictureBox1.Height / 2, width, Math.Abs(height_of_rectangle));
- SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
- graph.FillRectangle(brush, rect);
- graph.DrawString(array[i].ToString(), font, brush1, x - 5, pictureBox1.Height / 2 - height_of_rectangle);
- }*/
- if (array[i]>=0)
- {
- string symbol = Convert.ToString(array[i]);
- int countOfSymbol = symbol.Length;
- Rectangle rect = new Rectangle(x, pictureBox1.Height / 2 - height_of_rectangle, width,height_of_rectangle);
- SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
- graph.FillRectangle(brush, rect);
- graph.DrawString(array[i].ToString(), font, brush1, x+width/2 - countOfSymbol*4, pictureBox1.Height / 2 - height_of_rectangle - 15);
- }
- if(array[i]<0)
- {
- string symbol = Convert.ToString(array[i]);
- int countOfSymbol = symbol.Length;
- Rectangle rect = new Rectangle(x, pictureBox1.Height / 2, width, Math.Abs(height_of_rectangle));
- SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
- graph.FillRectangle(brush, rect);
- graph.DrawString(array[i].ToString(), font, brush1, x+width/2-countOfSymbol*4, pictureBox1.Height / 2 + Math.Abs(height_of_rectangle));
- }
- /*if (array[i]>0 && array[i]>1000)
- {
- Rectangle rect = new Rectangle(x, pictureBox1.Height / 2 - height_of_rectangle, width, height_of_rectangle);
- SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
- graph.FillRectangle(brush, rect);
- graph.DrawString(array[i].ToString(), font, brush1, x-3, pictureBox1.Height / 2 - height_of_rectangle - 15);
- }*/
- x = x + 2 * width; //расстояние между соседними значениями
- }
- graph.DrawLine(pens, 0, pictureBox1.Height / 2, width*countOfRectangles*2-width-1, pictureBox1.Height / 2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement