forked from siddharthraja/yelp-review-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradietVis.html
77 lines (64 loc) · 1.71 KB
/
gradietVis.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.space {
position: absolute;
}
.space div {
position: absolute;
top: 0;
left: 20px;
}
</style>
<body>
<script src="js/d3.v3.min.js"></script>
<script>
var spaces = [
{name: "1", interpolate: d3.interpolateHsl},
{name: "2", interpolate: d3.interpolateHcl},
{name: "3", interpolate: d3.interpolateLab},
{name: "4", interpolate: d3.interpolateRgb}
];
var y = d3.scale.ordinal()
.domain(spaces.map(function(d) { return d.name; }))
.rangeRoundBands([0, 500], .09);
var margin = y.range()[0],
width = 960 - margin - margin,
height = y.rangeBand();
var color = d3.scale.linear()
.domain([0, width])
.range(["hsl(102, 100%, 41%)", "hsl(360, 100%, 41%)"]);
var space = d3.select("body").selectAll(".space")
.data(spaces)
.enter().append("div")
.attr("class", "space")
.style("width", width + "px")
.style("height", height + "px")
.style("left", margin + "px")
.style("top", function(d, i) { return y(d.name) + "px"; });
space.append("canvas")
.attr("width", width)
.attr("height", 1)
.style("width", width + "px")
.style("height", height + "px")
.each(render);
space.append("div")
.style("line-height", height + "px")
.text(function(d) { return d.name; });
function render(d) {
var context = this.getContext("2d"),
image = context.createImageData(width, 1);
color.interpolate(d.interpolate);
for (var i = 0, j = -1, c; i < width; ++i) {
c = d3.rgb(color(i));
image.data[++j] = c.r;
image.data[++j] = c.g;
image.data[++j] = c.b;
image.data[++j] = 255;
}
context.putImageData(image, 0, 0);
}
</script>