Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
MSN Live Log Manager - GraphTimePerDay.vb

GraphTimePerDay.vb

Caricato da: Totem
Scarica il programma completo

  1. Public Class GraphTimePerDay
  2.     Private Days As List(Of Date)
  3.     Private Hours As Dictionary(Of Date, Single)
  4.     Private [Step], Pixels As Single
  5.     Private DFont As New Font("Microsoft Sans Serif", 13, FontStyle.Regular)
  6.  
  7.     Private Const BottomSpan As Int16 = 80
  8.     Private Const LeftSpan As Int16 = 50
  9.     Private Const DefaultWidth As Int16 = 650
  10.  
  11.     Private Sub InitializeValues(ByVal Log As LogExplorer.Log)
  12.         Days = New List(Of Date)
  13.         Hours = New Dictionary(Of Date, Single)
  14.  
  15.         For Each S As LogExplorer.Session In Log.Sessions
  16.             If Not Days.Contains(S.StartTime.Date) Then
  17.                 Days.Add(S.StartTime.Date)
  18.                 Hours.Add(S.StartTime.Date, S.ElapsedTime.TotalHours)
  19.             Else
  20.                 Hours(S.StartTime.Date) += S.ElapsedTime.TotalHours
  21.             End If
  22.         Next
  23.  
  24.         Days.Sort()
  25.         Pixels = 35 * (22 / Days.Count)
  26.         [Step] = Days.Count / 22
  27.         Me.Refresh()
  28.     End Sub
  29.  
  30.     Sub New(ByVal Log As LogExplorer.Log)
  31.         Me.InitializeComponent()
  32.         Me.InitializeValues(Log)
  33.     End Sub
  34.  
  35.     Private Function GetX(ByVal Index As Int16, ByVal PixelSpan As Single) As Single
  36.         Return LeftSpan + 17 + (Index) * PixelSpan
  37.     End Function
  38.  
  39.     Private Sub GraphTimePerDay_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  40.         Try
  41.             With e.Graphics
  42.                 .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
  43.  
  44.                 .DrawLine(Pens.Black, 50, 0, 50, Me.Height)
  45.                 .DrawLine(Pens.Black, 0, Me.Height - BottomSpan, Me.Width, Me.Height - BottomSpan)
  46.  
  47.                 Dim Index As Int16 = 0
  48.                 Dim Prec, PrecStart As Int16
  49.                 Dim Size As SizeF
  50.                 Dim Pix As Single
  51.                 Dim Points As New List(Of Point)
  52.                 Dim Max As Single
  53.  
  54.                 If [Step] < 1 Then
  55.                     Pix = Pixels * Me.Width / DefaultWidth
  56.                 Else
  57.                     Pix = 35 * Me.Width / DefaultWidth
  58.                 End If
  59.  
  60.                 For Each V As Single In Hours.Values
  61.                     If V > Max Then
  62.                         Max = V
  63.                     End If
  64.                 Next
  65.  
  66.                 For I As Int16 = 0 To Days.Count - 1
  67.                     If I Mod CInt(Math.Ceiling([Step])) = 0 Then
  68.                         .DrawLine(Pens.Black, GetX(Index, Pix), Me.Height - BottomSpan, GetX(Index, Pix), Me.Height - BottomSpan + 3)
  69.                         Size = .MeasureString(Days(I).Day, DFont)
  70.                         .DrawString(Days(I).Day, DFont, Brushes.Black, GetX(Index, Pix) - Size.Width / 2, Me.Height - BottomSpan + 1)
  71.                         Index += 1
  72.                     End If
  73.                     If Days(I).Month <> Prec Then
  74.                         If PrecStart > 0 Then
  75.                             Dim Month As String = New Date(2000, Prec, 1).ToString("MMMM")
  76.                             Dim Y As Int16 = Me.Height - BottomSpan + 20
  77.                             Dim ThisX As Int16 = GetX(Index - 1, Pix)
  78.                             Dim Color As Color = Drawing.Color.Gray
  79.                             Size = .MeasureString(Month, DFont)
  80.                             If PrecStart + Size.Width > ThisX Then
  81.                                 Y += 30
  82.                                 Color = Drawing.Color.LightGray
  83.                                 Month = Month.Substring(0, 3)
  84.                                 Size = .MeasureString(Month, DFont)
  85.                             End If
  86.                             .FillRectangle(New SolidBrush(Color), PrecStart, Y, ThisX - PrecStart - 1, 25)
  87.                             .DrawString(Month, DFont, Brushes.Black, PrecStart + (ThisX - PrecStart) / 2 - Size.Width / 2, Y)
  88.                         End If
  89.                         Prec = Days(I).Month
  90.                         PrecStart = GetX(Index - 1, Pix)
  91.                     End If
  92.                 Next
  93.                 If PrecStart > 0 Then
  94.                     Dim Month As String = New Date(2000, Days(Days.Count - 1).Month, 1).ToString("MMMM")
  95.                     Dim Y As Int16 = Me.Height - BottomSpan + 20
  96.                     Dim Color As Color = Drawing.Color.Gray
  97.                     Size = .MeasureString(Month, DFont)
  98.                     .FillRectangle(New SolidBrush(Color), PrecStart, Y, Me.Width - PrecStart - 1, 25)
  99.                     .DrawString(Month, DFont, Brushes.Black, PrecStart + (Me.Width - PrecStart) / 2 - Size.Width / 2, Y)
  100.                 End If
  101.  
  102.                 For I As Int16 = 0 To Days.Count - 1
  103.                     Points.Add(New Point(GetX(I, (Pix / Math.Ceiling([Step]))), Me.Height - BottomSpan - ((Me.Height - BottomSpan) * Hours(Days(I)) / Max)))
  104.                 Next
  105.  
  106.                 For I As Int16 = 0 To 10
  107.                     Size = .MeasureString(String.Format("{0:N2}", Max * I / 10), DFont)
  108.                     .DrawLine(Pens.Black, LeftSpan - 3, CInt(Me.Height - BottomSpan - ((Me.Height - BottomSpan) * I / 10)), LeftSpan, CInt(Me.Height - BottomSpan - ((Me.Height - BottomSpan) * I / 10)))
  109.                     .DrawString(String.Format("{0:N2}", Max * I / 10), DFont, Brushes.Black, 50 - Size.Width, CInt(Me.Height - BottomSpan - ((Me.Height - BottomSpan) * I / 10)))
  110.                 Next
  111.  
  112.                 Dim IntPen As New Pen(Color.Blue)
  113.                 Dim RealPen As New Pen(Color.Black)
  114.  
  115.                 IntPen.DashCap = Drawing2D.DashCap.Round
  116.                 IntPen.DashStyle = Drawing2D.DashStyle.Dash
  117.                 If btnLine.Text = "Interpolazione" Then
  118.                     .DrawCurve(IntPen, Points.ToArray)
  119.                 Else
  120.                     .DrawLines(RealPen, Points.ToArray)
  121.                 End If
  122.             End With
  123.         Catch Ex As Exception
  124.             RemoveHandler Me.Paint, AddressOf GraphTimePerDay_Paint
  125.             MessageBox.Show("Impossibile disegnare il grafico con questi dati!", "Errore grafico", MessageBoxButtons.OK, MessageBoxIcon.Error)
  126.         End Try
  127.     End Sub
  128.  
  129.     Private Sub btnLine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine.Click
  130.         If btnLine.Text = "Interpolazione" Then
  131.             btnLine.Text = "Normale"
  132.         Else
  133.             btnLine.Text = "Interpolazione"
  134.         End If
  135.         Me.Refresh()
  136.     End Sub
  137. End Class