public Form2(int type, RGBColor color) { InitializeComponent(); this.type = type; this.r = color.R; this.g = color.G; this.b = color.B; okRes = false; }
private void drawToolStripMenuItem_Click(object sender, EventArgs e) { if (isData) { isDraw = true; oldRGBColor = rgbColor; oldType = Type; GraphPanel.BackgroundImage = DrawGraph(GraphPanel.Width, GraphPanel.Height, oldRGBColor, oldType); } else { MessageBox.Show("No input data!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public Image DrawGraph(int w, int h, RGBColor rgbColor, int Type) { Pen pen2 = new Pen(Color.Blue, 2); Pen pen1 = new Pen(Color.Blue, 1); Bitmap image = new Bitmap(w, h); Graphics g = Graphics.FromImage(image); g.SmoothingMode = SmoothingMode.AntiAlias; if (Type == 1) { int maxPositive = points.Max(p => p.Y); maxPositive = maxPositive < 0 ? 0 : maxPositive; int maxNegative = points.Min(p => p.Y); bool isNegative = maxNegative < 0; int maxH = isNegative ? maxPositive + Math.Abs(maxNegative) : maxPositive; int hInterval = (h - 4) / maxH; int xInterval = (w - 4) / points.Count; int bottomPadding = isNegative ? hInterval * Math.Abs(maxNegative) : 0; SolidBrush rectangleBrush = new SolidBrush(Color.FromArgb(rgbColor.R, rgbColor.G, rgbColor.B)); for (int i = 0; i < points.Count; i++) { if (points[i].Y > 0) { g.FillRectangle(rectangleBrush, i * xInterval + 2, h - 2 - bottomPadding - hInterval * points[i].Y, xInterval, hInterval * points[i].Y); } else { g.FillRectangle(rectangleBrush, i * xInterval + 2, h - 2 - bottomPadding, xInterval, hInterval * Math.Abs(points[i].Y)); } } //X g.DrawLine(pen2, 2, h - 2 - bottomPadding, w - 2, h - 2 - bottomPadding); //Y g.DrawLine(pen2, 2, h - 2, 2, 2); int val = 0; for (int i = h - 2 - bottomPadding; i > 0; i -= hInterval) { g.DrawLine(pen1, 0, i, 4, i); if (val % 5 == 0 && val!=0) g.DrawString(val.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(4, i - 4)); val++; } val = 0; if (isNegative) for (int i = h - 2 - bottomPadding; i < h; i += hInterval) { g.DrawLine(pen1, 0, i, 4, i); if (val % 5 == 0 && val != 0) g.DrawString(val.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(4, i - 4)); val--; } g.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, new PointF(2, h - 2 - bottomPadding)); g.DrawString("X", SystemFonts.DefaultFont, Brushes.Blue, new PointF(w - 15, h - 2 - bottomPadding)); g.DrawString("Y", SystemFonts.DefaultFont, Brushes.Blue, new PointF(4, 10)); this.Text = windowName + " - " + verticalDiagram; } if (Type == 2) { int maxPositive = points.Max(p => p.X); maxPositive = maxPositive < 0 ? 0 : maxPositive; int maxNegative = points.Min(p => p.X); bool isNegative = maxNegative < 0; int maxW = isNegative ? maxPositive + Math.Abs(maxNegative) : maxPositive; int hInterval = (h - 4) / points.Count; int wInterval = (w - 4) / maxW; int leftPadding = isNegative ? wInterval * Math.Abs(maxNegative) : 0; SolidBrush rectangleBrush = new SolidBrush(Color.FromArgb(rgbColor.R, rgbColor.G, rgbColor.B)); for (int i = 0; i < points.Count; i++) { if (points[i].X > 0) { g.FillRectangle(rectangleBrush, 2 + leftPadding, i * hInterval + 2, points[i].X * wInterval, hInterval); } else { g.FillRectangle(rectangleBrush, 2 + leftPadding - Math.Abs(points[i].X) * wInterval, i * hInterval + 2, Math.Abs(points[i].X) * wInterval, hInterval); } } //X g.DrawLine(pen2, 2, 2, w - 2, 2); //Y g.DrawLine(pen2, 2 + leftPadding, 2, 2 + leftPadding, h - 2); int val = 0; for (int i = 2 + leftPadding; i < w; i += wInterval) { g.DrawLine(pen1, i, 0, i, 4); if (val % 5 == 0 && val != 0) g.DrawString(val.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(i-5, 4)); val++; } val = 0; if (isNegative) { for (int i = 2 + leftPadding; i > 0; i -= wInterval) { g.DrawLine(pen1, i, 0, i, 4); if (val % 5 == 0 && val != 0) g.DrawString(val.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(i - 5, 4)); val--; } } g.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, new PointF(6 + leftPadding, 6)); g.DrawString("Y", SystemFonts.DefaultFont, Brushes.Blue, new PointF(6 + leftPadding, h - 16)); g.DrawString("X", SystemFonts.DefaultFont, Brushes.Blue, new PointF(w - 28, 4)); this.Text = windowName + " - " + horizontalDiagram; } if (Type == 3) { int maxPositiveY = points.Max(p => p.Y); maxPositiveY = maxPositiveY < 0 ? 0 : maxPositiveY; int maxNegativeY = points.Min(p => p.Y); bool isNegativeY = maxNegativeY < 0; int maxPositiveX = points.Max(p => p.X); maxPositiveX = maxPositiveX < 0 ? 0 : maxPositiveX; int maxNegativeX = points.Min(p => p.X); bool isNegativeX = maxNegativeX < 0; int maxH = isNegativeY ? maxPositiveY + Math.Abs(maxNegativeY) : maxPositiveY; int maxW = isNegativeX ? maxPositiveX + Math.Abs(maxNegativeX) : maxPositiveX; int hInterval = (h - 8) / maxH; int wInterval = (w - 8) / maxW; int bottomPadding = isNegativeY ? hInterval * Math.Abs(maxNegativeY) : 0; int leftPadding = isNegativeX ? wInterval * Math.Abs(maxNegativeX) : 0; SolidBrush brush = new SolidBrush(Color.FromArgb(rgbColor.R, rgbColor.G, rgbColor.B)); int centerX = 4 + leftPadding; int centerY = h - 4 - bottomPadding; Point firstP; Point secondP; Pen linePen = new Pen(Color.FromArgb(rgbColor.R, rgbColor.G, rgbColor.B), 2); for (int i = 1; i < points.Count; i++) { firstP = points[i - 1]; secondP = points[i]; g.DrawLine(linePen, centerX + firstP.X * wInterval, centerY - firstP.Y * hInterval, centerX + secondP.X * wInterval, centerY - secondP.Y * hInterval); } int valY = 0; for (int i = h - 4 - bottomPadding; i > hInterval; i -= hInterval) { g.DrawLine(pen1, centerX - 2, i, centerX + 2, i); if (valY % 5 == 0 && valY != 0) g.DrawString(valY.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(centerX + 4, i - 4)); valY++; } valY = 0; if (isNegativeY) for (int i = h - 4 - bottomPadding; i < h - hInterval; i += hInterval) { g.DrawLine(pen1, centerX - 2, i, centerX + 2, i); if (valY % 5 == 0 && valY != 0) g.DrawString(valY.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(centerX + 4, i - 4)); valY--; } int valX = 0; for (int i = 4 + leftPadding; i < w - wInterval; i += wInterval) { g.DrawLine(pen1, i, centerY - 2, i, centerY + 2); if (valX % 5 == 0 && valX != 0) g.DrawString(valX.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(i - 5, centerY + 4)); valX++; } valX = 0; if (isNegativeX) { for (int i = 4 + leftPadding; i > wInterval; i -= wInterval) { g.DrawLine(pen1, i, centerY - 2, i, centerY + 2); if (valX % 5 == 0 && valX != 0) g.DrawString(valX.ToString(), SystemFonts.DefaultFont, Brushes.Blue, new PointF(i - 5, centerY + 4)); valX--; } } //X g.DrawLine(pen2, 4, h - 4 - bottomPadding, w - 4, h - 4 - bottomPadding); g.DrawLine(pen2, w - 4, h - 4 - bottomPadding, w - 8, h - 1 - bottomPadding); g.DrawLine(pen2, w - 4, h - 4 - bottomPadding, w - 8, h - 7 - bottomPadding); //Y g.DrawLine(pen2, 4 + leftPadding, 4, 4 + leftPadding, h - 4); g.DrawLine(pen2, 4 + leftPadding, 4, 1 + leftPadding, 7); g.DrawLine(pen2, 4 + leftPadding, 4, 7 + leftPadding, 7); g.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, new PointF(centerX + 2, centerY + 4)); g.DrawString("Y", SystemFonts.DefaultFont, Brushes.Blue, new PointF(6 + leftPadding, 8)); g.DrawString("X", SystemFonts.DefaultFont, Brushes.Blue, new PointF(w - 15, h - 2 - bottomPadding)); this.Text = windowName + " - " + xoy; } return image; }
private void chooseToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f = new Form2(Type, rgbColor); f.ShowDialog(); if (f.okRes) { drawToolStripMenuItem.Enabled = true; this.Type = f.Type; this.rgbColor = new RGBColor(f.R, f.G, f.B); } }