-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
152 lines (126 loc) · 3.38 KB
/
scripts.js
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
// Following steps order and formatting Amelia Wattenberger outlines
// in her Fullstack D3 book. This structure helps organize
// all the steps we have to take in d3!
// 1. Access data
const dataset = [
{
name: "puppies",
quantity: 10
},
{
name: "kittens",
quantity: 8
},
{
name: "bananas",
quantity: 12
},
{
name: "pizzas",
quantity: 8
},
{
name: "avocados",
quantity: 3
},
{
name: "lasagnas",
quantity: 4
},
]
const yAccessor = d => d.quantity
const xAccessor = d => d.name
// 2. Create chart dimensions
let dimensions = {
width: 500,
height: 400,
margin: {
top: 15,
right: 15,
bottom: 60,
left: 40,
},
}
dimensions.boundedWidth = dimensions.width
- dimensions.margin.left
- dimensions.margin.right
dimensions.boundedHeight = dimensions.height
- dimensions.margin.top
- dimensions.margin.bottom
// 3. Draw canvas
const wrapper = d3.select("#wrapper")
.append("svg")
.attr("width", dimensions.width)
.attr("height", dimensions.height)
const bounds = wrapper.append("g")
.style("transform", `translate(${
dimensions.margin.left
}px, ${
dimensions.margin.top
}px)`)
// 4. Create scales
const xScale = d3.scaleBand()
.domain(dataset.map(d => d.name))
.range([0, dimensions.boundedWidth])
.padding(0.2)
const yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, yAccessor)])
.range([dimensions.boundedHeight, 0])
// 5. Draw data
bounds.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr('x', d => xScale(d.name))
.attr("y", d => yScale(yAccessor(d)))
.attr('width', d => xScale.bandwidth())
.attr("height", d => dimensions.boundedHeight
- yScale(d.quantity)
)
// 6. Draw peripherals
const xAxisGenerator = d3.axisBottom()
.scale(xScale)
const xAxis = bounds.append("g")
.call(xAxisGenerator)
.attr("class", "axis x-axis")
.style("transform", `translateY(${dimensions.boundedHeight}px)`)
const xAxisLabel = xAxis.append("text")
.attr("x", dimensions.boundedWidth / 2)
.attr("y", dimensions.margin.bottom - 10)
.style("font-size", "1.4em")
.text("Our chart of amazing things")
.attr("fill", "black")
const yAxisGenerator = d3.axisLeft()
.scale(yScale)
const yAxis = bounds.append("g")
.attr("class", "axis y-axis")
.call(yAxisGenerator)
const yAxisLabel = yAxis.append("text")
.attr("x", -dimensions.boundedHeight / 2)
.attr("y", -dimensions.margin.left + 10)
.style("transform", "rotate(-90deg)")
.style("text-anchor", "middle")
.style("font-size", "1.4em")
.text("Quantity")
.attr("fill", "black")
// 7. Set up interactions
bounds.selectAll("rect")
.on("mouseenter", onMouseEnter)
.on("mouseleave", onMouseLeave)
const tooltip = d3.select("#tooltip")
function onMouseEnter(datum) {
tooltip.text(yAccessor(datum) + " " + datum.name)
const x = (xScale.bandwidth() / 2)
+ xScale(datum.name)
+ dimensions.margin.left
const y = yScale(yAccessor(datum))
+ dimensions.margin.top
tooltip.style("transform", `translate(`
+ `calc(-50% + ${x}px),`
+ `calc(-100% + ${y}px)`
+ `)`)
tooltip.style("opacity", 1)
}
function onMouseLeave() {
tooltip.style("opacity", 0)
}