> Manuales > Taller de ASP.NET

Gracias a este truco de asp.net podrás generar gráficos dinámicos de forma rápida y sencilla.

Este gráfico está basado en un array de valores. En este array se almacenan los valores y las fechas, para poder sacar un gráfico en condiciones en 2D parecido al siguiente:


El código fuente para generar el gráfico es el siguiente:

Imports System.Drawing
System.Drawing
Imports System.Drawing.Imaging
System.Drawing.Imaging
Public Class graph
Class graph
Inherits System.Web.UI.Page
Region " Código generado por el Diseñador de Web Forms "
'El Diseñador de Web Forms requiere esta llamada.
<;System.Diagnostics.DebuggerStepThrough()>; Private Sub InitializeComponent()
Private Sub InitializeComponent()
End Sub
'NOTA: el Diseñador de Web Forms necesita la siguiente declaración del marcador de posición.
'No se debe eliminar o mover.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: el Diseñador de Web Forms requiere esta llamada de método
'No la modifique con el editor de código.
InitializeComponent()

End Sub
End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim objBitmap As New Bitmap(700, 200)
Dim objGraphic As Graphics = Graphics.FromImage(objBitmap)
Dim orange As New SolidBrush(Color.Orange)
Dim whiteBrush As New SolidBrush(Color.White)
Dim lineColor As New Pen(Color.Blue, 2)
objGraphic.FillRectangle(whiteBrush, 0, 0, 700, 200)

objGraphic.DrawLine(lineColor, New Point(0, 190), New Point(700, 190))
New Point(0, 190), New Point(700, 190))
objGraphic.DrawLine(lineColor, New Point(5, 5), New Point(5, 195))
New Point(5, 5), New Point(5, 195))
Dim l_arr As New ArrayList
'inserción en el array de valores para el ejemplo
l_arr.Add("800")
l_arr.Add("01/01/07")
l_arr.Add("500")
l_arr.Add("02/01/07")
l_arr.Add("1000")
l_arr.Add("03/01/07")
l_arr.Add("200")
l_arr.Add("03/01/07")
l_arr.Add("1000")
l_arr.Add("03/01/07")
l_arr.Add("1300")
l_arr.Add("03/01/07")
l_arr.Add("1000")
l_arr.Add("03/01/07")
l_arr.Add("1000")
l_arr.Add("03/01/07")
l_arr.Add("1000")
l_arr.Add("03/01/07")
l_arr.Add("1000")
l_arr.Add("03/01/07")
Dim i As Int32 = 0
Dim pos As Int32 = 0
Dim PointValue As New PointF(-30, 0)
Dim PointDate As New PointF(-30, 190)
Dim objFont As New Font("Verdana", 10)
Dim FontDate As New Font("Verdana", 7)
Dim wb2 As New SolidBrush(Color.Black)
Dim x As Int32 = -50
Dim maxValue As Int32 = 0
'calculo del máximo valor de la gráfica
i = 0

While i <; l_arr.Count
If l_arr(i) >; maxValue Then
maxValue = l_arr(i)

End If
i += 2

End While
'creación de todos los rectángulos de la gráfica
i = 0

While i <; l_arr.Count
x += 70

Dim aux As Int32
aux = (l_arr(i) * 189) / (maxValue + 200)

objGraphic.FillRectangle(orange, x, 189 - aux, 40, aux)

PointValue.X = x

PointDate.X = x - 10

objGraphic.DrawString(l_arr(i).ToString, objFont, wb2, PointValue)

objGraphic.DrawString(l_arr(i + 1).ToString, FontDate, wb2, PointDate)

i += 2
End While
Response.ContentType = "image/gif"
objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
End Sub
End Class

Puedes descargarte todo el ejemplo desde aquí

Pol Salvat

Manual