-
Notifications
You must be signed in to change notification settings - Fork 4
/
canvas.html
183 lines (167 loc) · 4.78 KB
/
canvas.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE HTML>
<head>
<title>canvas examples</title>
<script type="text/javascript" src="lib/jquery.min.js"></script>
<script type="text/javascript" src="lib/modernizr.js"></script>
<script type="text/javascript" src="lib/js-webshim/minified/polyfiller.js"></script>
<script type="text/javascript" src="lib/underscore.js"></script>
<script type="text/javascript">
var map = _.map;
</script>
<!--
<script type="text/javascript" src="lib/json2.js"></script>
<script type="text/javascript" src="lib/js-shim.js"></script>
<script type="text/javascript">
var CSSStyleDeclaration = function() {};
CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
return this.getAttribute(a);
};
CSSStyleDeclaration.prototype.setProperty = function(styleName, value, priority) {
this.setAttribute(styleName,value);
var priority = typeof priority != 'undefined' ? priority : '';
if (priority != '') {
// Add priority manually
var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*' + RegExp.escape(value) + '(\\s*;)?', 'gmi');
this.cssText = this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
}
}
CSSStyleDeclaration.prototype.removeProperty = function(a) {
return this.removeAttribute(a);
}
CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
var rule = new RegExp(RegExp.escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?', 'gmi');
return rule.test(this.cssText) ? 'important' : '';
}
RegExp.escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
</script>
-->
<script type="text/javascript" src="lib/d3.v2.js"></script>
<script type="text/javascript">
window.FlashCanvasOptions = {
disableContextMenu: true
};
$.webshims.setOptions('canvas', {type: 'flashpro' });
$.webshims.polyfill('canvas');
$(function() {
var w = 720,
h = 300,
p = [0, 25, 15, 0],
transition_count = 0,
last = [],
x = d3.scale.ordinal().rangeRoundBands([0, w - p[1] - p[3]]),
y = d3.scale.linear().range([0, h - p[0] - p[2]]),
z = d3.scale.ordinal().range(["darkgray", "lightblue"]),
parse = d3.time.format("%b %e").parse,
format = d3.time.format("%b %e"),
swap = true;
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');
d3.json("revenue.json", function(json) {
//Draw graph on load
var data = d3.layout.stack()(_(json[0]).map(function(row) {
return row;
}));
render(data);
last = data;
});
$('#switch').click(function() {
if (swap) {
transition("rev-two.json", ++transition_count);
swap = false;
} else {
transition("revenue.json", ++transition_count);
swap = true;
}
});
function render(data) {
// Compute the x-domain and y-domain.
y.domain([0, d3.max(_(data).map(function(d) {
return d3.max(d, function(e, i) {
return data[1][i].y + data[0][i].y
})}))*1.1]);
x.domain(_(data[1]).map(function(d) { return d.x; }));
// y axis
_(y.ticks(4)).map(drawAxis);
// Draw bars
_(data).each(function(row, i) {
_(row).each(function(d, j) {
drawBar(i, j, d);
});
});
};
function drawBar(i, j, d) {
ctx.fillStyle = z(i);
ctx.fillRect(p[1]+x(d.x)+1, h-p[2]-y(d.y0)-y(d.y), x.rangeBand()-1, y(d.y));
// x axis
if (j%7 == 0 || j==0) {
ctx.fillStyle = "#999";
ctx.fillText(format(new Date(d.x)), x(d.x) + x.rangeBand(), h-2);
}
};
function drawAxis(a) {
//draw y axis
ctx.fillStyle = "#999";
if (a != 0) {
ctx.fillText(a, 0, h-p[2]-y(a)+12);
ctx.lineWidth = .1;
ctx.beginPath();
ctx.moveTo(0, h-p[2]-y(a));
ctx.lineTo(w, h-p[2]-y(a));
ctx.stroke();
} else {
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, h-p[2]-y(a)+1);
ctx.lineTo(w, h-p[2]-y(a)+1);
ctx.stroke();
}
};
function transition(file, count) {
d3.json(file, function(json) {
var data = d3.layout.stack()(_(json[0]).map(function(row) { return row; }));
// find next positions by interpolating arrays
var transition = d3.interpolate(position(last), position(data));
// run transition
d3.timer(function(t) {
// abort old transition
if (count < transition_count) return true;
clear();
if (t > 1000) {
last = data;
render(last);
return true
};
last = transition(t/1000);
render(resumeData(last));
});
});
};
function resumeData(data) {
return _(data).map(function(d) {
return _(d).map(function(e) {
return {"x":e[0],"y":e[1],"y0":e[2]}
})
});
}
function position(data) {
return _(data).map(function(d) {
return _(d).map(function(e) {
return [e.x,e.y,e.y0]
})
});
};
// clear canvas
function clear() {
ctx.clearRect(0,0,w,h);
}
});
</script>
</head>
<body>
<div id="graphs"></div>
<canvas width="720" height="300" id="canvas"></canvas>
<button id="switch">Switch</button>
</body>
</html>