Click here to Skip to main content
15,886,362 members
Articles / Web Development / IIS
Article

Create Dynamic Line Chart graph

Rate me:
Please Sign up or sign in to vote.
1.05/5 (21 votes)
25 May 200619 min read 99.7K   34   14
Create Dynamic Line Chart graph

Introduction

Creating Line Charts from Database Data<o:p>

This article shows you how to create a line chart for web forms using VB.NET. I think there are several third party components available for creating line chart and graphs. It wouldn't get you a knowledge on how internally it works. Hence I have created a line chart using GDI+ in VB.NET. Besides this, you will be able to create a line chart in a quick manner <o:p>

 Using the code <o:p>

Below are the code for functions used to create the line chart <o:p>


<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"><o:lock aspectratio="t" v:ext="edit"><v:shape id="_x0000_i1025" style="WIDTH: 7.5pt; HEIGHT: 3pt" alt="" type="#_x0000_t75"><v:imagedata o:href="http://msdn.microsoft.com/msdnmag/images/dingbats/indent.gif" src="file:///C:\DOCUME~1\hssp5089\LOCALS~1\Temp\msohtml1\03\clip_image001.gif">Now that you know how to create a simple image from an ASP.NET Web page, you can create more complex (and useful) images. For the remainder of this article I'll look at how to use the .NET Framework drawing classes to create a Line chart from database information. I'll build all of this functionality into a set of functions in an ASP.NET Web page that will end up streaming the dynamically created Line chart's binary content to the Response object's OutputStream.
<v:shape id="_x0000_i1026" style="WIDTH: 7.5pt; HEIGHT: 3pt" alt="" type="#_x0000_t75"><v:imagedata o:href="http://msdn.microsoft.com/msdnmag/images/dingbats/indent.gif" src="file:///C:\DOCUME~1\hssp5089\LOCALS~1\Temp\msohtml1\03\clip_image001.gif">While creating a set of page-level functions to display a line chart will accomplish the task at hand, a more reusable solution would be to encapsulate this functionality into a custom-defined ASP.NET Web control or compiled custom control. One disadvantage of such an approach, though, would be that the custom-defined ASP.NET Web control or compiled custom control would have to save the image's file to the Web server's file system and then render it from an appropriate img tag. While this isn't difficult to accomplish, you'll have to deal with the disadvantages I mentioned earlier, including the fact that each time a chart is generated you'll keep adding to the list of images on the Web server's file system.<o:p>

1) Create the another vb Class file  for it That will be Creating the Line Graph according to user Sessions :===<o:p>

The code for the class file is over and now we need to create an instance of <o:p>

this file in a Web Form which needs line graph, and set appropriate properties. <o:p>

Here I'm using Score Comparison report as an example (date Vs. Attrition & Absentisum) to

This Class file  will be generate the Line Chart According to your Fatched Data Values from database<o:p>

draw a line graph to compare the runs scored by both teams using line graph<o:p>

LineChart Class vb class file name Like (LineChartAbs.vb) code<o:p>

‘ Imports These Namespaces<o:p>

Imports System.Data<o:p>

Imports System.Drawing<o:p>

Imports System.Drawing.Drawing2D<o:p>

Imports System.Drawing.Imaging<o:p>

Imports System.Web.UI<o:p>

<o:p> 

<o:p> 

Public Class LineChartAbs<o:p>

    Public b As Bitmap<o:p>

    Public Title As String = "Default Title"<o:p>

    Public chartValues As ArrayList = New ArrayList<o:p>

    Public Xorigin As Single = 0<o:p>

    Public Yorigin As Single = 0<o:p>

    Public ScaleX As Single<o:p>

    Public ScaleY As Single<o:p>

    Public Xdivs As Single = 2<o:p>

    Public Ydivs As Single = 2<o:p>

    Private Width As Integer<o:p>

    Private Height As Integer<o:p>

    Private g As Graphics<o:p>

    Private p As Page<o:p>

    Private btn As Button<o:p>

    Structure datapoint<o:p>

        Public x As Single<o:p>

        Public y As Single<o:p>

        Public xCaption As String<o:p>

        Public yCaption As String<o:p>

        Public ATTRAVERAGE As String<o:p>

        Public valid As Boolean<o:p>

    End Structure<o:p>

