forked from InteractiveDataVis/Interactive-Data-Vis-Sp2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
76 lines (65 loc) · 2.85 KB
/
main.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
// data load
// reference for d3.autotype: https://github.com/d3/d3-dsv#autoType
d3.csv('astro.csv', d3.autoType)
.then(data => {
// confirm that the data loaded in by looking in the console
console.log("data", data)
/** CONSTANTS */
// constants help us reference the same values throughout our code
const width = window.innerWidth * .8;
const height = window.innerHeight /1.6;
const margins = {top: 45, bottom: 30, left: 25, right: 90}; //names-left
const color = d3.scaleSequential()
.domain([0, d3.max(data, d => d.days)])
.interpolator(d3.interpolateBlues)
/** SCALES */
// reference for d3.scales: https://github.com/d3/d3-scale
const yScale = d3.scaleBand()
.domain(data.map(d=> d.name)) // get all the `activity` values
.range([300, margins.top, height - margins.bottom])
.paddingInner(.5) //spacing bet bars
.paddingOuter(.1)
const xScale = d3.scaleLinear()
.domain([0, d3.max(data, d => d.days)]) // grab the max value
.range([margins.left, width - margins.right])
//console.log(yScale, yScale('Scott Kelly'))
/** DRAWING ELEMENTS */
const svg = d3.select('#barchart-container')
.append("svg")
.attr("width", width)
.attr("height", height)
//console.log(svg)
// draw rect bars
svg.selectAll("rect")
.data(data)
.join("rect")
.attr("width", d=> xScale(d.days) - margins.right) //width - margins.left - xScale(d.days))
.attr("height", yScale.bandwidth())
.attr("x", margins.right)
.attr("y", d=>yScale(d.name))
//.attr("transform", `translate(100, ${height - margin.bottom, margin.top})`)
.attr("fill", d=>color(d.days)) // try changing this to be a dynamic colorscale
.attr("y", d => yScale(d.name))
.attr("x", 50, d => xScale(d.days)) //movingbar
// draw bottom 'activity' text
svg.selectAll("text.name")
.data(data)
.join("text")
.attr("class", 'name')
.attr("y", d=> yScale(d.name))//+(yScale.bandwidth() / 4))
.attr("x", d=> margins.left)
.attr("dx", "2em") // adjust the text a bit lower down
.attr("text-anchor", 'start') // set the x/y to refer to the middle of the word
.text(d => d.name) // set the text
// draw top 'count' text
svg.selectAll("text.days")
.data(data)
.join("text")
.attr("class", 'days')
.attr("y", d => yScale(d.name) + (yScale.bandwidth() / 2))
.attr("x", d => xScale(d.days))
.attr("dy", "0.4em") // adjust the text a bit lower down
.attr("dx", "-0.8em")
.attr("text-anchor", 'end') // set the x/y to refer to the middle of the word
.text(d => d3.format(",")(d.days)) // set the text, add a formatter to properly format numbers: https://github.com/d3/d3-format
})