Skip to content

Commit 0aa1bad

Browse files
update to chart instead of table
1 parent bc62128 commit 0aa1bad

3 files changed

Lines changed: 97 additions & 23 deletions

File tree

css/main.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,13 @@ table {
125125
.color-deepred { background-color: #a50026; color: white; }
126126
.table-cell { padding: 6px 6px; text-align: right; }
127127
.table-head { font-weight: bold; text-align: left; padding: 6px 10px; background-color: #eee; }
128+
129+
130+
canvas:hover {
131+
cursor: default;
132+
}
133+
134+
/* When hovering over the legend area */
135+
.chartjs-legend li:hover {
136+
cursor: pointer;
137+
}

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ <h2 style='margin-top:0'>About This Project</h2>
8080
<div class="instructions">
8181
Percent change in U.S. entries from Canada between 2024 and 2025, aggregated by province/territory of departure.
8282
</div>
83-
<div id="provinceTable" style="width:550px;margin-top: 10px;font-size:9pt"></div>
83+
<!-- <div id="provinceTable" style="width:550px;margin-top: 10px;font-size:9pt"></div> -->
84+
<canvas id="provinceChart" width="550" height="420"></canvas>
8485
</div>
8586
<div id="moreinfo">About this project</div>
8687
</div>

js/main.js

Lines changed: 85 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -178,35 +178,98 @@ function getColorClass(value) {
178178
fetch('data/prov.json')
179179
.then(response => response.json())
180180
.then(provData => {
181-
const container = document.getElementById("provinceTable");
182-
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug"];
183-
const monthKeys = ["1", "2", "3", "4", "5", "6", "7","8"];
184-
185-
// Define breakpoints for 5 classes (green = high positive, red = low negative)
186-
181+
const ctx = document.getElementById("provinceChart").getContext("2d");
182+
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug"];
183+
const monthKeys = ["1", "2", "3", "4", "5", "6", "7", "8"];
187184

188185
// Sort province names alphabetically
189186
const sortedProvinces = Object.keys(provData).sort();
190187

191-
// Build HTML table
192-
let html = '<table><thead><tr><th class="table-head" style="width:220px">Province of Departure</th>';
193-
months.forEach(m => html += `<th class="table-head">${m}</th>`);
194-
html += '</tr></thead><tbody>';
195-
196-
sortedProvinces.forEach(prov => {
197-
html += `<tr><td class="table-cell" style="cursor:pointer;color:#32a6c3;text-align:left;" onclick="loadprov(\'${prov}\')");">${prov}</td>`;
198-
monthKeys.forEach(k => {
199-
const val = provData[prov][k];
200-
const cls = getColorClass(val);
201-
html += `<td class="table-cell ${cls}">${val.toFixed(1)}%</td>`;
202-
});
203-
html += '</tr>';
204-
});
188+
// Define a color palette
189+
const colors = [
190+
"#1b9e77", "#d95f02", "#7570b3", "#e7298a",
191+
"#66a61e", "#e6ab02", "#a6761d", "#666666",
192+
"#386cb0", "#f0027f", "#bf5b17", "#999999"
193+
];
205194

206-
html += '</tbody></table>';
207-
container.innerHTML = html;
195+
// Build one dataset per province
196+
const datasets = sortedProvinces.map((prov, i) => ({
197+
label: prov,
198+
data: monthKeys.map(k => provData[prov][k]),
199+
borderColor: colors[i % colors.length],
200+
borderWidth: 2,
201+
fill: false,
202+
tension: 0.3
203+
}));
204+
205+
// Create the line chart
206+
new Chart(ctx, {
207+
type: 'line',
208+
data: {
209+
labels: months,
210+
datasets: datasets
211+
},
212+
options: {
213+
responsive: false,
214+
plugins: {
215+
title: {
216+
display: false
217+
},
218+
legend: {
219+
position: 'bottom',
220+
labels: {
221+
boxWidth: 12,
222+
usePointStyle: true
223+
},
224+
onClick: (evt, legendItem, legend) => {
225+
const prov = legendItem.text; // dataset label = province name
226+
// If you hold Alt/Option (or Ctrl/Cmd), also toggle visibility like default behavior
227+
const toggleLikeDefault = evt.altKey || evt.ctrlKey || evt.metaKey || evt.shiftKey;
228+
229+
// Open the province details overlay
230+
if (typeof loadprov === 'function') {
231+
loadprov(prov); // uses your existing function to build & show the modal:contentReference[oaicite:3]{index=3}
232+
}
233+
234+
// Optional: also toggle visibility when a modifier key is held
235+
if (toggleLikeDefault) {
236+
const ci = legend.chart;
237+
const index = legendItem.datasetIndex;
238+
ci.toggleDataVisibility(index);
239+
ci.update();
240+
}
241+
}
242+
},
243+
tooltip: {
244+
callbacks: {
245+
label: context => `${context.dataset.label}: ${context.parsed.y.toFixed(1)}%`
246+
}
247+
}
248+
},
249+
scales: {
250+
x: {
251+
title: {
252+
display: true,
253+
text: 'Month'
254+
}
255+
},
256+
y: {
257+
title: {
258+
display: true,
259+
text: '% Change'
260+
},
261+
beginAtZero: false
262+
}
263+
},
264+
animation: {
265+
duration: 1000,
266+
easing: 'easeOutQuart'
267+
}
268+
}
269+
});
208270
});
209271

272+
210273
var ports_provs = [];
211274
fetch('data/ports.geojson')
212275
.then(response => response.json())

0 commit comments

Comments
 (0)