-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.js
155 lines (140 loc) · 4.54 KB
/
controller.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
153
'use strict';
const model = new Model('data.json');
var statusColorScale;
var powergapColorScale;
var chart;
model.loadData()
.then((data) => {
model.data = data;
statusColorScale = d3.scaleOrdinal()
.domain(model.targetRolesByRank)
.range(d3.schemeSet3);
chart = new Chart({
data: model.getDataByDiscipline({sortValuesBy:'targetvalue'}),
container: document.querySelector('.chart-container'),
legend: {
status: d3.select('.legend-status'),
powergap: d3.select('.legend-powergap')
},
infobox: d3.select('.infobox'),
width: 1000,
height: 600,
margin: {top: 10, right: 40, bottom: 40, left: 100},
categories: model.targetRolesByRank,
colorScale: statusColorScale,
dotsPerBandWidth: 3,
colorScaleParam: 'targetvalue'
});
chart.draw();
//Move this somewhere cleaner
// Discipline Legend
d3.select('.legend-status .legend__items').selectAll('button')
.data(model.targetRolesByRank)
.enter()
.append('button')
.on('mouseover', () => {
var btn = d3.select(d3.event.target);
var currentFill = btn.style('background-color');
btn.style('background-color', () => d3.color(currentFill).darker(0.25));
})
.on('mouseout', (d) => {
d3.select(d3.event.target).style('background-color',
d => chart.colorScale(d));
})
.on('click', (d) => {
chart.data = model.getDataByDiscipline({
sortValuesBy: chart.colorScaleParam,
priorityValue: model.targetRolesByRank.indexOf(d)
})
chart.statusFilterValue = model.targetRolesByRank.indexOf(d);
chart.drawBars();
chart.drawDots();
resetBlurb();
})
.attr('class', 'btn')
.style('background-color', (d) => chart.colorScale(d))
.text((d) => d);
powergapColorScale = d3.scaleSequential(d3.interpolateBrBG)
.domain([-model.targetRolesByRank.length, model.targetRolesByRank.length]);
// Powergap legend
const powergapScale = [];
for (let i=-model.targetRolesByRank.length+1, ii=model.targetRolesByRank.length; i<ii; i++) {
powergapScale.push(i);
}
d3.select('.legend-powergap .legend__items').selectAll('span')
.data(powergapScale)
.enter()
.append('span')
.attr('style', (d) => {
return `background-color: ${powergapColorScale(d)};`;
})
.text((d) => d);
})
.catch((error) => {
throw error;
});
d3.select('.controls').on('click', function() {
d3.select(this).selectAll('button').classed('active', false);
d3.select(d3.event.target).classed('active', true)
});
d3.select('#show-target-role').on('click', showTargetStatus);
d3.select('#show-perp-role').on('click', showPerpStatus);
d3.select('#show-powergap').on('click', showPowerGap);
d3.select('body').on('click', () => {
if (d3.event.target.nodeName == 'svg' || d3.event.target.nodeName == 'DIV') {
if (chart.statusFilterValue != null) {
resetChart();
} else {
d3.selectAll('.selected').classed('selected', false);
resetBlurb();
}
}
});
function resetChart() {
if (chart.colorScaleParam == 'perpvalue') {
showPerpStatus();
} else if (chart.colorScaleParam == 'powergap') {
showPowerGap();
} else {
showTargetStatus();
}
}
function resetBlurb() {
if (chart.colorScaleParam == 'perpvalue') {
chart.displayPerpetratorBlurb();
} else if (chart.colorScaleParam == 'powergap') {
chart.displayPowerGapBlurb();
} else {
chart.displayTargetBlurb();
}
}
function showTargetStatus() {
chart.statusFilterValue = null;
chart.data = model.getDataByDiscipline({sortValuesBy: 'targetvalue'});
chart.colorScale = statusColorScale;
chart.colorScaleParam = 'targetvalue';
chart.drawBars();
chart.drawDots();
chart.toggleLegend('powergap','status');
chart.displayTargetBlurb();
}
function showPerpStatus() {
chart.statusFilterValue = null;
chart.data = model.getDataByDiscipline({sortValuesBy: 'perpvalue'});
chart.colorScale = statusColorScale;
chart.colorScaleParam = 'perpvalue';
chart.drawBars();
chart.drawDots();
chart.toggleLegend('powergap','status');
chart.displayPerpetratorBlurb();
}
function showPowerGap() {
chart.statusFilterValue = null;
chart.data = model.getDataByDiscipline({sortValuesBy: 'powergap'});
chart.colorScale = powergapColorScale;
chart.colorScaleParam = 'powergap';
chart.drawBars();
chart.drawDots();
chart.toggleLegend('status','powergap');
chart.displayPowerGapBlurb();
}