ChartDirector 6.0 (ASP/COM/VB Edition)

H-Bar Meter Orientation


      

This example demonstrates horizontal bar meters in different orientations.

In a horizontal bar meter, the scale labels can be positioned above the meter scale or below it. This is controlled by the last argument to LinearMeter.setMeter, which can be Top or Bottom,

The title and value readout added using BaseChart.addText can be put at any position. In this example, we put them at the opposite side to the scale labels.

Source Code Listing

[Web Version (in ASP)] aspdemo\hbarmeterorientation.asp
<%@ language="vbscript" %>
<%
Set cd = CreateObject("ChartDirector.API")

' This script can draw different charts depending on the chartIndex
chartIndex = CInt(Request("img"))

' The value to display on the meter
value = 75.35

' The bar colors for the meters
barColor = Array(&H0088ff, &H00cc00, &H8833dd, &Hff8800)

' Create a LinearMeter object of size 260 x 80 pixels with very light grey (0xeeeeee) backgruond and
' a light grey (0xccccccc) 3-pixel thick rounded frame
Set m = cd.LinearMeter(260, 80, &Heeeeee, &Hcccccc)
Call m.setRoundedFrame(cd.Transparent)
Call m.setThickFrame(3)

' This example demonstrates putting the text labels at the top or bottom. This is by setting the
' label alignment, as well as the scale, color scale and label positions.
alignment = Array(cd.Top, cd.Top, cd.Bottom, cd.Bottom)
meterYPos = Array(24, 24, 36, 36)
labelYPos = Array(65, 65, 16, 16)
colorScalePos = Array(48, 48, 27, 27)

' Set the scale region
Call m.setMeter(18, meterYPos(chartIndex), 222, 20, alignment(chartIndex))

' Set meter scale from 0 - 100, with a tick every 10 units
Call m.setScale(0, 100, 10)

' Add a 5-pixel thick smooth color scale
smoothColorScale = Array(0, &H0000ff, 25, &H0088ff, 50, &H00ff00, 75, &Hdddd00, 100, &Hff0000)
Call m.addColorScale(smoothColorScale, colorScalePos(chartIndex), 5)

' Add a bar from 0 to value with glass effect and 4 pixel rounded corners
Call m.addBar(0, value, barColor(chartIndex), cd.glassEffect(cd.NormalGlare, cd.Top), 4)

'
' In this example, some charts have the "Temperauture" label on the left side and the value readout
' on the right side, and some charts have the reverse
'

If chartIndex Mod 2 = 0 Then
    ' Add a label on the left side using 8pt Arial Bold font
    Call m.addText(15, labelYPos(chartIndex), "Temperature C", "arialbd.ttf", 8, cd.TextColor, _
        cd.Left)

    ' Add a text box on the right side. Display the value using white (0xffffff) 8pt Arial Bold font
    ' on a black (0x000000) background with depressed rounded border.
    Set t = m.addText(240, labelYPos(chartIndex), m.formatValue(value, "2"), "arial.ttf", 8, _
        &Hffffff, cd.Right)
    Call t.setBackground(&H000000, &H000000, -1)
    Call t.setRoundedCorners(3)
Else
    ' Add a label on the right side using 8pt Arial Bold font
    Call m.addText(243, labelYPos(chartIndex), "Temperature C", "arialbd.ttf", 8, cd.TextColor, _
        cd.Right)

    ' Add a text box on the left side. Display the value using white (0xffffff) 8pt Arial Bold font
    ' on a black (0x000000) background with depressed rounded border.
    Set t = m.addText(18, labelYPos(chartIndex), m.formatValue(value, "2"), "arial.ttf", 8, _
        &Hffffff, cd.Left)
    Call t.setBackground(&H000000, &H000000, -1)
    Call t.setRoundedCorners(3)
End If

' Output the chart
Response.ContentType = "image/png"
Response.BinaryWrite m.makeChart2(cd.PNG)
Response.End
%>

[Windows Version (in Visual Basic)] vbdemo\hbarmeterorientation.cls
Public Sub createChart(viewer As Object, chartIndex As Integer)

    Dim cd As New ChartDirector.API

    ' The value to display on the meter
    Dim value As Double
    value = 75.35

    ' The bar colors for the meters
    Dim barColor()
    barColor = Array(&H0088ff, &H00cc00, &H8833dd, &Hff8800)

    ' Create a LinearMeter object of size 260 x 80 pixels with very light grey (0xeeeeee) backgruond
    ' and a light grey (0xccccccc) 3-pixel thick rounded frame
    Dim m As LinearMeter
    Set m = cd.LinearMeter(260, 80, &Heeeeee, &Hcccccc)
    Call m.setRoundedFrame()
    Call m.setThickFrame(3)

    ' This example demonstrates putting the text labels at the top or bottom. This is by setting the
    ' label alignment, as well as the scale, color scale and label positions.
    Dim alignment()
    alignment = Array(cd.Top, cd.Top, cd.Bottom, cd.Bottom)
    Dim meterYPos()
    meterYPos = Array(24, 24, 36, 36)
    Dim labelYPos()
    labelYPos = Array(65, 65, 16, 16)
    Dim colorScalePos()
    colorScalePos = Array(48, 48, 27, 27)

    ' Set the scale region
    Call m.setMeter(18, meterYPos(chartIndex), 222, 20, alignment(chartIndex))

    ' Set meter scale from 0 - 100, with a tick every 10 units
    Call m.setScale(0, 100, 10)

    ' Add a 5-pixel thick smooth color scale
    Dim smoothColorScale()
    smoothColorScale = Array(0, &H0000ff, 25, &H0088ff, 50, &H00ff00, 75, &Hdddd00, 100, &Hff0000)
    Call m.addColorScale(smoothColorScale, colorScalePos(chartIndex), 5)

    ' Add a bar from 0 to value with glass effect and 4 pixel rounded corners
    Call m.addBar(0, value, barColor(chartIndex), cd.glassEffect(cd.NormalGlare, cd.Top), 4)

    '
    ' In this example, some charts have the "Temperauture" label on the left side and the value
    ' readout on the right side, and some charts have the reverse
    '

    If chartIndex Mod 2 = 0 Then
        ' Add a label on the left side using 8pt Arial Bold font
        Call m.addText(15, labelYPos(chartIndex), "Temperature C", "arialbd.ttf", 8, cd.TextColor, _
            cd.Left)

        ' Add a text box on the right side. Display the value using white (0xffffff) 8pt Arial Bold
        ' font on a black (0x000000) background with depressed rounded border.
        Dim t As ChartDirector.TextBox
        Set t = m.addText(240, labelYPos(chartIndex), m.formatValue(value, "2"), "arial.ttf", 8, _
            &Hffffff, cd.Right)
        Call t.setBackground(&H000000, &H000000, -1)
        Call t.setRoundedCorners(3)
    Else
        ' Add a label on the right side using 8pt Arial Bold font
        Call m.addText(243, labelYPos(chartIndex), "Temperature C", "arialbd.ttf", 8, _
            cd.TextColor, cd.Right)

        ' Add a text box on the left side. Display the value using white (0xffffff) 8pt Arial Bold
        ' font on a black (0x000000) background with depressed rounded border.
        Set t = m.addText(18, labelYPos(chartIndex), m.formatValue(value, "2"), "arial.ttf", 8, _
            &Hffffff, cd.Left)
        Call t.setBackground(&H000000, &H000000, -1)
        Call t.setRoundedCorners(3)
    End If

    ' Output the chart
    Set viewer.Picture = m.makePicture()

End Sub