Created
February 9, 2025 19:53
-
-
Save gitnasr/f5a850a8f3003eb51f033688563a28c4 to your computer and use it in GitHub Desktop.
The NASR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Drawing.Drawing2D; | |
namespace WinFormABC | |
{ | |
public partial class Form1 : Form | |
{ | |
private string title; | |
private Color color; | |
private Font font; | |
private Brush brush; | |
private SizeF textSize; | |
private float xPosition; | |
private float yPosition; | |
private int cellWidth = 120; | |
private int cellHeight = 40; | |
private int startX = 50; | |
private int startY = 200; | |
private int chartWidth = 500; | |
private int chartHeight = 300; | |
private int chartStartX = 400; | |
private int chartStartY = 650; | |
private int xMargin = 20; | |
private int barWidth = 30; | |
private Pen linePen = new Pen(Color.Blue, 2); | |
private Color barColor = Color.Green; | |
private HatchStyle brushStyle = HatchStyle.Divot; | |
private Brush barBrush; | |
private Dictionary<string, List<int>> tableData = new Dictionary<string, List<int>>() | |
{ | |
{ "Year", new List<int> { 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 } }, | |
{ "Revenue", new List<int> { 150, 170, 180, 175, 200, 250, 210, 240, 280, 140 } } | |
}; | |
public Form1() | |
{ | |
// Start form Maximized | |
this.WindowState = FormWindowState.Maximized; | |
InitializeComponent(); | |
barBrush = new HatchBrush(brushStyle, barColor); | |
} | |
private void renderTitle() | |
{ | |
Graphics g = CreateGraphics(); | |
title = "ABC Company"; | |
color = Color.Green; | |
font = new Font("Arial", 16); | |
brush = new SolidBrush(color); | |
textSize = g.MeasureString(title, font); | |
xPosition = (this.Width - textSize.Width) / 2; | |
yPosition = 0; | |
g.DrawString(title, font, brush, xPosition, yPosition); | |
} | |
private void renderSubtitle() | |
{ | |
Graphics g = CreateGraphics(); | |
title = "Annual Revene"; | |
color = Color.Red; | |
font = new Font("Arial", 16, FontStyle.Underline); | |
brush = new SolidBrush(color); | |
textSize = g.MeasureString(title, font); | |
xPosition = (this.Width - textSize.Width) / 2; | |
yPosition = 25; | |
g.DrawString(title, font, brush, xPosition, yPosition); | |
} | |
private void renderTable() | |
{ | |
Graphics g = CreateGraphics(); | |
Font font = new Font("Arial", 12, FontStyle.Bold); | |
Brush textBrush = Brushes.Black; | |
Pen borderPen = new Pen(Color.Black, 2); | |
g.DrawRectangle(borderPen, startX, startY, cellWidth, cellHeight); | |
g.DrawString("Year", font, textBrush, startX + 10, startY + 10); | |
g.DrawRectangle(borderPen, startX, startY + cellHeight, cellWidth, cellHeight); | |
g.DrawString("Revenue", font, textBrush, startX + 10, startY + cellHeight + 10); | |
for (int i = 0; i < tableData["Year"].Count; i++) | |
{ | |
int x = startX + (i + 1) * cellWidth; | |
// Year row | |
g.DrawRectangle(borderPen, x, startY, cellWidth, cellHeight); | |
g.DrawString(tableData["Year"][i].ToString(), font, textBrush, x + 10, startY + 10); | |
// Revenue row | |
g.DrawRectangle(borderPen, x, startY + cellHeight, cellWidth, cellHeight); | |
g.DrawString(tableData["Revenue"][i].ToString(), font, textBrush, x + 10, startY + cellHeight + 10); | |
} | |
} | |
private void renderChart() | |
{ | |
Graphics g = CreateGraphics(); | |
float maxRevenue = tableData["Revenue"].Max(); // عشان نجيب اعلا قيمه للاسكيل | |
float xScale = chartWidth / (tableData["Year"].Count - 1); // بنقسم علي عددهم عشان يبقي الفرق بينهم متساوي | |
// x,y Axixs | |
Pen axisPen = new Pen(Color.Black, 2); | |
int yAxisEndPoint = chartStartY - chartHeight; | |
Point StartingPointForBothAxis = new Point(chartStartX, chartStartY); | |
Point yAxis = new Point(chartStartX, yAxisEndPoint); | |
int xAxisEndPoint = chartStartX + (int)chartWidth + (int)xScale + 5; | |
Point xAxis = new Point(xAxisEndPoint, chartStartY); | |
g.DrawLine(axisPen, StartingPointForBothAxis, yAxis); // Y axis | |
g.DrawLine(axisPen, StartingPointForBothAxis, xAxis); // X axis | |
// Draw Y-axis labels | |
Font labelFont = new Font("Arial", 10); | |
for (int i = 0; i <= 10; i++) | |
{ | |
float value = maxRevenue * i / 10; // اقسم الواي اكسيس علي 10 بقي عشان بحب رقم 10 | |
float y = chartStartY - value; // Calculate position of label | |
float posX = chartStartX - 35; // We need the labels to be on the left of the Y-axis | |
float posY = y - 10; // بنطرح عشره عشان نخليهم ع مستوي واحد | |
Point point = new Point((int)posX, (int)posY); | |
g.DrawString(value.ToString(), labelFont, Brushes.Black, point); | |
} | |
// Draw X-axis labels (years) | |
for (int i = 0; i < tableData["Year"].Count; i++) | |
{ | |
float x = chartStartX + i * xScale + xMargin; | |
g.DrawString(tableData["Year"][i].ToString(), labelFont, Brushes.Black, x - 25, chartStartY + 25); | |
} | |
// Draw bar chart | |
for (int i = 0; i < tableData["Revenue"].Count; i++) | |
{ | |
float x = chartStartX + i * xScale - barWidth / 2 + xMargin; | |
float height = tableData["Revenue"][i]; | |
g.FillRectangle(barBrush, x, chartStartY - height, barWidth, height); | |
} | |
// Draw line chart | |
Point[] points = new Point[tableData["Revenue"].Count]; | |
for (int i = 0; i < tableData["Revenue"].Count; i++) | |
{ | |
points[i] = new Point( | |
(int)(chartStartX + i * xScale + xMargin), | |
(int)(chartStartY - tableData["Revenue"][i]) | |
); | |
} | |
g.DrawLines(linePen, points); | |
// Add chart title | |
Font titleFont = new Font("Arial", 12, FontStyle.Bold); | |
g.DrawString("Revenue Trends", titleFont, Brushes.Black, | |
chartStartX + chartWidth / 2, chartStartY - chartHeight - 30); | |
} | |
protected override void OnPaint(PaintEventArgs e) | |
{ | |
renderTitle(); | |
renderSubtitle(); | |
renderTable(); | |
renderChart(); | |
} | |
protected override void OnResize(EventArgs e) | |
{ | |
Invalidate(); | |
} | |
private void Form1_MouseClick(object sender, MouseEventArgs e) | |
{ | |
float maxRevenue = tableData["Revenue"].Max(); // عشان نجيب اعلا قيمه للاسكيل | |
// On Click on any bar get the year and revenue | |
float xScale = chartWidth / (tableData["Year"].Count - 1); // بنقسم علي عددهم عشان يبقي الفرق بينهم متساوي | |
for (int i = 0; i < tableData["Year"].Count; i++) | |
{ | |
float x = chartStartX + i * xScale - barWidth / 2 + xMargin; | |
float height = tableData["Revenue"][i]; | |
if (e.X >= x && e.X <= x + barWidth && e.Y >= chartStartY - height && e.Y <= chartStartY) | |
{ | |
MessageBox.Show($"Year: {tableData["Year"][i]}, Revenue: {tableData["Revenue"][i]}"); | |
} | |
} | |
//MessageBox.Show($"X: {e.X}, Y: {e.Y}") | |
} | |
private void redToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
linePen = new Pen(Color.Red, 2); | |
Invalidate(); | |
} | |
private void greenToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
linePen = new Pen(Color.Green, 2); | |
Invalidate(); | |
} | |
private void blueToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
linePen = new Pen(Color.Blue, 2); | |
Invalidate(); | |
} | |
private void solidToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
linePen.DashStyle = DashStyle.Solid; | |
Invalidate(); | |
} | |
private void dashToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
linePen.DashStyle = DashStyle.Dash; | |
Invalidate(); | |
} | |
private void dottedToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
linePen.DashStyle = DashStyle.Dot; | |
Invalidate(); | |
} | |
private void rightToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
brushStyle = HatchStyle.ForwardDiagonal; | |
barBrush = new HatchBrush(brushStyle, barColor); | |
Invalidate(); | |
} | |
private void leftToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
brushStyle = HatchStyle.BackwardDiagonal; | |
barBrush = new HatchBrush(brushStyle, barColor); | |
Invalidate(); | |
} | |
private void crossToolStripMenuItem_Click(object sender, EventArgs e) | |
{ | |
brushStyle = HatchStyle.Cross; | |
barBrush = new HatchBrush(brushStyle, barColor); | |
Invalidate(); | |
} | |
private void redToolStripMenuItem1_Click(object sender, EventArgs e) | |
{ | |
barColor = Color.Red; | |
barBrush = new HatchBrush(brushStyle, barColor); | |
Invalidate(); | |
} | |
private void greenToolStripMenuItem1_Click(object sender, EventArgs e) | |
{ | |
barColor = Color.Green; | |
barBrush = new HatchBrush(brushStyle, barColor); | |
Invalidate(); | |
} | |
private void blueToolStripMenuItem1_Click(object sender, EventArgs e) | |
{ | |
barColor = Color.Blue; | |
barBrush = new HatchBrush(brushStyle, barColor); | |
Invalidate(); | |
} | |
private void toolStripButton1_Click(object sender, EventArgs e) | |
{ | |
barColor = Color.Red; | |
barBrush = new HatchBrush(brushStyle, barColor); | |
Invalidate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment