ChartDirector 6.0 (ASP/COM/VB Edition)

Overlapping Bar Chart




This example demonstrates a multi-bar chart in which the bars within a cluster overlaps.

The overlapping effect is configured using BarLayer.setOverlapRatio. This method allows you to specify overlapping ratio and order. In this example, the overlapping ratio is 0.5, while the default overlapping order is used. The default order is that the first data set will stay on top of the second data set, and so on.

Source Code Listing

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

' The data for the bar chart
data0 = Array(100, 125, 156, 147, 87, 124, 178, 109, 140, 106, 192, 122)
data1 = Array(122, 156, 179, 211, 198, 177, 160, 220, 190, 188, 220, 270)
data2 = Array(167, 190, 213, 267, 250, 320, 212, 199, 245, 267, 240, 310)
labels = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec")

' Create a XYChart object of size 580 x 280 pixels
Set c = cd.XYChart(580, 280)

' Add a title to the chart using 14pt Arial Bold Italic font
Call c.addTitle("Product Revenue For Last 3 Years", "arialbi.ttf", 14)

' Set the plot area at (50, 50) and of size 500 x 200. Use two alternative background colors (f8f8f8
' and ffffff)
Call c.setPlotArea(50, 50, 500, 200, &Hf8f8f8, &Hffffff)

' Add a legend box at (50, 25) using horizontal layout. Use 8pt Arial as font, with transparent
' background.
Call c.addLegend(50, 25, False, "arial.ttf", 8).setBackground(cd.Transparent)

' Set the x axis labels
Call c.xAxis().setLabels(labels)

' Draw the ticks between label positions (instead of at label positions)
Call c.xAxis().setTickOffset(0.5)

' Add a multi-bar layer with 3 data sets
Set layer = c.addBarLayer2(cd.Side)
Call layer.addDataSet(data0, &Hff8080, "Year 2003")
Call layer.addDataSet(data1, &H80ff80, "Year 2004")
Call layer.addDataSet(data2, &H8080ff, "Year 2005")

' Set 50% overlap between bars
Call layer.setOverlapRatio(0.5)

' Add a title to the y-axis
Call c.yAxis().setTitle("Revenue (USD in millions)")

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

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

    Dim cd As New ChartDirector.API

    ' The data for the bar chart
    Dim data0()
    data0 = Array(100, 125, 156, 147, 87, 124, 178, 109, 140, 106, 192, 122)
    Dim data1()
    data1 = Array(122, 156, 179, 211, 198, 177, 160, 220, 190, 188, 220, 270)
    Dim data2()
    data2 = Array(167, 190, 213, 267, 250, 320, 212, 199, 245, 267, 240, 310)
    Dim labels()
    labels = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", _
        "Dec")

    ' Create a XYChart object of size 580 x 280 pixels
    Dim c As XYChart
    Set c = cd.XYChart(580, 280)

    ' Add a title to the chart using 14pt Arial Bold Italic font
    Call c.addTitle("Product Revenue For Last 3 Years", "arialbi.ttf", 14)

    ' Set the plot area at (50, 50) and of size 500 x 200. Use two alternative background colors
    ' (f8f8f8 and ffffff)
    Call c.setPlotArea(50, 50, 500, 200, &Hf8f8f8, &Hffffff)

    ' Add a legend box at (50, 25) using horizontal layout. Use 8pt Arial as font, with transparent
    ' background.
    Call c.addLegend(50, 25, False, "arial.ttf", 8).setBackground(cd.Transparent)

    ' Set the x axis labels
    Call c.xAxis().setLabels(labels)

    ' Draw the ticks between label positions (instead of at label positions)
    Call c.xAxis().setTickOffset(0.5)

    ' Add a multi-bar layer with 3 data sets
    Dim layer As BarLayer
    Set layer = c.addBarLayer2(cd.Side)
    Call layer.addDataSet(data0, &Hff8080, "Year 2003")
    Call layer.addDataSet(data1, &H80ff80, "Year 2004")
    Call layer.addDataSet(data2, &H8080ff, "Year 2005")

    ' Set 50% overlap between bars
    Call layer.setOverlapRatio(0.5)

    ' Add a title to the y-axis
    Call c.yAxis().setTitle("Revenue (USD in millions)")

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

    'include tool tip for the chart
    viewer.ImageMap = c.getHTMLImageMap("clickable", "", _
        "title='{xLabel} Revenue on {dataSetName}: {value} millions'")

End Sub