<o:p> 

    Public Sub New(ByVal myWidth As Integer, ByVal myHeight As Integer, ByVal myPage As Page)<o:p>

        Width = myWidth<o:p>

        Height = myHeight<o:p>

        ScaleX = myWidth<o:p>

        ScaleY = myHeight<o:p>

        b = New Bitmap(myWidth, myHeight)<o:p>

        g = Graphics.FromImage(b)<o:p>

        p = myPage<o:p>

    End Sub<o:p>

 <o:p>

    Public Sub AddValue(ByVal x As Integer, ByVal y As Double, ByVal xCaption As String, ByVal yCaption As String)<o:p>

        Dim myPoint As datapoint<o:p>

        myPoint.x = x<o:p>

        myPoint.y = y<o:p>

        myPoint.xCaption = xCaption<o:p>

        myPoint.yCaption = yCaption<o:p>

        'myPoint.ATTRAVERAGE = ATTRAVERAGE<o:p>

        myPoint.valid = True<o:p>

        chartValues.Add(myPoint)<o:p>

    End Sub<o:p>

    Public Sub AddValueN(ByVal x As Integer, ByVal y As Double, ByVal xCaption As String, ByVal ATTRAVERAGE As String)<o:p>

        Dim myPoint As datapoint<o:p>

        myPoint.x = x<o:p>

        myPoint.y = y<o:p>

        myPoint.xCaption = xCaption<o:p>

        'myPoint.yCaption = yCaption<o:p>

        myPoint.ATTRAVERAGE = ATTRAVERAGE<o:p>

        myPoint.valid = True<o:p>

        chartValues.Add(myPoint)<o:p>

    End Sub<o:p>

<o:p> 

    Public Sub AddValue(ByVal x As Integer, ByVal y As Double)<o:p>

        AddValue(x, y, x.ToString, y.ToString())<o:p>

         <o:p>

    End Sub<o:p>

    Public Sub DisplayValues()<o:p>

        Dim i As Integer<o:p>

        Dim Labelvalue As String<o:p>

        Dim blackBrushN As Brush = New SolidBrush(Color.Black)<o:p>

        Dim axesFontN As Font = New Font("arial", 10)<o:p>

        Dim ChartInset As Integer = 500<o:p>

        Dim ChartWidth As Integer = Width - (2 * ChartInset)<o:p>

        Dim ChartHeight As Integer = Height - (2 * ChartInset)<o:p>

        For i = 0 To chartValues.Count<o:p>

            If i >= chartValues.Count Then<o:p>

                Labelvalue = ""<o:p>

            Else<o:p>

    Labelvalue = CType(chartValues(i), datapoint).xCaption<o:p>

    Labelvalue = Labelvalue & "-" & CType(chartValues(i), datapoint).yCaption<o:p>

 <o:p>

    g.DrawString(Labelvalue, axesFontN, blackBrushN, Width + 1, Height)<o:p>

      <o:p>

            End If<o:p>

        Next<o:p>

    End Sub<o:p>

<o:p> 

    'Public Sub Draw(ByVal filename As String)<o:p>

        Public Sub Draw()<o:p>

        Dim i As Integer<o:p>

        Dim x As Single<o:p>

        Dim y As Single<o:p>

        Dim y1, y2 As Single<o:p>

        Dim x0 As Single<o:p>

        Dim y0 As Single<o:p>

        Dim PrintAonDot As Integer<o:p>

        Dim myLabel As String<o:p>

<o:p> 

        Dim d1 As String<o:p>

        Dim m1, m2, m3 As String<o:p>

<o:p> 

        Dim blackPen As Pen = New Pen(Color.Black, 1)<o:p>

        Dim RedPen As Pen = New Pen(Color.FromArgb(186, 5, 5), 1)<o:p>

        Dim bluePen As Pen = New Pen(Color.Blue, 1)<o:p>

        Dim blueBrush As Brush = New SolidBrush(Color.Blue)<o:p>

        Dim RedBrush As Brush = New SolidBrush(Color.FromArgb(109, 44, 0))<o:p>

        Dim MarunBrush As Brush = New SolidBrush(Color.FromArgb(109, 44, 0))<o:p>

        Dim blackBrush As Brush = New SolidBrush(Color.Black)<o:p>

        Dim axesFont As Font = New Font("verdana", 8, FontStyle.Bold)<o:p>

        p.Response.ContentType = "image/jpeg"<o:p>

        'g.FillRectangle(New SolidBrush(Color.LightYellow), 0, 0, Width, Height)<o:p>

        g.FillRectangle(New SolidBrush(Color.FromArgb(255, 216, 189)), 0, 0, Width, Height)<o:p>

        'Dim ChartInset As Integer = 50   old given for test change<o:p>

        Dim ChartInset As Integer = 50<o:p>

        Dim ChartWidth As Integer = Width - (2 * ChartInset)<o:p>

        Dim ChartHeight As Integer = Height - (2 * ChartInset)<o:p>

        g.DrawRectangle(New Pen(Color.Blue, 1), ChartInset, ChartInset, ChartWidth, ChartHeight)<o:p>

        btn = New Button<o:p>

        btn.ID = "btnBack"<o:p>

        btn.Text = "deepchand"<o:p>

