using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MyDUMeter
{
/// <summary>
/// Summary description for ReportForm.
/// </summary>
public class ReportForm : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.ComponentModel.IContainer components;
int hours;
MyDUMeter.Log.RateLogDataTable table;
Bitmap graph;
Pen h;
Pen r;
Pen s;
Pen b;
int linespeed;
private System.Windows.Forms.ToolTip toolTip1;
DateTime now = DateTime.Now;
public ReportForm(int hours, Log.RateLogDataTable table, int linespeed)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.table = table;
this.hours = hours;
this.linespeed = linespeed;
Text = "Report for the last " + hours + " hours";
int width = 720;
graph = new Bitmap(width, pictureBox1.Height);
h = new Pen(Color.Black, 1);
r = new Pen(Color.Red);
s = new Pen(Color.Green);
b = new Pen(Color.Yellow);
pictureBox1.Image = graph;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
h.Dispose();
r.Dispose();
b.Dispose();
s.Dispose();
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(712, 224);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click_1);
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
this.pictureBox1.MouseHover += new System.EventHandler(this.pictureBox1_MouseHover);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox1.DoubleClick += new System.EventHandler(this.pictureBox1_Click);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
//
// toolTip1
//
this.toolTip1.AutomaticDelay = 0;
this.toolTip1.ShowAlways = true;
//
// ReportForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(712, 224);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pictureBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Name = "ReportForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "ReportForm";
this.Resize += new System.EventHandler(this.ReportForm_Resize);
this.Load += new System.EventHandler(this.ReportForm_Load);
this.ResumeLayout(false);
}
#endregion
private void ReportForm_Load(object sender, System.EventArgs e)
{
DrawGraph();
}
private void DrawGraph()
{
Graphics g = Graphics.FromImage(graph);
g.Clear(Color.WhiteSmoke);
foreach (Log.RateLogRow row in table)
{
float maxr = row.Recv;
float minr = row.Send;
Pen p = r;
if (row.Recv < row.Send)
{
p = s;
maxr = row.Send;
minr = row.Recv;
}
float w = graph.Width/(hours*60F);
float x = (float)((row.ID - now.AddHours(-hours)).TotalMinutes*w);
p.Width = w;
b.Width = w;
g.DrawLine(p, x, graph.Height,
x , graph.Height - graph.Height * ((maxr/60) / linespeed));
g.DrawLine(b, x, graph.Height,
x , graph.Height - graph.Height * ((minr/60) / linespeed));
}
pictureBox1.Image = graph;
g.Dispose();
}
int inc = 60;
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
h.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
switch (hours)
{
case 1:
inc = graph.Width/12; //every 5 min
break;
case 3:
inc = graph.Width/12; //every 15 min
break;
case 6:
inc = graph.Width/12; //every 30 min
break;
case 12:
inc = graph.Width/12; //every 1 hour
break;
case 24:
inc = graph.Width/8; //every 3 hours
break;
case 72:
inc = graph.Width/12; //every 6 hours
break;
}
for (int i = 0; i < graph.Width; i+=inc)
{
e.Graphics.DrawLine(h,
i, 0 , i, graph.Height);
}
h.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
int vlines = 4;
for (int j = 1; j < vlines; j++)
{
int y = j*graph.Height/vlines;
e.Graphics.DrawLine(h, 0, y, graph.Width, y);
}
}
private void ReportForm_Resize(object sender, System.EventArgs e)
{
graph = new Bitmap(720, pictureBox1.Height);
Size = new Size(720, Size.Height);
DrawGraph();
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
if (FormBorderStyle != FormBorderStyle.SizableToolWindow)
FormBorderStyle = FormBorderStyle.SizableToolWindow;
else
FormBorderStyle = FormBorderStyle.None;
}
bool mousedown = false;
Point mousept;
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
mousept = new Point(e.X, e.Y);
mousedown = true;
break;
}
}
private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (mousedown)
{
Location = new Point(Location.X - (mousept.X - e.X), Location.Y - (mousept.Y - e.Y));
}
}
private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
mousedown = false;
}
private void pictureBox1_MouseHover(object sender, System.EventArgs e)
{
//ShowTT();
}
private void ShowTT()
{
Point m = pictureBox1.PointToClient(MousePosition);
Console.WriteLine("MPoint: {0}" , m);
Console.WriteLine("Inc: {0}", inc);
int section = m.X/inc;
Console.WriteLine("Section: {0}", section);
float minutewidth = graph.Width/(hours*60F);
Console.WriteLine("MinuteWidth: {0}", minutewidth);
float rtot = 0;
float stot = 0;
int counter = 1;
DateTime from = now.AddHours(-hours).AddMinutes((inc/minutewidth) * section);
DateTime to = now.AddHours(-hours).AddMinutes((inc/minutewidth) * (section + 1));
foreach (Log.RateLogRow row in table)
{
if (row.ID >= from &
row.ID <= to )
{
rtot += row.Recv;
stot += row.Send;
counter++;
}
}
toolTip1.SetToolTip(pictureBox1, String.Format(@"Period: {6}
From:{4}
To: {5}
Total Recv:{0} KB
Total Sent:{1} KB
Avg Recv:{2} KB/sec
Avg Sent:{3} KB/sec",
(rtot/1024F).ToString("f0"),
(stot/1024F).ToString("f0"),
(rtot/(counter*60*1024)).ToString("f1"),
(stot/(counter*60*1024)).ToString("f1"),
from,
to,
(to - from).TotalMinutes > 60 ?
(to - from).TotalHours.ToString("f0") + " hours" :
(to - from).TotalMinutes.ToString("f0") + " minutes"
));
}
private void pictureBox1_Click_1(object sender, System.EventArgs e)
{
ShowTT();
}
}
}