/
chart.html
146 lines (117 loc) · 5.29 KB
/
chart.html
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<title>Canvas 2D Context API Primer</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=1024" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<!--STYLES-->
<link href="css/main.css" rel="stylesheet" />
<!--SCRIPTS-->
<script type="text/javascript">
/*
Data retrieved from:
http://gs.statcounter.com/#browser-ww-monthly-201209-201309-bar
File transformed to JSON using transformed using http://www.convertcsv.com/csv-to-json.htm
*/
// Loading a JSON file
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'data/browser.data.json', true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// .open will NOT return a value but simply returns undefined in async mode so use a callback
callback(xobj.responseText);
}
}
xobj.send(null);
}
// Call to function with anonymous callback
loadJSON(function(response) {
// Get a reference to the canvas
var canvas = document.getElementById('canvas');
// Get a reference to the drawing context
var ctx = canvas.getContext('2d');
// Draw chart area
ctx.fillStyle = "white";
ctx.fillRect(0,0,canvas.width,canvas.height);
// Draw axis lines
ctx.fillStyle = "black";
ctx.lineWidth = 2.0;
ctx.beginPath();
ctx.moveTo( 50,10);
ctx.lineTo( 50, canvas.height-20 );
ctx.lineTo( canvas.width - 20, canvas.height-20 );
ctx.stroke();
// Retrieve data
var jsonresponse = JSON.parse(response);
if( jsonresponse.length > 0 ){
var ITEM_COUNT = 5;
var colors = [ "#01C001", "#0152D8", "#E46F0F", "#868686", "#FF2727"];
var axisFraction = jsonresponse[0].Percentage / ITEM_COUNT;
axisFraction = ( Math.ceil( axisFraction ));
var axisPadding = ( canvas.width - 80 ) / ITEM_COUNT;
// Draw chart
for( var i = 0; i < ITEM_COUNT; ++i ){
///////////////////////////////////////////
// Draw data
///////////////////////////////////////////
// Retrieve the usage percentage of the browser
var pctg = jsonresponse[i].Percentage;
// Create the linear gradient
var gradient = ctx.createLinearGradient( 0, 0, canvas.width, 0 );
// Add the colors with fixed stops
gradient.addColorStop( "0", "white" );
gradient.addColorStop( "0.25", colors[i] );
gradient.addColorStop( "0.5", colors[i] );
gradient.addColorStop( "1.0", colors[i] );
// Set the gradient as the fill style
ctx.fillStyle = gradient;
ctx.fillRect( 51, 25 + i * 100, pctg * 20, 50 );
///////////////////////////////////////////
// X-Axis Data
///////////////////////////////////////////
// Separation line
ctx.fillStyle = "black";
ctx.lineWidth = 0.5;
ctx.beginPath();
ctx.moveTo( 50 + i * axisPadding, 10);
ctx.lineTo( 50 + i * axisPadding, canvas.height - 20 );
ctx.stroke();
// Percentage
ctx.fillStyle = "black";
ctx.fillText( i * axisFraction + "%", 50 + i * axisPadding, canvas.height );
///////////////////////////////////////////
// Y-Axis Data
///////////////////////////////////////////
ctx.fillStyle = "black";
// Browser name
ctx.fillText( jsonresponse[i].Browser + "", 4, i * 100 + 50 );
}
// Separation line
ctx.fillStyle = "black";
ctx.lineWidth = 0.5;
ctx.beginPath();
ctx.moveTo( 50 + ITEM_COUNT * axisPadding, 10);
ctx.lineTo( 50 + ITEM_COUNT * axisPadding, canvas.height - 20 );
ctx.stroke();
// Percentage
ctx.fillStyle = "black";
ctx.fillText( ITEM_COUNT * axisFraction + "%", 50 + ITEM_COUNT * axisPadding, canvas.height );
}
});
</script>
</head>
<body>
<p>
<a href="index.html">Back to index</a>
</p>
<div id="content">
<div id="header">
<h3>Top 5 Browsers from Sept 2012 to Sept 2013</h3>
</div>
<canvas id="canvas" width="860" height="520"></canvas>
</div>
</body>
</html>