This example demonstrates adding custom labels to data points.
- The red glassy spheres in this example come from a scatter layer created using XYChart.addScatterLayer.
- The custom labels in this example is contained in an array variable, and is added as an extra field to the chart using Layer.addExtraField.
- The data label format is set using Layer.setDataLabelFormat to show the extra field.
- The font for the data labels is set to 8 points Arial Bold using Layer.setDataLabelStyle.
- Layer.setDataLabelStyle returns a TextBox object representing the prototype of the data labels. In this example, the TextBox object is used to customize the background colors, 3D borders, position offsets and alignments of the data labels.
[Web Version (in ASP)] aspdemo\scatterlabels.asp
<%@ language="vbscript" %>
<%
Set cd = CreateObject("ChartDirector.API")
' The XY points for the scatter chart
dataX = Array(150, 400, 300, 1500, 800)
dataY = Array(0.6, 8, 5.4, 2, 4)
' The labels for the points
labels = Array("Nano<*br*>100", "SpeedTron<*br*>200 Lite", "SpeedTron<*br*>200", _
"Marathon<*br*>Extra", "Marathon<*br*>2000")
' Create a XYChart object of size 450 x 400 pixels
Set c = cd.XYChart(450, 400)
' Set the plotarea at (55, 40) and of size 350 x 300 pixels, with a light grey border (0xc0c0c0).
' Turn on both horizontal and vertical grid lines with light grey color (0xc0c0c0)
Call c.setPlotArea(55, 40, 350, 300, &Hffffff, -1, &Hc0c0c0, &Hc0c0c0, -1)
' Add a title to the chart using 18pt Times Bold Itatic font.
Call c.addTitle("Product Comparison Chart", "Times New Roman Bold Italic", 18)
' Add a title to the y axis using 12pt Arial Bold Italic font
Call c.yAxis().setTitle("Capacity (tons)", "Arial Bold Italic", 12)
' Add a title to the x axis using 12pt Arial Bold Italic font
Call c.xAxis().setTitle("Range (miles)", "Arial Bold Italic", 12)
' Set the axes line width to 3 pixels
Call c.xAxis().setWidth(3)
Call c.yAxis().setWidth(3)
' Add the data as a scatter chart layer, using a 15 pixel circle as the symbol
Set layer = c.addScatterLayer(dataX, dataY, "", cd.GlassSphereShape, 15, &Hff3333, &Hff3333)
' Add labels to the chart as an extra field
Call layer.addExtraField(labels)
' Set the data label format to display the extra field
Call layer.setDataLabelFormat("{field0}")
' Use 8pt Arial Bold to display the labels
Set textbox = layer.setDataLabelStyle("Arial Bold", 8)
' Set the background to purple with a 1 pixel 3D border
Call textbox.setBackground(&Hcc99ff, cd.Transparent, 1)
' Put the text box 4 pixels to the right of the data point
Call textbox.setAlignment(cd.Left)
Call textbox.setPos(4, 0)
' Output the chart
Set viewer = cd.WebChartViewer(Request, "chart1")
Call viewer.setChart(c, cd.SVG)
' Include tool tip for the chart
viewer.ImageMap = c.getHTMLImageMap("", "", "title='Range = {x} miles, Capacity = {value} tons'")
%>
<!DOCTYPE html>
<html>
<head>
<title>Custom Scatter Labels</title>
<!-- Include ChartDirector Javascript Library to support chart interactions -->
<script type="text/javascript" src="cdjcv.js"></script>
</head>
<body style="margin:5px 0px 0px 5px">
<div style="font:bold 18pt verdana;">
Custom Scatter Labels
</div>
<hr style="border:solid 1px #000080; background:#000080" />
<div style="font:10pt verdana; margin-bottom:1.5em">
<a href="viewsource.asp?file=<%= Request("SCRIPT_NAME") %>">View Chart Source Code</a>
</div>
<!-- ****** Here is the chart image ****** -->
<%= viewer.renderHTML() %>
</body>
</html>
[Windows Version (in Visual Basic)] vbdemo\scatterlabels.cls
Public Sub createChart(viewer As Object, chartIndex As Integer)
Dim cd As New ChartDirector.API
' The XY points for the scatter chart
Dim dataX()
dataX = Array(150, 400, 300, 1500, 800)
Dim dataY()
dataY = Array(0.6, 8, 5.4, 2, 4)
' The labels for the points
Dim labels()
labels = Array("Nano<*br*>100", "SpeedTron<*br*>200 Lite", "SpeedTron<*br*>200", _
"Marathon<*br*>Extra", "Marathon<*br*>2000")
' Create a XYChart object of size 450 x 400 pixels
Dim c As XYChart
Set c = cd.XYChart(450, 400)
' Set the plotarea at (55, 40) and of size 350 x 300 pixels, with a light grey border
' (0xc0c0c0). Turn on both horizontal and vertical grid lines with light grey color (0xc0c0c0)
Call c.setPlotArea(55, 40, 350, 300, &Hffffff, -1, &Hc0c0c0, &Hc0c0c0, -1)
' Add a title to the chart using 18pt Times Bold Itatic font.
Call c.addTitle("Product Comparison Chart", "timesbi.ttf", 18)
' Add a title to the y axis using 12pt Arial Bold Italic font
Call c.yAxis().setTitle("Capacity (tons)", "arialbi.ttf", 12)
' Add a title to the x axis using 12pt Arial Bold Italic font
Call c.xAxis().setTitle("Range (miles)", "arialbi.ttf", 12)
' Set the axes line width to 3 pixels
Call c.xAxis().setWidth(3)
Call c.yAxis().setWidth(3)
' Add the data as a scatter chart layer, using a 15 pixel circle as the symbol
Dim layer As ScatterLayer
Set layer = c.addScatterLayer(dataX, dataY, "", cd.GlassSphereShape, 15, &Hff3333, &Hff3333)
' Add labels to the chart as an extra field
Call layer.addExtraField(labels)
' Set the data label format to display the extra field
Call layer.setDataLabelFormat("{field0}")
' Use 8pt Arial Bold to display the labels
Dim textbox As ChartDirector.TextBox
Set textbox = layer.setDataLabelStyle("arialbd.ttf", 8)
' Set the background to purple with a 1 pixel 3D border
Call textbox.setBackground(&Hcc99ff, cd.Transparent, 1)
' Put the text box 4 pixels to the right of the data point
Call textbox.setAlignment(cd.Left)
Call textbox.setPos(4, 0)
' Output the chart
Set viewer.Picture = c.makePicture()
'include tool tip for the chart
viewer.ImageMap = c.getHTMLImageMap("clickable", "", _
"title='Range = {x} miles, Capacity = {value} tons'")
End Sub
© 2021 Advanced Software Engineering Limited. All rights reserved.