So, while trying to load Chart.js into Sugarcube 2 I ended up getting the following error message.
A fatal error has occurred. Aborting.
Error: Chart is not defined.
Funnily enough, despite the error, the sample chart I was using for testing purposes still loaded, which meant that it’s probably something I did wrong. Here’s how I have been doing this. I’m going to guess that it’s because the instructions say to use the setup.JSLoaded to check if the library has finished loading. But I don’t know how to have the code run only after everything is loaded.
I first loaded the Chart.js using HiEv’s loading External Script’s
setup.JSLoaded = false;
var lockID = LoadScreen.lock(); // Lock loading screen
importScripts("https://cdn.jsdelivr.net/npm/chart.js") // Your JavaScript files go here
.then(function() {
setup.JSLoaded = true;
// Reload current passage since imported scripts can function now.
Engine.play(passage(), true);
LoadScreen.unlock(lockID); // Unlock loading screen
}).catch(function(error) {
alert(error);
}
);
Then I have the following Chart being loaded on the Start Passage with an <<include [[Passage]]>> Macro. I have used the instructions from the following link to get it working in the first place.
The actual chart is of the example ones in Chart.js Documentation page.
<div>
<canvas id="myChart"></canvas>
</div>
<<script>>
$(document).one(":passageend", function (event) {
var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
});
<</script>>
So, I can sort of tell where I went wrong, but don’t really know how to fix it. I’d be grateful if anyone can help.