-
Notifications
You must be signed in to change notification settings - Fork 3
/
SampleSpace.js
93 lines (78 loc) · 2.75 KB
/
SampleSpace.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
'use strict';
export default class SampleSpace {
constructor(el, sides) {
this.mount = el;
this.sides = sides;
this.faces = [1, 2, 3, 4, 5, 6];
this.data = this.tabulate();
}
init() {
// we need to constrain
const margin = { top: 20, right: 20, bottom: 40, left: 40 },
width = document.getElementsByClassName('sample-space')[0].clientWidth - 100 - margin.left - margin.right,
height = document.getElementsByClassName('sample-space')[0].clientHeight - 100 - margin.top - margin.bottom;
// define a container and coordinate space
const graph = d3.select(this.mount).select('svg')
.attr('width', width + margin.right + margin.left)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
// define scales, input domain and output range
const x = d3.scalePoint()
.padding(0.5)
.domain(this.faces)
.range([0, width]);
const y = d3.scalePoint()
.padding(0.5)
.domain(this.faces)
.range([height, 0]);
const xAxis = d3.axisBottom(x),
yAxis = d3.axisLeft(y);
graph.append('g')
.attr('class', 'x-axis')
.attr('transform', `translate(0, ${height})`)
.call(xAxis);
graph.append('g')
.attr('class', 'y-axis')
.call(yAxis);
const offset = y.step() / 2;
graph.selectAll('.data-band')
.data(this.data)
.enter().append('g')
.attr('class', 'data-band')
.attr('transform', (d, i) => `translate(0, ${i * y.step() + offset})`);
d3.selectAll('.data-band')
.each(function(row) {
console.log(row);
d3.select(this).selectAll('.point')
.data(row)
.enter().append('circle')
.attr('class', 'point')
.attr('r', 3.5)
.attr('cx', d => x(d.x))
.style('fill', '#444');
});
}
sum(roll1, roll2) {
const points = d3.selectAll('.point');
points.transition(300).attr('r', 3.5).style('fill', '#444');
points.each(function(d) {
const sum = d.x + d.y;
const point = d3.select(this);
sum == (roll1 + roll2) ? point.transition(300).attr('r', 6).style('fill', '#f44336') : null;
});
}
event(roll) {
const points = d3.selectAll('.point');
points.transition(300).attr('r', 3.5).style('fill', '#444');
points.each(function(d) {
const point = d3.select(this);
`${d.x}${d.y}` == roll ? point.transition(300).attr('r', 6).style('fill', '#f44336') : null;
});
}
tabulate() {
const matrix = this.faces.map((elem, i, obj) => obj.map(x => ({ x: x, y: obj[i] }))).reverse();
console.log(matrix);
return matrix;
}
}