-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvizThree.js
More file actions
61 lines (48 loc) · 1.61 KB
/
vizThree.js
File metadata and controls
61 lines (48 loc) · 1.61 KB
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
const ht = 500,
wh = 700;
const svgThree = d3.select("#vizThree")
.append("svg")
.attr("width", wh)
.attr("height", ht)
.attr("class","svgThree")
d3.json("data.json", function(error, data) {
if (error) throw error
data.forEach(function(d) {
d.estimate = +d.estimate
})
console.log(data)
var x = d3.scaleBand()
.domain(d3.range(data.length))
.range([0,wh])
.paddingInner(0.1)
.paddingOuter(0.2)
var y = d3.scaleLinear()
.domain(d3.extent(data, function(d){ return d.estimate}))
.range([ht, 0])
var colors = ["green", "orange", "red", "blue","purple"]
function randomColor() {
return colors[Math.floor(Math.random() * colors.length)]
}
var rect = svgThree.selectAll("rect").data(data)
rect
.enter()
.append("rect")
.attr("x",function(d,i) { return x(i)})
.attr("width", x.bandwidth())
.attr("y", function(d) {return y(d.estimate)})
.attr("fill", randomColor)
.attr("height", function(d){ return ht - y(d.estimate)})
.append("title")
.text(function (d) {
return d.country});
// svg.append("g")
// .attr("class", "axis-y")
// .call(d3.axisLeft(y).ticks(6, "s"))
// .append("text")
// .attr("transform", "rotate(-90)")
var leftAxis = d3.axisRight(y);
svgThree.append("g")
.attr("class", "axis")
.attr("transform","translate("+ 0 +",0)")
.call(leftAxis);
})