dcsimg
Results 1 to 4 of 4

Thread: 3D Histogram Example

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,518

    Thumbs up 3D Histogram Example

    This is an example for creating a 3D Histogram using GDI+ in VB.Net (and c# added 17/7/2015)

    Name:  23-06-2015 04.11.02.jpg
Views: 819
Size:  17.5 KB

    All of the drawing is done with Transforms in the PictureBox_Paint event, and the Histogram has MouseOver features:

    Code:
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If propertiesDictionary("Total Income") <> 0 Then
    
            Dim totalHeight As Integer = CInt(propertiesDictionary("Total Income"))
            Dim topHeight As Integer = CInt(totalHeight * 0.05)
    
            ' Flip vertically and scale to fit.
            Dim scalex As Single = 20.0F
            Dim scaley As Single = CSng(-PictureBox1.Height / (totalHeight * 1.5))
            e.Graphics.ScaleTransform(scalex, scaley, MatrixOrder.Append)
    
            ' Translate so (0, MAX_VALUE) maps to the origin.
            e.Graphics.TranslateTransform(0, PictureBox1.Height, MatrixOrder.Append)
    
            Dim front() As PointF = { _
            New PointF(0, 0), _
            New PointF(0, totalHeight), _
            New PointF(2, totalHeight), _
            New PointF(2, 0), _
            New PointF(0, 0) _
            }
    
            Dim top() As PointF = { _
                New PointF(0, totalHeight), _
                New PointF(1, totalHeight + topHeight), _
                New PointF(3, totalHeight + topHeight), _
                New PointF(2, totalHeight), _
                New PointF(0, totalHeight) _
            }
    
            Dim right() As PointF = { _
                New PointF(2, totalHeight), _
                New PointF(3, totalHeight + topHeight), _
                New PointF(3, topHeight), _
                New PointF(2, 0), _
                New PointF(2, totalHeight) _
            }
    
            ' Draw the histogram.
            For x As Integer = 0 To propertiesDictionary.Count - 1
                rgns(x) = New GraphicsPath
                Dim value As Integer = CInt(propertiesDictionary.ElementAt(x).Value)
                If value > 0 Then
                    Dim offsetX As Single = CSng(x * 2.5)
    
                    Dim barFront() As PointF = Array.ConvertAll(front, Function(pf) New PointF(pf.X + offsetX, pf.Y))
                    barFront(1).Y = value
                    barFront(2).Y = value
                    e.Graphics.FillPolygon(New SolidBrush(barColors(x)), barFront)
                    e.Graphics.DrawPolygon(New Pen(Color.Black, 0), barFront)
                    rgns(x).AddPolygon(Array.ConvertAll(barFront, Function(pf) New PointF(pf.X * scalex, pf.Y * scaley)))
    
                    Dim barTop() As PointF = Array.ConvertAll(top, Function(pf) New PointF(pf.X + offsetX, value))
                    barTop(1).Y += topHeight
                    barTop(2).Y += topHeight
                    e.Graphics.FillPolygon(New SolidBrush(barColors(x)), barTop)
                    e.Graphics.DrawPolygon(New Pen(Color.Black, 0), barTop)
    
                    rgns(x).AddPolygon(Array.ConvertAll(barTop, Function(pf) New PointF(pf.X * scalex, pf.Y * scaley)))
    
                    Dim barRight() As PointF = Array.ConvertAll(right, Function(pf) New PointF(pf.X + offsetX, pf.Y))
                    barRight(0).Y = value
                    barRight(1).Y = value + topHeight
                    barRight(4).Y = value
                    e.Graphics.FillPolygon(New SolidBrush(barColors(x)), barRight)
                    e.Graphics.DrawPolygon(New Pen(Color.Black, 0), barRight)
    
                    rgns(x).AddPolygon(Array.ConvertAll(barRight, Function(pf) New PointF(pf.X * scalex, pf.Y * scaley)))
    
                End If
            Next
    
            e.Graphics.ResetTransform()
    
        Else
            Array.Clear(rgns, 0, 10)
        End If
    End Sub

    Download example project: here (c# added 17/7/2015)
    Last edited by .paul.; Jul 17th, 2015 at 12:54 AM.

  2. #2
    Member
    Join Date
    Feb 2004
    Location
    THANE
    Posts
    37

    Re: 3D Histogram Example

    Looking nice... It's useful... Thanks
    From Manoj's Desk

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,518

    Re: 3D Histogram Example

    Improved version available here...

    http://www.scproject.biz/Household_Budgets.php

  4. #4
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    546

    Re: 3D Histogram Example

    Thanks paul

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width