Advertisement
Guest User

Untitled

a guest
May 20th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 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.  
  52.  
  53.  
  54.  
  55. Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  56. Graphics graph = Graphics.FromImage(bmp);
  57. Pen pen = new Pen(Color.Red);
  58. Pen pens = new Pen(Color.Black);
  59. Pen pen1 = new Pen(Color.Green);
  60. pictureBox1.Image = bmp;
  61.  
  62. int x = 0;
  63. int width = pictureBox1.Width/countOfRectangles/2; //ширина для каждого прямоугольника
  64.  
  65. SolidBrush brush1 = new SolidBrush(Color.Black);
  66. int minOfArray = array.Min();
  67. int maxOfArray = array.Max();
  68. int maxCount = 1;
  69. if (maxOfArray >= Math.Abs(minOfArray))
  70. {
  71. maxCount = maxOfArray;
  72. }
  73. else
  74. {
  75. maxCount = Math.Abs(minOfArray);
  76. }
  77. double height = pictureBox1.Height;
  78. double coefficient = height/maxCount;
  79. Font font;
  80. StringFormat stringFormat = new StringFormat();
  81. stringFormat.Alignment = StringAlignment.Center;
  82. stringFormat.LineAlignment = StringAlignment.Center;
  83. if (countOfRectangles >= 15)
  84. {
  85. font = new Font("Arial", 7);
  86. }
  87. else
  88. {
  89. font = new Font("Arial", 10);
  90. }
  91. for (i = 0; i<countOfRectangles; i++)
  92. {
  93. int height_of_rectangle = Convert.ToInt32(array[i] * coefficient*0.4);
  94. /*if (array[i] < 0 && Math.Abs(array[i]) > 1000)
  95. {
  96. Rectangle rect = new Rectangle(x, pictureBox1.Height / 2, width, Math.Abs(height_of_rectangle));
  97. SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
  98. graph.FillRectangle(brush, rect);
  99. graph.DrawString(array[i].ToString(), font, brush1, x - 5, pictureBox1.Height / 2 - height_of_rectangle);
  100. }*/
  101. if (array[i]>=0)
  102. {
  103. string symbol = Convert.ToString(array[i]);
  104. int countOfSymbol = symbol.Length;
  105. Rectangle rect = new Rectangle(x, pictureBox1.Height / 2 - height_of_rectangle, width,height_of_rectangle);
  106. SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
  107. graph.FillRectangle(brush, rect);
  108. graph.DrawString(array[i].ToString(), font, brush1, x+width/2 - countOfSymbol*4, pictureBox1.Height / 2 - height_of_rectangle - 15);
  109. }
  110. if(array[i]<0)
  111. {
  112. string symbol = Convert.ToString(array[i]);
  113. int countOfSymbol = symbol.Length;
  114. Rectangle rect = new Rectangle(x, pictureBox1.Height / 2, width, Math.Abs(height_of_rectangle));
  115. SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
  116. graph.FillRectangle(brush, rect);
  117. graph.DrawString(array[i].ToString(), font, brush1, x+width/2-countOfSymbol*4, pictureBox1.Height / 2 + Math.Abs(height_of_rectangle));
  118. }
  119. /*if (array[i]>0 && array[i]>1000)
  120. {
  121. Rectangle rect = new Rectangle(x, pictureBox1.Height / 2 - height_of_rectangle, width, height_of_rectangle);
  122. SolidBrush brush = new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));
  123. graph.FillRectangle(brush, rect);
  124. graph.DrawString(array[i].ToString(), font, brush1, x-3, pictureBox1.Height / 2 - height_of_rectangle - 15);
  125. }*/
  126.  
  127. x = x + 2 * width; //расстояние между соседними значениями
  128. }
  129. graph.DrawLine(pens, 0, pictureBox1.Height / 2, width*countOfRectangles*2-width-1, pictureBox1.Height / 2);
  130.  
  131. }
  132.  
  133.  
  134.  
  135.  
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement