Manually downloading and pinning the packages from jsdelivr.com worked for me.
Download with curl into vendor/javascript:
curl https://cdn.jsdelivr.net/npm/[email protected]/+esm -o vendor/javascript/chart.js.js
curl https://cdn.jsdelivr.net/npm/@kurkle/[email protected]/+esm -o vendor/javascript/kurklecolor.js
Manually change the import statement in the chart.js.js file to:
import {Color as t} from "kurklecolor"
Manually add pins to your config/importmap.rb
:
pin 'chart.js' # https://cdn.jsdelivr.net/npm/[email protected]/+esm
pin 'kurklecolor' # https://cdn.jsdelivr.net/npm/@kurkle/[email protected]/+esm
Now you can just follow the official chart.js documentation. A working example below.
Add to your view:
<canvas id="acquisitions"></canvas>
Add to your Stimulus controller:
import { Controller } from '@hotwired/stimulus';
import { Chart, Colors, BarController, CategoryScale, LinearScale, BarElement, Legend } from 'chart.js'
export default class extends Controller {
connect() {
Chart.register(Colors, BarController, BarElement, CategoryScale, LinearScale, Legend);
const data = [
{ year: 2010, count: 10 },
{ year: 2011, count: 20 },
{ year: 2012, count: 15 },
{ year: 2013, count: 25 },
{ year: 2014, count: 22 },
{ year: 2015, count: 30 },
{ year: 2016, count: 28 },
];
new Chart(
document.getElementById('acquisitions'),
{
type: 'bar',
data: {
labels: data.map(row => row.year),
datasets: [
{
label: 'Acquisitions by year',
data: data.map(row => row.count)
}
]
}
}
);
}
}