Refreshing a drawing
-
Hi everyone I got a small problem I have devloped hotel software and I am trying to draw achart that shows %age occupancy. I would like to draw a pie chart on a form. I have managed to do this so far,the chart is being drawn when the form loads. I would like the form to be able to redraw when a user specifies a different date such that you can view the past occupancy. The code I am using is shown below. I hope you will be able to help me. twisted F8 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim TotalCount As Single Dim rect As Rectangle = New Rectangle(100, 105, 150, 150) For Each gd As GraphData In occupancy TotalCount += gd.percentageocc Next Dim g As Graphics = e.Graphics ' Create variables to hold the changing values of Angles g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality Dim StartAngle As Single = 0 Dim SweepAngle As Single = 0 For Each gd As GraphData In occupancy SweepAngle = 360 * gd.percentageocc / TotalCount g.FillPie(New SolidBrush(gd.Clr), rect, StartAngle, SweepAngle) StartAngle += SweepAngle Next ' g.DrawPie(New Pen(Color.Brown), rect, StartAngle, SweepAngle) ' Create a Brush to draw the text Dim TextBrsh As Brush = New SolidBrush(Color.Black) ' Create a Font object instance for text display Dim TextFont As New Font("Arial", 12, FontStyle.Bold) g.DrawString("Chart Key", TextFont, TextBrsh, 310, 100) Dim pxFromTop As Integer = 135 For Each gd As GraphData In occupancy ' Draw bullet g.FillEllipse(New SolidBrush(gd.Clr), 310, pxFromTop, 15, 15) ' Draw line round bullet. g.DrawEllipse(New Pen(Color.Black), 310, pxFromTop, 15, 15) ' Draw the text - color coded g.DrawString(gd.Description & " (" & gd.percentageocc & "%)", TextFont, TextBrsh, 360, pxFromTop) ' Increase gap from Top for next line pxFromTop += 30 Next End Sub
-
Hi everyone I got a small problem I have devloped hotel software and I am trying to draw achart that shows %age occupancy. I would like to draw a pie chart on a form. I have managed to do this so far,the chart is being drawn when the form loads. I would like the form to be able to redraw when a user specifies a different date such that you can view the past occupancy. The code I am using is shown below. I hope you will be able to help me. twisted F8 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim TotalCount As Single Dim rect As Rectangle = New Rectangle(100, 105, 150, 150) For Each gd As GraphData In occupancy TotalCount += gd.percentageocc Next Dim g As Graphics = e.Graphics ' Create variables to hold the changing values of Angles g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality Dim StartAngle As Single = 0 Dim SweepAngle As Single = 0 For Each gd As GraphData In occupancy SweepAngle = 360 * gd.percentageocc / TotalCount g.FillPie(New SolidBrush(gd.Clr), rect, StartAngle, SweepAngle) StartAngle += SweepAngle Next ' g.DrawPie(New Pen(Color.Brown), rect, StartAngle, SweepAngle) ' Create a Brush to draw the text Dim TextBrsh As Brush = New SolidBrush(Color.Black) ' Create a Font object instance for text display Dim TextFont As New Font("Arial", 12, FontStyle.Bold) g.DrawString("Chart Key", TextFont, TextBrsh, 310, 100) Dim pxFromTop As Integer = 135 For Each gd As GraphData In occupancy ' Draw bullet g.FillEllipse(New SolidBrush(gd.Clr), 310, pxFromTop, 15, 15) ' Draw line round bullet. g.DrawEllipse(New Pen(Color.Black), 310, pxFromTop, 15, 15) ' Draw the text - color coded g.DrawString(gd.Description & " (" & gd.percentageocc & "%)", TextFont, TextBrsh, 360, pxFromTop) ' Increase gap from Top for next line pxFromTop += 30 Next End Sub
-
Hi everyone I got a small problem I have devloped hotel software and I am trying to draw achart that shows %age occupancy. I would like to draw a pie chart on a form. I have managed to do this so far,the chart is being drawn when the form loads. I would like the form to be able to redraw when a user specifies a different date such that you can view the past occupancy. The code I am using is shown below. I hope you will be able to help me. twisted F8 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) Dim TotalCount As Single Dim rect As Rectangle = New Rectangle(100, 105, 150, 150) For Each gd As GraphData In occupancy TotalCount += gd.percentageocc Next Dim g As Graphics = e.Graphics ' Create variables to hold the changing values of Angles g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality Dim StartAngle As Single = 0 Dim SweepAngle As Single = 0 For Each gd As GraphData In occupancy SweepAngle = 360 * gd.percentageocc / TotalCount g.FillPie(New SolidBrush(gd.Clr), rect, StartAngle, SweepAngle) StartAngle += SweepAngle Next ' g.DrawPie(New Pen(Color.Brown), rect, StartAngle, SweepAngle) ' Create a Brush to draw the text Dim TextBrsh As Brush = New SolidBrush(Color.Black) ' Create a Font object instance for text display Dim TextFont As New Font("Arial", 12, FontStyle.Bold) g.DrawString("Chart Key", TextFont, TextBrsh, 310, 100) Dim pxFromTop As Integer = 135 For Each gd As GraphData In occupancy ' Draw bullet g.FillEllipse(New SolidBrush(gd.Clr), 310, pxFromTop, 15, 15) ' Draw line round bullet. g.DrawEllipse(New Pen(Color.Black), 310, pxFromTop, 15, 15) ' Draw the text - color coded g.DrawString(gd.Description & " (" & gd.percentageocc & "%)", TextFont, TextBrsh, 360, pxFromTop) ' Increase gap from Top for next line pxFromTop += 30 Next End Sub
Hi, some remarks: 1. You can force a redraw by calling Invalidate() on the Control that does the drawing, in your case a Form. 2. Please, please publish code snippets inside PRE tags; it reads much better. 3. You must call Dispose() on those objects that you create and havre such a class, as in SolidBrush and Pen. 4. For predefined colors there also exist predefined objects (Pen, Brush), see Pens class and Brushes class. You should not try to dispose these, since you did not create them! :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google