g.DrawString(Title, New Font("verdana", 8, FontStyle.Bold), blueBrush, Width / 6, 5)<o:p>

        i = 0<o:p>

        i = 0<o:p>

        While i <= Xdivs<o:p>

            x = ChartInset + (i * ChartWidth) / Xdivs<o:p>

            y = ChartHeight + ChartInset<o:p>

            'myLabel = (Xorigin + (ScaleX * i / Xdivs)).ToString<o:p>

            ' This code Add for Print The X-axis values in Formate<o:p>

            If i >= chartValues.Count Then<o:p>

                'If i > chartValues.Count Then<o:p>

                myLabel = "Days->" & vbCrLf & " "<o:p>

                g.DrawString(myLabel, axesFont, blueBrush, x - 15, y + 10)<o:p>

            Else<o:p>

                'myLabel = CType(chartValues(i - 1), datapoint).xCaption  change<o:p>

                myLabel = CType(chartValues(i), datapoint).xCaption<o:p>

                d1 = myLabel.Substring(0, 2).ToString<o:p>

                m1 = myLabel.Substring(3, 3).ToString<o:p>

                myLabel = d1 & vbCrLf & m1<o:p>

                g.DrawString(myLabel, axesFont, blackBrush, x - 10, y + 10)<o:p>

<o:p> 

            End If<o:p>

            'g.DrawString(myLabel, axesFont, blackBrush, x - 8, y + 10)<o:p>

            g.DrawLine(blackPen, x, y + 2, x, y - 2)<o:p>

<o:p> 

            If i < chartValues.Count Then<o:p>

                '' To Print the Attrition% on the dots<o:p>

                myLabel = CType(chartValues(i), datapoint).yCaption<o:p>

x = (ChartWidth * (CType(chartValues(i), datapoint).x - Xorigin) / ScaleX) + ChartInset<o:p>

y = ChartHeight - (ChartHeight * (CType(chartValues(i), datapoint).y - Yorigin) / ScaleY) + ChartInset - 15<o:p>

       g.DrawString(myLabel, axesFont, blackBrush, x - 10, y - 5)<o:p>

                ''End To Print the Attrition% on the dots<o:p>

            End If<o:p>

<o:p> 

            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)<o:p>

        End While<o:p>

<o:p> 

        i = 0<o:p>

        i = 0 <o:p>

<o:p> 

        While i <= Ydivs<o:p>

            x = ChartInset<o:p>

            y = ChartHeight + ChartInset - (i * ChartHeight / Ydivs)<o:p>

            myLabel = (Yorigin + (ScaleY * i / Ydivs)).ToString & "%"<o:p>

<o:p> 

            If i = Ydivs Then<o:p>

                Dim s1 As StringFormat = New StringFormat(StringFormatFlags.DirectionVertical)<o:p>

                myLabel = "<-Absenteeism"<o:p>

                g.DrawString(myLabel, axesFont, blueBrush, 15, y - 45, s1)<o:p>

<o:p> 

            ElseIf (i Mod 5) = 0 Then<o:p>

<o:p> 

               g.DrawString(myLabel, axesFont, blackBrush, 20, y - 6)<o:p>

            Else<o:p>

                myLabel = ""<o:p>

                g.DrawString(myLabel, axesFont, blackBrush, 20, y - 6)<o:p>

<o:p> 

            End If<o:p>

<o:p> 

            g.DrawLine(blackPen, x + 2, y, x - 2, y)<o:p>

            <o:p>

            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)<o:p>

        End While<o:p>

<o:p> 

        g.RotateTransform(180)<o:p>

        g.TranslateTransform(0, -Height)<o:p>

        g.TranslateTransform(-ChartInset, ChartInset)<o:p>

        g.ScaleTransform(-1, 1)<o:p>

        Dim prevPoint As datapoint = New datapoint<o:p>

        prevPoint.valid = False<o:p>

<o:p> 

        i = 0<o:p>

        For Each myPoint As datapoint In chartValues<o:p>

            If prevPoint.valid = True Then<o:p>

                x0 = ChartWidth * (prevPoint.x - Xorigin) / ScaleX<o:p>

                y0 = ChartHeight * (prevPoint.y - Yorigin) / ScaleY<o:p>

                'y1 = ChartHeight * (prevPoint.ATTRAVERAGE - Yorigin) / ScaleY<o:p>

                x = ChartWidth * (myPoint.x - Xorigin) / ScaleX<o:p>

                y = ChartHeight * (myPoint.y - Yorigin) / ScaleY<o:p>

                'y2 = ChartHeight * (myPoint.ATTRAVERAGE - Yorigin) / ScaleY<o:p>

                g.DrawLine(bluePen, x0, y0, x, y)<o:p>

                g.FillEllipse(blueBrush, x0 - 2, y0 - 2, 6, 6)<o:p>

                g.FillEllipse(blueBrush, x - 2, y - 2, 6, 6)<o:p>

