-
Notifications
You must be signed in to change notification settings - Fork 1
/
not_python.html
77 lines (60 loc) · 2.09 KB
/
not_python.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v6.js"></script>
<!-- <script src="not_python.js"></script> -->
<!-- Add 2 buttons -->
<button onclick="update('var1')">Variable 1</button>
<button onclick="update('var2')">Variable 2</button>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>
// set the dimensions and margins of the graph
const margin = {top: 30, right: 30, bottom: 70, left: 60},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
const svg = d3.select("#my_dataviz")
.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})`);
// Initialize the X axis
const x = d3.scaleBand()
.range([ 0, width ])
.padding(0.2);
const xAxis = svg.append("g")
.attr("transform", `translate(0,${height})`);
// Initialize the Y axis
const y = d3.scaleLinear()
.range([ height, 0]);
const yAxis = svg.append("g")
.attr("class", "myYaxis");
// A function that create / update the plot for a given variable:
function update(selectedVar) {
// Parse the Data
d3.csv("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/barplot_change_data.csv").then( function(data) {
// X axis
x.domain(data.map(d => d.group));
xAxis.transition().duration(1000).call(d3.axisBottom(x));
// Add Y axis
y.domain([0, d3.max(data, d => +d[selectedVar]) ]);
yAxis.transition().duration(1000).call(d3.axisLeft(y));
// variable u: map data to existing bars
const u = svg.selectAll("rect")
.data(data)
// update bars
u.join("rect")
.transition()
.duration(1000)
.attr("x", d => x(d.group))
.attr("y", d => y(d[selectedVar]))
.attr("width", x.bandwidth())
.attr("height", d => height - y(d[selectedVar]))
.attr("fill", "#69b3a2")
})
}
// Initialize plot
update('var1')
</script>