-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.js
107 lines (90 loc) · 3.09 KB
/
main3.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
import * as d3v6 from "d3-v6"
// set the dimensions and margins of the graph
const margin = {top: 10, right: 30, bottom: 20, left: 50},
width = 1000 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
// append the svg object to the body of the page
const svg = d3v6.select("#my_dataviz3")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",`translate(${margin.left},${margin.top})`);
// Parse the Data
d3v6.csv("gender.csv").then( function(data) {
// List of subgroups = header of the csv files = soil condition here
const subgroups = data.columns.slice(1)
// List of groups = species here = value of the first column called group -> I show them on the X axis
const groups = data.map(d => d.group)
// Add X axis
const x = d3v6.scaleBand()
.domain(groups)
.range([0, width])
.padding([0.2])
svg.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3v6.axisBottom(x).tickSizeOuter(0));
// Add Y axis
const y = d3v6.scaleLinear()
.domain([0, 100])
.range([ height, 0 ]);
svg.append("g")
.call(d3v6.axisLeft(y));
// color palette = one color per subgroup
const color = d3v6.scaleOrdinal()
.domain(subgroups)
.range(['#6495ED','#ff1493','#d9d574'])
//stack the data? --> stack per subgroup
const stackedData = d3v6.stack()
.keys(subgroups)
(data)
// ----------------
// Create a tooltip
// ----------------
const tooltip = d3v6.select("#my_dataviz3")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "1px")
.style("border-radius", "5px")
.style("padding", "10px")
.style("width","150px");
// Three function that change the tooltip when user hover / move / leave a cell
const mouseover = function(event, d) {
const subgroupName = d3v6.select(this.parentNode).datum().key;
const subgroupValue = d.data[subgroupName];
tooltip
.html("subgroup: " + subgroupName + "<br>" + "Value: " + subgroupValue)
.style("opacity", 1)
}
const mousemove = function(event, d) {
tooltip.style("transform","translateY(-55%)")
.style("left",(event.x)/2+"px")
.style("top",(event.y)/2-30+"px")
}
const mouseleave = function(event, d) {
tooltip
.style("opacity", 0)
}
// Show the bars
svg.append("g")
.selectAll("g")
// Enter in the stack data = loop key per key = group per group
.data(stackedData)
.join("g")
.attr("fill", d => color(d.key))
.selectAll("rect")
// enter a second time = loop subgroup per subgroup to add all rectangles
.data(d => d)
.join("rect")
.attr("x", d => x(d.data.group))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width",x.bandwidth())
.attr("stroke", "grey")
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
})