<o:p> 

            End If<o:p>

            prevPoint = myPoint<o:p>

            i = i + 1<o:p>

<o:p> 

        Next<o:p>

<o:p> 

        b.Save(p.Response.OutputStream, ImageFormat.Jpeg)<o:p>

        b.Dispose()<o:p>

<o:p> 

<o:p> 

‘ you this code un commete when pass file name from aspx page 18-nov-05<o:p>

<o:p> 

        'b.Save("d:\Graph\Absent.gif", ImageFormat.Gif)         <o:p>

<o:p> 

 ''''Start This code line save output as an image in specified Location<o:p>

        'Try<o:p>

        '    b.Save(filename, ImageFormat.Gif)<o:p>

        '    b.Dispose()<o:p>

        'Catch ex As Exception<o:p>

        '    'Response.Write("error:" & ex.Message.ToString)<o:p>

        'End Try<o:p>

        ''''End This code line save output as an image in specified Location <o:p>

<o:p> 

    End Sub<o:p>

<o:p> 

    Public Sub DrawAverage()<o:p>

        Dim i As Integer<o:p>

        Dim x As Single<o:p>

        Dim y As Single<o:p>

        Dim x0 As Single<o:p>

        Dim y0 As Single<o:p>

        Dim PrintAonDot As Integer<o:p>

        Dim myLabel As String<o:p>

<o:p> 

        Dim d1 As String<o:p>

        Dim m1, m2, m3 As String<o:p>

<o:p> 

        Dim blackPen As Pen = New Pen(Color.Black, 1)<o:p>

        Dim bluePen As Pen = New Pen(Color.Blue, 1)<o:p>

        Dim blueBrush As Brush = New SolidBrush(Color.Blue)<o:p>

        Dim blackBrush As Brush = New SolidBrush(Color.Black)<o:p>

        Dim axesFont As Font = New Font("verdana", 8, FontStyle.Bold)<o:p>

        p.Response.ContentType = "image/jpeg"<o:p>

        'g.FillRectangle(New SolidBrush(Color.LightYellow), 0, 0, Width, Height)<o:p>

g.FillRectangle(New SolidBrush(Color.FromArgb(255, 216, 189)), 0, 0, Width, Height)<o:p>

        'Dim ChartInset As Integer = 50   old given for test change<o:p>

        Dim ChartInset As Integer = 50<o:p>

        Dim ChartWidth As Integer = Width - (2 * ChartInset)<o:p>

        Dim ChartHeight As Integer = Height - (2 * ChartInset)<o:p>

        g.DrawRectangle(New Pen(Color.Blue, 1), ChartInset, ChartInset, ChartWidth, ChartHeight)<o:p>

        'g.DrawString(Title, New Font("arial", 14), blackBrush, Width / 3, 10)<o:p>

        g.DrawString(Title, New Font("verdana", 8, FontStyle.Bold), blueBrush, Width / 6, 5)<o:p>

<o:p> 

        i = 0<o:p>

        i = 0<o:p>

        While i <= Xdivs<o:p>

            x = ChartInset + (i * ChartWidth) / Xdivs<o:p>

            y = ChartHeight + ChartInset<o:p>

            ' This code Add for Print The X-axis values in Formate<o:p>

<o:p> 

            If i >= chartValues.Count Then<o:p>

                myLabel = "Weeks->" & vbCrLf & " "<o:p>

                'g.DrawString(myLabel, axesFont, blueBrush, x - 17, y + 10)<o:p>

            Else<o:p>

                'myLabel = CType(chartValues(i - 1), datapoint).xCaption  change<o:p>

                myLabel = CType(chartValues(i), datapoint).xCaption<o:p>

                d1 = myLabel.Substring(0, 2).ToString<o:p>

                m1 = myLabel.Substring(3, 3).ToString<o:p>

                myLabel = d1 & vbCrLf & m1<o:p>

<o:p> 

            End If<o:p>

            g.DrawString(myLabel, axesFont, blackBrush, x - 8, y + 10)<o:p>

            g.DrawLine(blackPen, x, y + 2, x, y - 2)<o:p>

<o:p> 

