ChartDirector 6.0 (ASP/COM/VB Edition)

Icon Pie Chart (2)




This example extends the Icon Pie Chart (1) example to demonstrate using CDML to include multiple fonts and colors in chart title, sector labels and to draw logo. It also demonstrates silver background color, rounded corners, and glass shading effect for title.

Source Code Listing

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

' The data for the pie chart
data = Array(28, 45, 5, 1, 12)

' The labels for the pie chart
labels = Array("Excellent", "Good", "Bad", "Very Bad", "Neutral")

' The icons for the sectors
icons = Array("laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png")

' Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, 1 pxiel
' 3D border effect and rounded corners
Set c = cd.PieChart(560, 300, cd.silverColor(), &H000000, 1)
Call c.setRoundedFrame()

' Set default directory for loading images from current script directory
Call c.setSearchPath(Server.MapPath("."))

' Set the center of the pie at (280, 150) and the radius to 120 pixels
Call c.setPieSize(280, 150, 120)

' Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass effect
Call c.addTitle( _
    "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: " & _
    "<*font=timesi.ttf,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>TM" & _
    "<*/font*> molecules?", "timesbi.ttf", 15, &H000080).setBackground(&Ha0c8ff, &H000000, _
    cd.glassEffect())

' Add a logo to the chart written in CDML as the bottom title aligned to the bottom right
Call c.addTitle2(cd.BottomRight, _
    "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*>" & _
    "<*font=timesbi.ttf,size=12*>Molecular Engineering<*br*><*font=arial.ttf,size=10*>Creating " & _
    "better molecules")

' Set the pie data and the pie labels
Call c.setData(data, labels)

' Set 3D style
Call c.set3D()

' Use the side label layout method
Call c.setLabelLayout(cd.SideLayout)

' Set the label background color to transparent
Call c.setLabelStyle().setBackground(cd.Transparent)

' Add icons to the chart as a custom field
Call c.addExtraField(icons)

' Configure the sector labels using CDML to include the icon images
Call c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)")

' Explode the 3rd and 4th sectors as a group (index = 2 and 3)
Call c.setExplodeGroup(2, 3)

' Set the start angle to 135 degrees may improve layout when there are many small sectors at the end
' of the data array (that is, data sorted in descending order). It is because this makes the small
' sectors position near the horizontal axis, where the text label has the least tendency to overlap.
' For data sorted in ascending order, a start angle of 45 degrees can be used instead.
Call c.setStartAngle(135)

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

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

    Dim cd As New ChartDirector.API

    ' The data for the pie chart
    Dim data()
    data = Array(28, 45, 5, 1, 12)

    ' The labels for the pie chart
    Dim labels()
    labels = Array("Excellent", "Good", "Bad", "Very Bad", "Neutral")

    ' The icons for the sectors
    Dim icons()
    icons = Array("laugh.png", "smile.png", "sad.png", "angry.png", "nocomment.png")

    ' Create a PieChart object of size 560 x 300 pixels, with a silver background, black border, 1
    ' pxiel 3D border effect and rounded corners
    Dim c As PieChart
    Set c = cd.PieChart(560, 300, cd.silverColor(), &H000000, 1)
    Call c.setRoundedFrame()

    ' Set the center of the pie at (280, 150) and the radius to 120 pixels
    Call c.setPieSize(280, 150, 120)

    ' Add a title box with title written in CDML, on a sky blue (A0C8FF) background with glass
    ' effect
    Call c.addTitle( _
        "<*block,valign=absmiddle*><*img=doc.png*> Customer Survey: " & _
        "<*font=timesi.ttf,color=000000*>Do you like our <*font,color=dd0000*>Hyper<*super*>TM" & _
        "<*/font*> molecules?", "timesbi.ttf", 15, &H000080).setBackground(&Ha0c8ff, &H000000, _
        cd.glassEffect())

    ' Add a logo to the chart written in CDML as the bottom title aligned to the bottom right
    Call c.addTitle2(cd.BottomRight, _
        "<*block,valign=absmiddle*><*img=molecule.png*> <*block*><*color=FF*>" & _
        "<*font=timesbi.ttf,size=12*>Molecular Engineering<*br*><*font=arial.ttf,size=10*>" & _
        "Creating better molecules")

    ' Set the pie data and the pie labels
    Call c.setData(data, labels)

    ' Set 3D style
    Call c.set3D()

    ' Use the side label layout method
    Call c.setLabelLayout(cd.SideLayout)

    ' Set the label background color to transparent
    Call c.setLabelStyle().setBackground(cd.Transparent)

    ' Add icons to the chart as a custom field
    Call c.addExtraField(icons)

    ' Configure the sector labels using CDML to include the icon images
    Call c.setLabelFormat("<*block,valign=absmiddle*><*img={field0}*> {label} ({percent|0}%)")

    ' Explode the 3rd and 4th sectors as a group (index = 2 and 3)
    Call c.setExplodeGroup(2, 3)

    ' Set the start angle to 135 degrees may improve layout when there are many small sectors at the
    ' end of the data array (that is, data sorted in descending order). It is because this makes the
    ' small sectors position near the horizontal axis, where the text label has the least tendency
    ' to overlap. For data sorted in ascending order, a start angle of 45 degrees can be used
    ' instead.
    Call c.setStartAngle(135)

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

    'include tool tip for the chart
    viewer.ImageMap = c.getHTMLImageMap("clickable", "", _
        "title='{label}: {value} responses ({percent}%)'")

End Sub