Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  GDI+ Chart Success Part 7: Printing Charts  »  Line Chart - Drawing
 »  Home  »  Windows Development  »  Graphics  »  GDI+ Chart Success Part 7: Printing Charts  »  Line Chart - Drawing
GDI+ Chart Success Part 7: Printing Charts
by Ged Mead | Published  10/16/2006 | .NET Newbie Graphics | Rating:
Line Chart - Drawing

Introduction

    Parts 5 and 6 of this series contain detailed descriptions of how you can create and display various line charts.   For the printing demonstration I thought I would just stick to the final version covered in Part 6, that is the so called dynamic line chart.    This is mainly because the procedure printing the line chart from Part 5 is the same as that we just covered for the bar chart, i.e. grab the PictureBox image and print it.

   The chart in Part 6 was slightly more complex because it used two PictureBox controls side by side, one to display the vertical axis details and the other to display the changing chart display.  We would therefore need a way of printing both pictureboxes side by side if we want to replicate what the user sees on screen and put it on paper.  So that's what we are going to do.

   The code that enables a user to create the line chart is repeated below.  For full details of how it works, plus info on which controls will need to be placed on the form you will need to visit the Part 6 article here.  

' Variables to store the changing values to be charted (previous and current)
Private OldValue As Single = 0
Private NewValue As Single = 0
Dim XMove As Integer = 4
Dim Chunks As Integer = 12


Private Sub More_Generic_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  ' Paint Guidelines on picGraph
 picGraph.Image = DisplayGuidelines(picGraph, Chunks)
  ' Paint Guidelines and Numbers on picValues
 picValues.Image = Me.DisplayVerticalValues(picValues, Chunks, SBUserValue.Minimum, SBUserValue.Maximum)
End Sub


++++++++++++++++++++++++++++

Private Function DisplayGuidelines(ByVal PicBox As PictureBox, ByVal chunks As Integer) As Bitmap
' Step 1
' Create a bitmap to draw on and grab its Graphics Object

Dim bm As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
' Step 2
' Draw guidelines on main chart.
' Get the total height available and split it into chunks

Dim total As Integer = PicBox.Height
Dim chunk As Single = total / chunks
' Step 3
For i As Single = chunk To total Step chunk
  gr.DrawLine(Pens.Green, 0, i, PicBox.Width, i)
Next i
' Step 4
' return the results.

Return bm
' Step 5
gr.Dispose()
End Function
 
Private Function DisplayVerticalValues(ByVal PB As PictureBox, ByVal HowManyChunks As Single, _
ByVal MinValue As Single, ByVal MaxValue As Single) As Bitmap
' Step 1
Dim bmp As New Bitmap(PB.Width, PB.Height)
Dim gv As Graphics = Graphics.FromImage(bmp)
' Step 2
' Draw guidelines on values strip
' Get the total height available and split it into chunks
' This value represents a number of pixels

Dim TotalPixels As Integer = PB.Height
Dim SingleChunk As Single = TotalPixels / HowManyChunks
 For i As Single = SingleChunk To TotalPixels Step SingleChunk
   gv.DrawLine(Pens.Green, 0, i, PB.Width, i)
 Next i
' Step 3
' Draw Numbers as Text, correctly spaced vertically
' Begin with the highest value allowed

Dim NextMarker As Integer = MaxValue
' Calculate the plottable range
Dim ValueRange As Integer = MaxValue - MinValue
' Draw the numbers, decrementing values proportionately each time through the loop
 For i As Single = 0 To TotalPixels Step SingleChunk
   gv.DrawString(
   CStr(NextMarker), New Font("Verdana", 8, FontStyle.Regular), Brushes.Black, 1, i)
   NextMarker -= (ValueRange / HowManyChunks)
 Next
' Step 4
Return bmp
' Step 5
gv.Dispose()
End Function
 
Private Function DisplayGuidelinesAndChart(ByVal PicBox As PictureBox, ByVal chunks As Integer, _
ByVal XMove As Integer, ByVal NewValue As Single, ByVal Min As Single, ByVal Max As Single) As Bitmap
 
' Step 1
' Grab the current image (the latest version of the chart)

Dim bm As New Bitmap(PicBox.Width, PicBox.Height)
Dim gr As Graphics = Graphics.FromImage(bm)
' Step 2
' Get the total height available and split it into chunks

Dim total As Integer = PicBox.Height
Dim chunk As Single = total / chunks
' Tack missing guidelines to right hand side on the Graphics object.
 For i As Single = chunk To total Step chunk
   gr.DrawLine(Pens.Green, PicBox.Width - XMove, i, PicBox.Width, i)
 Next i
' Step 3
' Draw this grabbed image, placing it XMove pixels to the left
 If Not IsNothing(PicBox.Image) Then
   gr.DrawImage(PicBox.Image, -XMove, 0)
 End If
 
' Step 4
' Plot the new value.
' Calculate the scaling required to make full use of the height of
' the PictureBox
Dim ValueRange As Single = Max - Min
Dim vScale As Single = PicBox.Height / ValueRange
' Apply the scale to the current value
NewValue *= vScale
' Step 5
' Shift start point from top left to bottom left.

gr.TranslateTransform(0, PicBox.Height)
' Step 6
' Draw the next line segment on the Graphics object.
' If Min is > 0 then you need to shift the drawing down once again,
' this time to put the Min value on the horizontal axis

If Min > 0 Then gr.TranslateTransform(0, Min * vScale)
 
Dim p As Pen = New Pen(Color.Navy, 2)
gr.DrawLine(p, PicBox.Width - 1 - XMove, -OldValue, PicBox.Width - 1, -NewValue)
OldValue = NewValue
' Step 7
' Return the Bitmap .

Return bm
' Step 8
' All done

gr.Dispose()
End Function


Private Sub _3rd_More_Generic_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
' Paint Guidelines on picGraph

picGraph.Image = DisplayGuidelines(picGraph, Chunks)
' Paint Guidelines and Numbers on picValues

picValues.Image = Me.DisplayVerticalValues(picValues, Chunks, SBUserValue.Minimum, SBUserValue.Maximum)
End Sub

    If you use a Horizontal ScrollBar as we did in the previous article, then this code will cause the above procedures to draw the chart:

Private Sub SBUserValue_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SBUserValue.ValueChanged
  picGraph.Image = DisplayGuidelinesAndChart(picGraph, Chunks, XMove,
  Me.SBUserValue.Value, SBUserValue.Minimum, SBUserValue.Maximum)
  lblValue.Text = SBUserValue.Value.ToString
End Sub


Sponsored Links