<o:p> 

            If i < chartValues.Count And i >= 3 Then<o:p>

                '' To Print the Average Attrition% on the dots<o:p>

                myLabel = CType(chartValues(i), datapoint).ATTRAVERAGE<o:p>

                x = (ChartWidth * (CType(chartValues(i), datapoint).x - Xorigin) / ScaleX) + ChartInset<o:p>

                y = ChartHeight - (ChartHeight * (CType(chartValues(i), datapoint).y - Yorigin) / ScaleY) + ChartInset - 15<o:p>

                g.DrawString(myLabel, axesFont, blackBrush, x, y)<o:p>

                ''End To Print the Average Attrition% on the dots<o:p>

            End If<o:p>

<o:p> 

            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)<o:p>

        End While<o:p>

<o:p> 

        i = 0<o:p>

        i = 0<o:p>

        While i <= Ydivs<o:p>

            x = ChartInset<o:p>

            y = ChartHeight + ChartInset - (i * ChartHeight / Ydivs)<o:p>

            myLabel = (Yorigin + (ScaleY * i / Ydivs)).ToString & "%"<o:p>

            'g.DrawString(myLabel, axesFont, blackBrush, 5, y - 6) old given for test change<o:p>

            g.DrawString(myLabel, axesFont, blackBrush, 25, y - 6)<o:p>

            g.DrawLine(blackPen, x + 2, y, x - 2, y)<o:p>

            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)<o:p>

        End While<o:p>

<o:p> 

        g.RotateTransform(180)<o:p>

        g.TranslateTransform(0, -Height)<o:p>

        g.TranslateTransform(-ChartInset, ChartInset)<o:p>

        g.ScaleTransform(-1, 1)<o:p>

        Dim prevPoint As datapoint = New datapoint<o:p>

        prevPoint.valid = False<o:p>

<o:p> 

        i = 0<o:p>

        For Each myPoint As datapoint In chartValues<o:p>

            If prevPoint.valid = True Then<o:p>

                x0 = ChartWidth * (prevPoint.x - Xorigin) / ScaleX<o:p>

                y0 = ChartHeight * (prevPoint.y - Yorigin) / ScaleY<o:p>

                x = ChartWidth * (myPoint.x - Xorigin) / ScaleX<o:p>

                y = ChartHeight * (myPoint.y - Yorigin) / ScaleY<o:p>

                'If CType(chartValues(i), datapoint).ATTRAVERAGE.Trim > 0 Then<o:p>

                'g.DrawLine(bluePen, x0, y0, x, y)<o:p>

                'End If<o:p>

                If i >= 3 Then<o:p>

                    g.DrawLine(bluePen, x0, y0, x, y)<o:p>

                    g.FillEllipse(blueBrush, x0 - 2, y0 - 2, 6, 6)<o:p>

                    g.FillEllipse(blueBrush, x - 2, y - 2, 6, 6)<o:p>

                End If<o:p>

                i = i + 1<o:p>

            End If<o:p>

<o:p> 

            'myLabel = myPoint.yCaption<o:p>

            'g.DrawString(myLabel, axesFont, blackBrush, x0 + 1, y0 + 1)<o:p>

            prevPoint = myPoint<o:p>

<o:p> 

        Next<o:p>

<o:p> 

        b.Save(p.Response.OutputStream, ImageFormat.Jpeg)<o:p>

<o:p> 

    End Sub<o:p>

    Protected Overrides Sub Finalize()<o:p>

        g.Dispose()<o:p>

        b.Dispose()<o:p>

    End Sub<o:p>

<o:p> 

End Class<o:p>

<o:p> 

*************************************************************************************************<o:p>

2) The CreateLineChart Function for That create a one ASPX Page. Name is Display.aspx<o:p>

*********************************************************************<o:p>

‘ Imports These Namespaces <o:p>

Imports System.Data<o:p>

Imports System.Data.OracleClient<o:p>

<o:p> 

<o:p> 

<o:p> 

   ‘ Variable Declarations<o:p>

    Dim orclCommand As OracleCommand<o:p>

    Dim rdr As OracleDataReader<o:p>

    Private OraCn As New OracleConnection(ConfigurationSettings.AppSettings("connString"))  'OracleConnection()<o:p>

<o:p> 

    'NOTE: The following placeholder declaration is required by the Web Form Designer.<o:p>

    'Do not delete or move it.<o:p>

    Private designerPlaceholderDeclaration As System.Object<o:p>

<o:p> 

    Dim Itm As ListItem<o:p>

    Dim Sqlstr As String<o:p>

<o:p> 

‘ On page_load Event of that Page<o:p>

<o:p> 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<o:p>

<o:p> 

        If Not Page.IsPostBack Then<o:p>

            Session("numSelIndex") = 0<o:p>

<o:p> 

            Sqlstr = " SQL-Query For Get the dates from and to”<o:p>

            OraCn.Open()<o:p>

            orclCommand = New OracleCommand(Sqlstr, OraCn)<o:p>

            rdr = orclCommand.ExecuteReader<o:p>

<o:p> 

            Itm = New ListItem<o:p>

            Itm.Text = "---Select---"<o:p>

            Itm.Value = ""<o:p>

            Itm.Selected = True<o:p>

            CmbFrWeek.Items.Add(Itm)<o:p>

            CmbToWeek.Items.Add(Itm)<o:p>

<o:p> 

            While rdr.Read<o:p>

                Itm = New ListItem<o:p>

                Itm.Text = rdr.GetValue(0)<o:p>

                Itm.Value = rdr.GetValue(0)<o:p>

                CmbFrWeek.Items.Add(Itm)<o:p>

                CmbToWeek.Items.Add(Itm)<o:p>

            End While<o:p>

            rdr.Close()<o:p>

            OraCn.Close()<o:p>

           <o:p>

        End If<o:p>

    End Sub<o:p>

<o:p> 

    Private Sub BtnReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReport.Click<o:p>

        Dim NoOfWeek As Integer<o:p>

        Dim Enddate, M As String<o:p>

        Dim StartDate, Enddate1 As String<o:p>

        Dim TotalPer, RowNo As Integer<o:p>

        Dim DynWdith As Integer<o:p>

        Dim XFromDate As String<o:p>

        Dim XFromDate1 As Integer<o:p>

        Dim YattritionPer, YattritionPerAvg As Double<o:p>

        Dim Grdflag As Boolean = True<o:p>

<o:p> 

<o:p> 

        Try<o:p>

            If CmbToWeek.SelectedIndex < 1 And CmbFrWeek.SelectedIndex < 1 Then<o:p>

                Label9.Text = "Select a Proper Dates"<o:p>

                Label9.Visible = True<o:p>

                flgFormSubmit.Value = 0<o:p>

                Exit Sub<o:p>

            ElseIf (CmbToWeek.SelectedIndex - CmbFrWeek.SelectedIndex) < 4 Then  <o:p>

                Label9.Text = "Date's are selected should be more than 5"<o:p>

                flgFormSubmit.Value = 0<o:p>

                Label9.Visible = True<o:p>

                Exit Sub<o:p>

<o:p> 

            ElseIf CmbToWeek.SelectedIndex <= CmbFrWeek.SelectedIndex Then<o:p>

                Label9.Text = "From Date must be less than To Date"<o:p>

                flgFormSubmit.Value = 0<o:p>

                Label9.Visible = True<o:p>

                Exit Sub<o:p>

            ElseIf (CmbToWeek.SelectedIndex - CmbFrWeek.SelectedIndex) > 31 Then<o:p>

                Label9.Text = "Date's can not select more than one months"<o:p>

                Label9.Visible = True<o:p>

                flgFormSubmit.Value = 0<o:p>

                Exit Sub<o:p>

            Else<o:p>

<o:p> 

                Label9.Text = ""<o:p>

            End If<o:p>

<o:p> 

          Label9.Text = ""<o:p>

            If CmbFrWeek.SelectedIndex = 1 Then<o:p>

                Enddate = "24-May-2003"<o:p>

            Else<o:p>

<o:p> 

                Enddate = CmbFrWeek.Items(CmbFrWeek.SelectedIndex - 1).Value<o:p>

            End If<o:p>

<o:p> 

<o:p> 

            NoOfWeek = (CmbToWeek.SelectedIndex - CmbFrWeek.SelectedIndex) + 2<o:p>

<o:p> 

            ' This code for <st1:place w:st="on"><st1:placename w:st="on">Excute <st1:placename w:st="on">Proc <st1:placetype w:st="on">Pass the parameter<o:p>

            OraCn.Open()<o:p>

‘ Create a one Glogal Temprary Table That will be Store the Main Graphical data<o:p>

 ‘ That will display in the Line Graph From This Table<o:p>

<o:p> 

            Dim cmd As New OracleCommand("Proc_Absent_New", OraCn)<o:p>

            cmd.CommandType = CommandType.StoredProcedure<o:p>

<o:p> 

            Dim FromWeek As OracleParameter = cmd.Parameters.Add("frmDay", OracleType.DateTime)<o:p>

            FromWeek.Direction = ParameterDirection.Input<o:p>

            FromWeek.Value = UCase(Trim(Enddate.ToString()))<o:p>

