-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinegraph.js
277 lines (223 loc) · 9.34 KB
/
linegraph.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Using code from this: https://d3-graph-gallery.com/graph/line_several_group.html
// and this: https://www.data-to-viz.com/caveat/spaghetti.html
var softColours = [
"#B22222", "#002d9c", "#7c1158","#5d914c"
];
function getVisualizationDimensions(containerId) {
var container = document.getElementById(containerId);
var containerWidth = container.getBoundingClientRect().width;
var margin = { top: 10, right: 30, bottom: 30, left: 60 };
var width = containerWidth - margin.left - margin.right;
var height = (width / 800) * 500; // Maintain the aspect ratio
return { width, height, margin };
}
var baseFontSize = 16
var axisFontSize = baseFontSize * 0.85
var yLabelFontSize = baseFontSize * 0.95
var tickValues = d3.range(2010, 2024, 2);
// Append the svg object to the body of the page
var dims = getVisualizationDimensions('my_dataviz1');
var svg = d3.select("#my_dataviz1")
.append("svg")
.attr("viewBox", `0 0 ${dims.width + dims.margin.left + dims.margin.right} ${dims.height + dims.margin.top + dims.margin.bottom}`)
.attr("preserveAspectRatio", "xMinYMin meet")
.append("g")
.attr("transform", `translate(${dims.margin.left}, ${dims.margin.top})`);
// Info display setup
var infoDisplay = svg.append("g")
.attr("transform", "translate(" + (dims.width - 270) + ",30)");
infoDisplay.append("rect")
.attr("width", 245)
.attr("height", 100)
.attr("fill", "white")
.attr("rx", 4)
.attr("stroke-width", 2.5)
.style("opacity", 0.9);
var infoText = infoDisplay.append("text")
.attr("x", 20)
.attr("y", 60)
.style("text-anchor", "start")
.style("font-size", baseFontSize + "px");
// Prepare category data
const highlightCategories = ["Violent charges", "Sex offence charges", "Military misdemeanours", "Theft charges"];
const categoryTexts = {
"Violent charges": "Violent charges saw a considerable growth, featuring in 35% of all court martial cases in 2010 and 62% in 2023",
"Sex offence charges": "Sex offences saw the biggest change, growing from one in 20 cases in 2010 to one in three in 2023",
"Military misdemeanours": "Military misdemeanour charges have been trending downwards since 2010",
"Theft charges": "Theft charges remained similarly common between 2010 and 2023"
};
// Read the data and create the graph
(async function loadAndProcessData() {
try {
const data = await d3.csv("yearly_charges_percentages_long_format_cleaned.csv");
data.forEach(d => {
d.year = +d.year;
d.value = +d.value;
});
var sumstat = d3.group(data, d => d.category);
// Axis setup
var x = d3.scaleLinear()
.domain(d3.extent(data, d => d.year))
.range([0, dims.width]);
svg.append("g")
.attr("transform", "translate(0," + dims.height + ")")
.call(d3.axisBottom(x)
.tickValues(tickValues)
.tickFormat(d3.format("d")))
.selectAll("text")
.style("font-size", axisFontSize);
var y = d3.scaleLinear()
.domain([0, 100])
.range([dims.height, 0]);
svg.append("g")
.call(d3.axisLeft(y))
.selectAll("text")
.style("font-size", axisFontSize + "px");
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 0)
.attr("x", 0)
.attr("dy", "-2.5em")
.attr("transform", "rotate(-90)")
.style("font-size", yLabelFontSize + "px")
.text("Proportion of court martial cases featuring this charge (%)");
var colour = d3.scaleOrdinal()
.domain(highlightCategories)
.range(softColours);
let currentCategoryIndex = -1;
function highlightNextCategory() {
currentCategoryIndex = (currentCategoryIndex + 1) % highlightCategories.length;
updateVisualisation();
}
function highlightLastCategory() {
currentCategoryIndex = (currentCategoryIndex - 1 + highlightCategories.length) % highlightCategories.length;
updateVisualisation();
}
function updateVisualisation() {
const currentCategory = highlightCategories[currentCategoryIndex];
const categoryColour = colour(currentCategory);
// Update line styles and colours
svg.selectAll(".line").remove();
svg.selectAll(".line")
.data(sumstat)
.enter()
.append("path")
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", d => d[0] === currentCategory ? categoryColour : "grey")
.attr("stroke-width", d => d[0] === currentCategory ? 3 : 1)
.attr("d", d => d3.line()
.x(d => x(d.year))
.y(d => y(d.value))
(d[1]));
// Update text information display
updateInfoDisplay(categoryTexts[currentCategory]);
// Change the rectangle's fill colour to match the line colour
infoDisplay.select("rect").attr("stroke", categoryColour);
}
function updateInfoDisplay(text) {
infoText.selectAll("*").remove();
const rectHeight = 110;
const x = 10;
const maxWidth = 230;
// Start the first line of text
var tspan = infoText.append("tspan")
.attr("x", x)
.attr("y", 10)
.attr("text-anchor", "start");
var words = text.split(/\s+/);
var line = [];
words.forEach(function(word) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > maxWidth) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = infoText.append("tspan")
.attr("x", x)
.attr("dy", "1.2em")
.text(word);
}
});
// Add link for where i found bbox guide
var bbox = infoText.node().getBBox();
var textHeight = bbox.height;
var startY = (rectHeight - textHeight) / 2 + bbox.y;
// Adjust the text group position to center it vertically
infoText.attr("transform", "translate(0," + (startY - bbox.y) + ")");
}
// Setup button event listener
document.getElementById("backButton").addEventListener("click", highlightLastCategory);
document.getElementById("nextButton").addEventListener("click", highlightNextCategory);
// Initially load the first category details
highlightNextCategory();
} catch (error) {
console.error('Error with CSV:', error);
}
})();
function setupBasicLineGraph() {
console.log("Setting up the basic line graph...");
var dims = getVisualizationDimensions('my_dataviz2');
var svg = d3.select("#my_dataviz2")
.append("svg")
.attr("viewBox", `0 0 ${dims.width + dims.margin.left + dims.margin.right} ${dims.height + dims.margin.top + dims.margin.bottom}`)
.attr("preserveAspectRatio", "xMinYMin meet")
.append("g")
.attr("transform", `translate(${dims.margin.left}, ${dims.margin.top})`);
// Scales and axes setup with the same domains and styles
var x = d3.scaleLinear()
.domain([2010, 2023])
.range([0, dims.width]);
var y = d3.scaleLinear()
.domain([0, 100])
.range([dims.height, 0]);
svg.append("text")
.attr("class", "y label2")
.attr("text-anchor", "end")
.attr("y", 4)
.attr("x", 0)
.attr("dy", "-3em")
.attr("transform", "rotate(-90)")
.style("font-size", yLabelFontSize + "px")
.text("Conviction rate on violent and sex offence charges (%)");
d3.csv("long_format_conviction_rates.csv").then(function(data) {
console.log("Data loaded successfully:", data);
data.forEach(function(d) {
d.year = +d.year;
d.value = +d.value;
});
var sumstat = d3.group(data, d => d.category);
svg.append("g")
.attr("transform", "translate(0," + dims.height + ")")
.call(d3.axisBottom(x)
.tickValues(tickValues)
.tickFormat(d3.format("d")))
.selectAll("text")
.style("font-size", axisFontSize);
// Set up the Y Axis with larger font size
svg.append("g")
.call(d3.axisLeft(y))
.selectAll("text")
.style("font-size", axisFontSize);
var colour = d3.scaleOrdinal()
.domain(highlightCategories)
.range(softColours);
sumstat.forEach(function(values, key) {
svg.append("path")
.datum(values)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", colour(key))
.attr("stroke-width", 3)
.attr("d", d3.line()
.x(d => x(d.year))
.y(d => y(d.value))
);
});
}).catch(function(error) {
console.error('Error loading the CSV:', error);
});
}
setupBasicLineGraph();