Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 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.Windows.Forms;
  9. using System.Drawing.Imaging;
  10.  
  11. namespace Graph
  12. {
  13. //<summary>
  14. //Нарисовать круговую диаграмму, используя данную информацию.
  15. //</summary>
  16. public partial class Form1 : Form
  17. {
  18. public Bitmap Draws(Color bgCol, int width, int height,
  19. decimal[] vals)
  20. {
  21. // Создаем новый образ и стираем фон
  22. Bitmap mybit = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  23. Graphics graphics = Graphics.FromImage(mybit);
  24. SolidBrush brush = new SolidBrush(bgCol);
  25. graphics.FillRectangle(brush, 0, 0, width, height);
  26. brush.Dispose();
  27.  
  28. // Создаем кисти для окрашивания на круговой диаграмме
  29. SolidBrush[] brush2 = new SolidBrush[10];
  30. brush2[0] = new SolidBrush(Color.Yellow);
  31. brush2[1] = new SolidBrush(Color.Green);
  32. brush2[2] = new SolidBrush(Color.Blue);
  33. brush2[3] = new SolidBrush(Color.Cyan);
  34. brush2[4] = new SolidBrush(Color.Magenta);
  35. brush2[5] = new SolidBrush(Color.Red);
  36. brush2[6] = new SolidBrush(Color.Black);
  37. brush2[7] = new SolidBrush(Color.Gray);
  38. brush2[8] = new SolidBrush(Color.Maroon);
  39. brush2[9] = new SolidBrush(Color.LightBlue);
  40.  
  41. // Сумма для получения общего
  42. decimal all = 0.0m;
  43. foreach (decimal val in vals)
  44. all += val;
  45.  
  46. // Рисуем круговую диаграмму
  47. float startZ = 0.0f;
  48. float endZ = 0.0f;
  49. decimal current = 0.0m;
  50. for (int i = 0; i < vals.Length; i++)
  51. {
  52. current += vals[i];
  53. startZ = endZ;
  54. endZ = (float)(current / all) * 360.0f;
  55. graphics.FillPie(brush2[i % 10], 0.0f, 0.0f, width,height, startZ, endZ - startZ);
  56. }
  57.  
  58. // Очищаем ресурсы кисти
  59. foreach (SolidBrush cleanBrush in brush2)
  60. cleanBrush.Dispose();
  61.  
  62. return mybit;
  63. }
  64.  
  65.  
  66. public Form1()
  67. {
  68. InitializeComponent();
  69. }
  70.  
  71. private void Form1_Load(object sender, EventArgs e)
  72. {
  73.  
  74. }
  75.  
  76. private void Form1_Paint_1(object sender, PaintEventArgs e)
  77. {
  78. Color myColor = Color.FromArgb(255, 255, 255);//Задаем белый цвет для фона
  79. decimal[] vals = { 33, 25, 12, 10, 11, 9 };//Массив значений, числа могут быть любыми, не обязательно в сумме давать 100
  80. Bitmap myBitmap = Draws(myColor, 300, 300, vals);//Создаем картинку
  81. Graphics g = e.Graphics;
  82. g.DrawImage(myBitmap, 5, 5); //Выводим круговую диаграмму на экран в нужных координатах
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement