jChart - Line Chart Example

About jChart

The line chart uses the LineOverlay overlay type and can display multiple series on a single chart.

Series One value=52.11751 Series One value=43.13611 Series One value=29.70728 Series One value=42.81749 Series One value=51.36805 Series One value=29.87071 Series One value=45.4397

ASP.NET 2.0 example


This is the HTML that displays the chart on the page. The image map is optional and is used to implement drill-down.

    <img src="<%=me.ImageURL %>" alt="" usemap="#MyMap" />
    <map id="Map1" name="MyMap">


The following code uses two environment variables.

Application("TempPath") is an application variable that contains the path to the directory in which temporary images should be stored in the format "c:\mywebsite\temp\"

Session("TempURL") is a session variable that contains the base URL of the directory in which temporary images are be stored in the format "http://myhost/temp/"

The chart object should be global as it is initialized in the Page_Load function and then referenced by the ImageURL and ImageMap functions later on.

    Dim moChart As jChart.GraphChart

The Page_Load event is where the heavy lifting gets done. It is here that the appearance of the chart (background color, size, chart type, etc.) is set, the X and Y axis values are set, and series are created and added to the chart.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '---Let's set up the chart.
        '   We'll make it 600 pixels wide by 400 pixels high.        
        moChart = New jChart.GraphChart(600, 400)

        '---We'll specify a 10 pixel border all the way around the inside
        '   edge of the chart.
        moChart.ChartDimensions.FrameSize = 10

        '---The Y Axis column on the left will be 20 pixels wide.
        moChart.ChartDimensions.YAxis1LabelColumnWidth = 20

        '---The X Axis row on the bottom will be 20 pixels high.
        moChart.ChartDimensions.XAxisLabelRowHeight = 20

        '---Let's add the X Axis values
        '   We'll have 7 values and in place of the numeric values we'll
        '   display friendly labels.
        Dim oXAxisValues() As Single = {0, 1, 2, 3, 4, 5, 6}
        Dim oXAxisLabels() As String = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}

        '---We'll create an XAxis object using the ChartDimensions object
        '   from the chart we just created.
        Dim oXAxis As New jChart.XAxis(moChart.ChartDimensions, oXAxisValues)

        '---Let's tell the X Axis about the friendly labels to display in place
        '   of numbers.
        oXAxis.XAxisLabels = oXAxisLabels

        '---OK, finally let's tell the chart about its X Axis.
        moChart.XAxis = oXAxis

        '---Now, let's set up the Y Axis.
        '   We'll define 8 values. We could specify labels for our Y Axis values just as
        '   we did for our X Axis values but we won't.
        Dim oYAxis1Values() As Single = {20, 25, 30, 35, 40, 45, 50, 55}
        'Dim oYAxis1Labels() As String = {"20", "25", "30", "35", "40", "45", "50", "55"}

        '---We'll tell our new Y Axis object that it will be on the left side of the chart
        '   and we'll pass in a reference to the chart's ChartDimension object so the Y Axis
        '   will know how big it needs to be.
        Dim oYAxis1 As New jChart.YAxis(jChart.YAxis.Alignment.Left, moChart.ChartDimensions, oYAxis1Values)
        'oYAxis1.YAxisLabels = oYAxis1Labels

        '---Now let's tell the chart about our Y Axis 
        moChart.YAxis1 = oYAxis1

        '---Finally, let's show some sample values.
        '   We'll make the values random, for more realism.
        '   There will be 7 of them, just as there are 7 points on the X Axis.
        '   The series object expects an array of single, so that's what we'll make.
        Dim oValues(8) As Single
        For iX As Short = 0 To 7
            oValues(iX) = Rnd() * 35 + 20

        '---Now that we have values, we'll need a series
        '   Our series will be a line series.
        Dim oIndicator As New jChart.Indicator( _
                    jChart.Indicator.Type.Cross, _
                    Color.Green, _

        Dim oLineSeries As New jChart.Overlay.Series.LineSeries(oIndicator)
        With oLineSeries
            '---We will call our series "series one". 
            '   This value is accessible on drill-down from the image map.
            .Name = "Series One"

            '---Now we'll give the series the values it should use.
            .Values = oValues

            '---The series will be green in color
            .Color = Color.Green

            '---The series will be slightly transparent (75% opaque)
            .Opacity = 0.75

            .LineType = Drawing2D.DashStyle.Dash
        End With

        '---Since we are using a line chart, which belongs to the 
        '   LineOverlay overlay type, we'll need a LineOverlay object.
        Dim oLineOverlay As jChart.Overlay.LineOverlay

        '---We need to tell the overlay which dimensions and axes to use when
        '   we create it.
        oLineOverlay = New jChart.Overlay.LineOverlay(moChart.ChartDimensions, oXAxis, oYAxis1)

        '---Let's add the series to the overlay

        '---Let's add the overlay to the chart.

        '---OK. All done. Now let's draw the chart.
        '   We'll start by creating a bitmap onto which the chart will be drawn.
        '   We do it this way so that the bitmap can be shared (a background image
        '   perhaps?)
        Dim oPicture As New Bitmap( _
                moChart.ChartDimensions.OuterWidth, _
                moChart.ChartDimensions.OuterHeight, _

        '---Now that we've created the bitmap, we'll draw the chart on it and save it
        '   to a temporary location on the server in JPEG format. The charting object
        '   creates a unique filename for our new chart. This unique filename is
        '   accessible via the chart's FileName property (see ImageURL)

        '---The path where the image will be saved must have write access for the ASPNET
        '   process enabled.
        moChart.SaveChartToFile( _
                    Application("TempPath"), _
    End Sub

The ImageURL property provides the name of the chart image that was created during Page_Load to the IMG tag on the page.

        Protected ReadOnly Property ImageURL() As String
            If moChart Is Nothing Then
                Return ""
                Return Session("TempURL") & moChart.FileName
            End If
        End Get
    End Property

The ImageMap property constructs and provides the image map that allows for drill-down capability.

        Protected ReadOnly Property ImageMap() As String
            If moChart Is Nothing Then
                Return ""
                Return MakeImageMapString()
            End If
        End Get
    End Property

Here we build the image map from the collection of chart regions. This is done in the code behind block so that it can be customized--perhaps only certain series should have drill-down enabled, and of course you will want to customize the action that occurs when a series value is clicked on.

   Private Function MakeImageMapString() As String
        Dim sImageMap As String = ""

        For Each oRegion As jChart.RegionArea In moChart.RegionList
            Dim sCoords As String = ""

            Dim oPoints As Point() = oRegion.SeriesPoints
            sCoords = oPoints(0).X & "," & oPoints(0).Y

            For iPoint As Integer = 1 To oPoints.GetUpperBound(0)
                sCoords = sCoords & "," & oPoints(iPoint).X & "," & oPoints(iPoint).Y

            Dim sArea As String = "<area " & _
                                        " shape=" & Chr(34) & "poly" & Chr(34) & _
                                        " coords=" & Chr(34) & sCoords & Chr(34) & _
                                        " href=" & Chr(34) & "javascript:alert('You clicked on " & oRegion.SeriesName & " at value: " + oRegion.YValue.ToString + "');" & Chr(34) & _
                                        " alt=" & Chr(34) & oRegion.SeriesName & " value=" & oRegion.YValue & Chr(34) & ">" & _

            sImageMap = sImageMap & sArea

        Return sImageMap
    End Function


Created by Joe Lynds 2002-2008. Contact Joe