<o:p> 

            Dim NoOfWeeks As OracleParameter = cmd.Parameters.Add("NoOfDays", OracleType.Int32, 20)<o:p>

            NoOfWeeks.Direction = ParameterDirection.Input<o:p>

            NoOfWeeks.Value = UCase(Trim(NoOfWeek.ToString()))<o:p>

<o:p> 

            cmd.ExecuteNonQuery()<o:p>

            cmd.Dispose()<o:p>

<o:p> 

<o:p> 

<o:p> 

‘ Create a one Glogal Temprary Table That will be Store the Main Graphical data<o:p>

 ‘ That will display in the Line Graph From This Table<o:p>

<o:p> 

            Sqlstr = "SELECT COUNT(*)RowNo,ROUND(MAX(absentper)+1) FROM ABSENT_TEMP "<o:p>

            'OraCn.Open()<o:p>

            orclCommand = New OracleCommand(Sqlstr, OraCn)<o:p>

            rdr = orclCommand.ExecuteReader<o:p>

            While rdr.Read()<o:p>

                TotalPer = rdr.GetValue(1)<o:p>

                DynWdith = rdr.GetValue(1)<o:p>

                RowNo = rdr.GetValue(0)<o:p>

            End While<o:p>

            rdr.Close()<o:p>

            orclCommand.Dispose()<o:p>

            <o:p>

'****This code for Draw graph call class declare Object<o:p>

'****Select Values from the Tempprary Table<o:p>

<o:p> 

            Sqlstr = "select Fromday,ABSENTPER, rownum,to_char(Fromday, 'dd-Mon') fdate from ABSENT_TEMP "<o:p>

            orclCommand = New OracleCommand(Sqlstr, OraCn)<o:p>

            rdr = orclCommand.ExecuteReader<o:p>

<o:p> 

            StartDate = CmbToWeek.Items(CmbToWeek.SelectedIndex).Value<o:p>

            Enddate1 = CmbFrWeek.Items(CmbFrWeek.SelectedIndex).Value<o:p>

<o:p> 

‘ Create the Object of Line chart class that we have created before <o:p>

<o:p> 

            Dim c1 As LineChartAbs = New LineChartAbs(RowNo * 50, 550, Page)<o:p>

<o:p> 

            c1.Title = "  Absenteeism Graph  " & vbCrLf & "  " & Convert.ToDateTime(Enddate1).ToString("dd/MM/yyyy") & "-" & Convert.ToDateTime(StartDate).ToString("dd/MM/yyyy")<o:p>

           <o:p>

            c1.Xorigin = 0<o:p>

            c1.ScaleX = NoOfWeek - 0<o:p>

            c1.Xdivs = NoOfWeek - 0<o:p>

            c1.Yorigin = 0<o:p>

            If TotalPer > 12 Then<o:p>

                c1.ScaleY = Math.Ceiling(TotalPer / 10) * 10<o:p>

                c1.Ydivs = CInt((Math.Ceiling(TotalPer / 10) * 10))<o:p>

<o:p> 

            Else<o:p>

                c1.ScaleY = TotalPer    'if Absent% is Less then 10% then implement this scale<o:p>

                c1.Ydivs = CInt(TotalPer) * 5<o:p>

                <o:p>

            End If<o:p>

<o:p> 

            'c1.ScaleY = 100<o:p>

            'c1.Ydivs = 50<o:p>

<o:p> 

            While rdr.Read<o:p>

                <o:p>

    XFromDate = rdr.GetValue(2) - 1<o:p>

                XFromDate1 = CType(XFromDate, Integer) * 1<o:p>

                YattritionPer = rdr.GetValue(1)<o:p>

<o:p> 

                c1.AddValue(XFromDate1, YattritionPer, rdr.GetValue(3), rdr.GetValue(1))<o:p>

<o:p> 

            End While<o:p>

<o:p> 

            rdr.Close()<o:p>

            c1.Draw()<o:p>

<o:p> 

