Tipp 0206 Balkendiagramm aus Zufallszahlen
Autor/Einsender:
Datum:
  Horst Quincy
16.12.2010
Entwicklungsumgebung:   VB.Net 2008
Framework:   2.0
In dem Beispiel werden 5 Zufallszahlen und eine Summe ermittelt. Die Zahlen werden in Textboxen ausgeworfen und mittels CreateGrafics aus Labeln erstellt. Die Zufallszahlen dienen zu Vereinfachung und Darstellung der Methode. Denkbar ist auch die Anbindung an z.B. Datenbanken.
 
Imports System.Drawing.Drawing2D

Public Class Form1

  ' Die hier gewählten Zufallzahlen dienen nur zur Demonstration,

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal _
          e As System.EventArgs) Handles Button1.Click
    Label1.Visible = False
    Label2.Visible = False
    Label3.Visible = False
    Label4.Visible = False
    Label5.Visible = False
    Label6.Visible = False

    Zufall()
    Rechnen()
    Ausrichten()
    Timer2.Enabled = True
    Label1.Visible = True
    Label2.Visible = True
    Label3.Visible = True
    Label4.Visible = True
    Label5.Visible = True
    Label6.Visible = True

  End Sub

  Private Sub Zufall()
    Dim rnd As New Random
    Dim b(5) As Byte
    Dim j As Integer
    rnd.NextBytes(b)

    For j = 0 To 1000
      TextBox1.Text = (b(0))
      TextBox2.Text = (b(1))
      TextBox3.Text = (b(2))
      TextBox4.Text = (b(3))
      TextBox5.Text = (b(4))
    Next j
  End Sub

  Private Sub Rechnen()
    Dim ctl As Control
    For Each ctl In Me.Controls
      If ctl.Name.StartsWith("TextBox") = True Then
        If ctl.Text = "" Then ctl.Text = "0"
      End If
    Next
    TextBox6.Text = CStr(CDbl(TextBox1.Text) + _
                    CDbl(TextBox2.Text) + CDbl(TextBox3.Text) + _
                    CDbl(TextBox4.Text) + CDbl(TextBox5.Text))

    ' Textbox6 ist die Summe also 100%
    Dim z As Double = CStr(CDbl(475) / CDbl(TextBox6.Text))
    z = Math.Round(z, 5)

    Label6.Height = CStr(CDbl(TextBox6.Text) * (z))

    ' Label = Balkendiagrammbalken
    Label1.Height = CStr(CDbl(TextBox1.Text) * (z))
    Label2.Height = CStr(CDbl(TextBox2.Text) * (z))
    Label3.Height = CStr(CDbl(TextBox3.Text) * (z))
    Label4.Height = CStr(CDbl(TextBox4.Text) * (z))
    Label5.Height = CStr(CDbl(TextBox5.Text) * (z))
  End Sub

  Private Sub Ausrichten()
    Label1.Location = New Point(20, 520 - _
           Convert.ToDouble(Label1.Height))
    Label2.Location = New Point(100, 520 - _
           Convert.ToDouble(Label2.Height))
    Label3.Location = New Point(180, 520 - _
           Convert.ToDouble(Label3.Height))
    Label4.Location = New Point(260, 520 - _
           Convert.ToDouble(Label4.Height))
    Label5.Location = New Point(340, 520 - _
           Convert.ToDouble(Label5.Height))
    Label6.Location = New Point(20, 520 - _
           Convert.ToDouble(Label6.Height))
  End Sub

  Private Sub Farbfüllung()
    Dim z1 As Double = CStr(CDbl(TextBox1.Text * 100) / _
        CDbl(TextBox6.Text))
    z1 = Math.Round(z1, 2)
    Dim z2 As Double = CStr(CDbl(TextBox2.Text * 100) / _
        CDbl(TextBox6.Text))
    z2 = Math.Round(z2, 2)
    Dim z3 As Double = CStr(CDbl(TextBox3.Text * 100) / _
        CDbl(TextBox6.Text))
    z3 = Math.Round(z3, 2)
    Dim z4 As Double = CStr(CDbl(TextBox4.Text * 100) / _
        CDbl(TextBox6.Text))
    z4 = Math.Round(z4, 2)
    Dim z5 As Double = CStr(CDbl(TextBox5.Text * 100) / _
        CDbl(TextBox6.Text))
    z5 = Math.Round(z5, 2)
    Dim z6 As Double = CStr(CDbl(TextBox6.Text * 100) / _
        CDbl(TextBox6.Text))
    z6 = Math.Round(z6, 2)

    If TextBox1.Text <> "0" Then
      Dim g1 As Graphics
      g1 = Label1.CreateGraphics
      Dim rect1 As Rectangle = _
          New Rectangle(0, 0, Label1.Width, Label1.Height)
      Dim Brush1 As LinearGradientBrush = _
          New LinearGradientBrush(rect1, Color.ForestGreen, _
          Color.Black, LinearGradientMode.Vertical)
      g1.FillRectangle(Brush1, rect1)
      Dim yBrush2 As New SolidBrush(Color.White)
      g1.DrawString(z1 & " %", New Font("Arial", 8), _
         yBrush2, 10, 2)
    End If

    If TextBox2.Text <> "0" Then
      Dim g2 As Graphics
      g2 = Label2.CreateGraphics

      Dim rect2 As Rectangle = _
          New Rectangle(0, 0, Label2.Width, Label2.Height)
      Dim Brush3 As LinearGradientBrush = _
          New LinearGradientBrush(rect2, Color.Fuchsia, _
          Color.Black, LinearGradientMode.Vertical)
      g2.FillRectangle(Brush3, rect2)
      Dim yBrush4 As New SolidBrush(Color.White)
      g2.DrawString(z2 & " %", New Font("Arial", 8), _
         yBrush4, 10, 2)
    End If

    If TextBox3.Text <> "0" Then
      Dim g3 As Graphics
      g3 = Label3.CreateGraphics

      Dim rect3 As Rectangle = _
          New Rectangle(0, 0, Label3.Width, Label3.Height)
      Dim Brush5 As LinearGradientBrush = _
          New LinearGradientBrush(rect3, Color.MediumSlateBlue, _
          Color.Black, LinearGradientMode.Vertical)
      g3.FillRectangle(Brush5, rect3)
      Dim yBrush6 As New SolidBrush(Color.White)
      g3.DrawString(z3 & " %", New Font("Arial", 8), _
         yBrush6, 10, 2)
    End If

    If TextBox4.Text <> "0" Then
      Dim g4 As Graphics
      g4 = Label4.CreateGraphics

      Dim rect4 As Rectangle = _
          New Rectangle(0, 0, Label4.Width, Label4.Height)
      Dim Brush7 As LinearGradientBrush = _
          New LinearGradientBrush(rect4, Color.PaleGoldenrod, _
          Color.Black, LinearGradientMode.Vertical)
      g4.FillRectangle(Brush7, rect4)
      Dim yBrush8 As New SolidBrush(Color.Black)
      g4.DrawString(z4 & " %", New Font("Arial", 8), _
         yBrush8, 10, 2)
    End If

    If TextBox5.Text <> "0" Then
      Dim g5 As Graphics
      g5 = Label5.CreateGraphics

      Dim rect5 As Rectangle = _
          New Rectangle(0, 0, Label5.Width, Label5.Height)
      Dim Brush8 As LinearGradientBrush = _
          New LinearGradientBrush(rect5, Color.Tomato, _
          Color.Black, LinearGradientMode.Vertical)
      g5.FillRectangle(Brush8, rect5)
      Dim yBrush9 As New SolidBrush(Color.Black)
      g5.DrawString(z5 & " %", New Font("Arial", 8), _
         yBrush9, 10, 2)
    End If

    If TextBox6.Text <> "0" Then
      Dim g6 As Graphics
      g6 = Label6.CreateGraphics

      Dim rect6 As Rectangle = _
          New Rectangle(0, 0, Label6.Width, Label6.Height)
      Dim Brush10 As LinearGradientBrush = _
          New LinearGradientBrush(rect6, Color.Yellow, _
          Color.Black, LinearGradientMode.Vertical)
      g6.FillRectangle(Brush10, rect6)
      Dim yBrush11 As New SolidBrush(Color.Black)
      Dim yBrush12 As New SolidBrush(Color.Red)

      ' hier werden noch mal alle Prozentzahlen vermerkt für den
      ' Fall daß der Balken zu niedrig ist
      g6.DrawString(z1 & " %", New Font("Arial", 8), _
         yBrush11, 10, 2)
      g6.DrawString(z2 & " %", New Font("Arial", 8), _
         yBrush11, 90, 2)
      g6.DrawString(z3 & " %", New Font("Arial", 8), _
         yBrush11, 170, 2)
      g6.DrawString(z4 & " %", New Font("Arial", 8), _
         yBrush11, 250, 2)
      g6.DrawString(z5 & " %", New Font("Arial", 8), _
         yBrush11, 330, 2)
      g6.DrawString(z6 & " %", New Font("Arial", 8), _
         yBrush12, 420, 2)
    End If
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load

    'Form erscheint immer mittig
    Me.Left = CInt((Screen.PrimaryScreen.WorkingArea.Width - _
       Me.Width) / 2)
    Me.Top = CInt((Screen.PrimaryScreen.WorkingArea.Height - _
       Me.Height) / 2)
  End Sub

  Private Sub Timer2_Tick(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Timer2.Tick
    Farbfüllung()
  End Sub
End Class
 
Weitere Links zum Thema
Kuchendiagramm erstellen

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (15 kB) Downloads bisher: [ 159 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Donnerstag, 15. Dezember 2011