Imports System
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Partial Class _Default
Inherits System.Web.UI.Page
Private myImage As Bitmap
Private g As Graphics
Private p() As Integer = {1000000, 600000, 2500000, 80000}
Private towns() As String = {"A", "B", "C", "D"}
Private myBrushes(4) As Brush
Sub page_Load()
' Create an in-memory bitmap where you will draw the image.
' The Bitmap is 300 pixels wide and 200 pixels high.
myImage = New Bitmap(500, 300, PixelFormat.Format32bppRgb)
' Get the graphics context for the bitmap.
g = Graphics.FromImage(myImage)
' Create the brushes for drawing
myBrushes(0) = New SolidBrush(Color.Red)
myBrushes(1) = New SolidBrush(Color.Blue)
myBrushes(2) = New SolidBrush(Color.Yellow)
myBrushes(3) = New SolidBrush(Color.Green)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
' Variables declaration
Dim i As Integer
Dim total As Integer
Dim percentage As Double
Dim angleSoFar As Double = 0.0
' Caculates the total
For i = 0 To p.Length - 1
total += p(i)
Next
' Draws the pie chart
For i = 0 To p.Length - 1
percentage = p(i) / total * 360
g.FillPie(myBrushes(i), 25, 25, 250, 250, CInt(angleSoFar), CInt(percentage))
angleSoFar += percentage
' Draws the lengend
g.FillRectangle(myBrushes(i), 350, 25 + (i * 50), 25, 25)
' Label the towns
g.DrawString("Town " & towns(i), New Font("Verdana", 8, FontStyle.Bold), Brushes.Brown, 390, 25 + (i * 50) + 10)
Next
End Sub
End Class
In
the above code which is used to generate pie chart Im getting
error:object reference not set to an instance of an object at
g.FillPie(myBrushes(i), 25, 25, 250, 250, CInt(angleSoFar),
CInt(percentage))
Please help me out with a solution for such kind of error
Loading
Meetu ChoudharyPosted Aug 29, 2009, 6:04 AM
' Get the graphics context for the bitmap.
g = Graphics.FromImage(myImage)
You haven't initilaized the g object try using new keyword. in your declaration section.
Roei BarPosted Aug 29, 2009, 4:38 AM
' Get the graphics context for the bitmap.
g = Graphics.FromImage(myImage)
this might not be initialized