1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<script>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
exportEnabled: true,
title:{
text: "Sales via Advertisement"
},
data: [{
type: "funnel",
reversed: true,
showInLegend: true,
legendText: "{label}",
indexLabel: "{label} - {y}",
toolTipContent: "<b>{label}</b>: {y} <b>({percentage}%)</b>",
indexLabelFontColor: "black",
dataPoints: [
{ y: 3500, label: "Impressions" },
{ y: 2130, label: "Clicked" },
{ y: 1950, label: "Free Downloads" },
{ y: 500, label: "Purchase" },
{ y: 300, label: "Renewal" }
]
}]
});
calculatePercentage();
chart.render();
function calculatePercentage() {
var dataPoint = chart.options.data[0].dataPoints;
var total = dataPoint[0].y;
for(var i = 0; i < dataPoint.length; i++) {
if(i == 0) {
chart.options.data[0].dataPoints[i].percentage = 100;
} else {
chart.options.data[0].dataPoints[i].percentage = ((dataPoint[i].y / total) * 100).toFixed(2);
}
}
}
}
</script>
<script src="../../asset/js/canvasjs/canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 370px; max-width: 920px; margin: 0px auto;">
</div>
</body>
</html>