-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3example.html
More file actions
57 lines (52 loc) · 1.34 KB
/
d3example.html
File metadata and controls
57 lines (52 loc) · 1.34 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
<html>
<head>
<style>
body{
height:100%;
width:100%;
}
svg{
border:1px solid black;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="container1"></div>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var width = 400, height = 400;
let bardata = [ {
year:2000,pop:20},{year:2001,pop:20},{year:2002,pop:60},{year:2003,pop:70},{year:2004,pop:10},{year:2005,pop:60},{year:2006,pop:90},{year:2007,pop:100}]
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
let margin=100
var xscale = d3.scaleBand()
.domain(bardata.map((d) =>{return d.year}))
.range([0, width - margin]).padding(0.1);
var yscale = d3.scaleLinear()
.domain([0,d3.max(bardata,(d)=>{return d.pop})])
.range([height-margin,0])
let g = svg.append("g")
.attr("transform","translate(50,50)")
svg.append("g")
.attr("transform", `translate(50,${height-50})`)
.call(d3.axisBottom(xscale))
svg.append("g")
.attr("transform", `translate(50,50)`)
.call(d3.axisLeft(yscale))
g.selectAll(".bar")
.data(bardata)
.enter()
.append("rect")
.classed("bar",true)
.attr("x", (d)=>{return xscale(d.year)})
.attr("y", (d)=>{return yscale(d.pop)})
.attr("width",xscale.bandwidth())
.attr("height",(d)=>{return (height-margin)-yscale(d.pop)})
.attr("fill","blue")
</script>
</body>
</html>