-
Notifications
You must be signed in to change notification settings - Fork 0
/
impactFunctions.js
86 lines (68 loc) · 2.39 KB
/
impactFunctions.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
// Function to render the impact matrix as an HTML table
export function renderImpactMatrix(matrix) {
let html = '<table class="impact-matrix futuristic-table">';
// Render table header
html += '<thead><tr><th class="header-cell">Policy/Node</th><th class="header-cell">Influenced By</th><th class="header-cell">Influences</th></tr></thead>';
// Render table rows
html += '<tbody>';
matrix.forEach(row => {
html += '<tr>';
html += `<td>${row.name}</td>`;
html += renderImpactCell(row.influencedBy); // Render influencedBy impact cell
html += renderImpactCell(row.influences); // Render influences impact cell
html += '</tr>';
});
html += '</tbody>';
html += '</table>';
return html;
}
// Function to render an impact cell with color
export function renderImpactCell(impact) {
const color = getImpactColor(impact);
return `<td style="background-color: ${color};">${impact !== null ? impact : '-'}</td>`;
}
// Function to create the impact matrix
export function createImpactMatrix(gameState, policyId) {
const matrix = [];
const relevantImpacts = gameState.impacts.filter(impact => impact.from === policyId || impact.to === policyId);
relevantImpacts.forEach(impact => {
let otherNodeId = impact.from === policyId ? impact.to : impact.from;
let existingRow = matrix.find(row => row.name === otherNodeId);
if (!existingRow) {
existingRow = {
name: otherNodeId,
influencedBy: null,
influences: null
};
matrix.push(existingRow);
}
if (impact.from === policyId) {
existingRow.influences = impact.impact;
} else {
existingRow.influencedBy = impact.impact;
}
});
return matrix;
}
// Function to calculate the color based on impact value
export function getImpactColor(impact) {
const colorScale = d3.scaleLinear()
.domain([-100, 0, 100])
.range(['red', 'white', 'green']);
return colorScale(impact);
}
// Function to show the strength of the impact
export function showImpactStrength(impact, x, y) {
let strength = Math.abs(impact.impact);
let tooltip = d3.select("#tooltip")
.style("left", `${x}px`)
.style("top", `${y}px`)
.style("opacity", 0);
tooltip.html(`${strength}`);
//tooltip.html(`Impact Strength: ${strength}`);
}
// Function to hide the impact strength
export function hideImpactStrength() {
let tooltip = d3.select("#tooltip")
.style("opacity", 0);
}