0
Answer

Drawing pie charts in asp.net-Help me

Ameer Basha

Ameer Basha

17y
2.2k
1

I am able to display pie chart.My problem is i want to display text inside the  pie charts as shown below.please help me....

this is my code:

Imports System.Drawing

Imports System.Drawing.Imaging

Partial Class _Default

Inherits System.Web.UI.Page

 

' Variables declaration

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

 

 

' Initialises the bitmap, graphics context and pens for drawing

Private Sub initialiseGraphics()

Try

' 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, 350, PixelFormat.Format32bppRgb)

' Get the graphics context for the bitmap.

g = Graphics.FromImage(myImage)

' Create the brushes for drawing

createBrushes()

Catch ex As Exception

Throw ex

End Try

End Sub

' Method to create brushes of specific colours

Private Sub createBrushes()

Try

myBrushes(0) = New SolidBrush(Color.Red)

myBrushes(1) = New SolidBrush(Color.Blue)

myBrushes(2) = New SolidBrush(Color.Yellow)

myBrushes(3) = New SolidBrush(Color.Green)

Catch ex As Exception

Throw ex

End Try

End Sub

' Draws the piechart

Private Sub drawPieChart(ByVal g As Graphics)

Try

' Variables declaration

Dim i As Integer

Dim total As Integer

Dim percentage As Double

Dim angleSoFar As Double = 0.0

g.DrawString("Total Population", New Font("Tahoma", 16), Brushes.Black, New PointF(50, 5))

 

' 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), 20, 50, 250, 250, CInt(angleSoFar), CInt(percentage))

g.DrawString(towns(i), New Font("Verdana", 12, FontStyle.Bold), Brushes.Black, 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

Catch ex As Exception

Throw ex

End Try

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Initialise graphics

initialiseGraphics()

g.Clear(Color.WhiteSmoke)

' draws the barchart

drawPieChart(g)

' Render the image to the HTML output stream.

myImage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

End Sub

End Class

 

 

The output:

 

i want to display % values inside the pie....please help me

Next Recommended Forum