''''Start This code line save output as an image in specified Location <o:p>

<o:p> 

            Dim strFilePath, strUser As String<o:p>

            Dim strPath As String<o:p>

            Session("login") = "Absenteesimg"<o:p>

            strUser = Session("login")<o:p>

            Try<o:p>

                strFilePath = Server.MapPath(".") & "/Graphs/" & strUser & ".gif"<o:p>

<o:p> 

            Catch ex As Exception<o:p>

                Response.Write("Error:" & ex.Message.ToString)<o:p>

            End Try<o:p>

            c1.Draw(strFilePath)<o:p>

            Response.Redirect("Graph.aspx")<o:p>

<o:p> 

            'End here that dispaly proper way<o:p>

<o:p> 

        Catch ex As Exception<o:p>

                Response.Write("Error Occured" & ex.StackTrace)<o:p>

            Finally<o:p>

                OraCn.Close()<o:p>

                'Label9.Text = ""<o:p>

<o:p> 

                'Session("numSelIndex") = CmbToWeek.SelectedIndex<o:p>

                'Session("formsubmit") = 0<o:p>

            End Try<o:p>

<o:p> 

    End Sub<o:p>

End Class<o:p>

<o:p> 

<o:p> 

******************************************************************************<o:p>

3) Create the One frm Graph.aspx file Write The Below line of code<o:p>

In this page Call a Generate Graph that created by previous Display ASPX PAGE<o:p>

'**********************************************************************<o:p>

Take The one Images control in that Call the Saved Graph.<o:p>

IMG TAGE set the  path of that Image.<o:p>

src="Graphs\Absenteesimg.gif"<o:p>

 Advantages :=

         1)    Multiple user can Access at same time.<o:p>

2)      Generate the Images(Graph) according to user session.<o:p>

3)      Export the Gif images with data in the Excel file as report point of view. <o:p>

4)      Generate the Dynamic Graph for each user request.<o:p>

Graphical Represention of the data so that user easier way understand the output of the system data.  <o:p>

*****************************************************************************<o:p>

<o:p> 

Possible Enhancements<o:p>

<o:p> 


The CreatePieChart function lacks the bang and pizzazz that a third-party graphing component may provide, but this function was created in less than 15 minutes, costs nothing (except my time), and, best of all, has the source code readily available for any future changes or enhancements you may be interested in making.
One possible enhancement for the CreatePieChart function would be to add the ability to pass in a SQL string as opposed to a database table name. In its current state, the CreatePieChart graph can only create pie charts for databases that have very simple data models. Being able to specify a SQL string means you could create graphs where the data came from multiple tables, or graph only certain rows from a database table by specifying a WHERE clause.


<o:p>

Conclusion<o:p>


Because ASP.NET allows you to use the classes from the .NET Framework, with a little bit of code you can create your own dynamic images from a Web page. These images can either be saved to the Web server's file system or streamed directly to the browser. All of the image-generation routines you will ever need are included in the .NET Framework. The charts and graphs you can create are limited only by your imagination.<o:p>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Patni Computer Systems Ltd. Mumbai
India India
Deepchand kosta
Sr. Software Engineer(MCA)
Working on Microsoft Technologies since 5 years , India. Technical experience most specifically on Biztalk Server 2004/2006, Webservices, C#.Net and .NET framework,VB.NET, XML, XSLT, Flat file.
I have done MCP,MCAD,MCDBA,MCTS(BizTalk server-2006)

Comments and Discussions

 
GeneralLine Chart Pin
hu20058-Aug-07 6:40
hu20058-Aug-07 6:40 
GeneralLine Chart Pin
wijayakoon23-Nov-06 18:47
wijayakoon23-Nov-06 18:47 
GeneralRe: Line Chart Pin
BraveShogun15-Apr-07 4:57
BraveShogun15-Apr-07 4:57 
GeneralRe: Line Chart Pin
Junior Boy11-Jun-07 19:43
Junior Boy11-Jun-07 19:43 
I had edied this code from this article and it's worked.
Now I have my own version line chart, it's multiple line chart,say that you can create lines on one chart.

If anybody intesest to get this code just reply this back to me.
big_kh@hotmail.com

Be gladly to help all guys!

junior boy

GeneralRe: Line Chart Pin
sergreb10-Oct-07 7:33
sergreb10-Oct-07 7:33 
GeneralRe: Line Chart Pin
illegolas3116-Jan-08 3:54
illegolas3116-Jan-08 3:54 
GeneralRe: Line Chart Pin
Member 49115484-Mar-08 19:53
Member 49115484-Mar-08 19:53 
GeneralRe: Line Chart Pin
ahyo19-Mar-08 15:28
ahyo19-Mar-08 15:28 
GeneralSimply Great! Pin
Nil_Gup25-May-06 5:26
Nil_Gup25-May-06 5:26 
GeneralRe: Simply Great! Pin
Marc Clifton25-May-06 15:17
mvaMarc Clifton25-May-06 15:17 
GeneralRe: Simply Great! Pin
DEEPCHAND KOSTA25-May-06 21:34
DEEPCHAND KOSTA25-May-06 21:34 
GeneralRe: Simply Great! Pin
Paul Lyons26-May-06 4:29
Paul Lyons26-May-06 4:29 
GeneralRe: Simply Great! Pin
nishant barsainyan20-Apr-07 18:23
nishant barsainyan20-Apr-07 18:23 
GeneralRe: Simply Great! Pin
Nil_Gup20-Apr-07 20:55
Nil_Gup20-Apr-07 20:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.