Using chart.js with importmaps in rail 7
Asked Answered
T

3

5

I am trying to integrate chartjs with importmaps in rails 7 but its not working

I have tried with following procedure

     bin/importmap pin "chart.js@^3.8.0" --download

here is my application.js

    import 'chart.js'

I am getting following error in chrome console and I believe it has something to do with ES modules

Get http://localhost:5000/_/e09df68b.js` net::ERR_ABORTED 404 (Not Found)    chart.js-67657cw24cb.js:1 

Am I missing something or its not possible yet to integrate chart.js with rails 7

Thus answered 4/6, 2022 at 13:21 Comment(0)
K
4

I was able to solve this by fetching the library from the CDN rather than downloading it. To do this, I first unpinned the downloaded version:

bin/importmap unpin chart.js --download

and then I repinned it without downloading:

bin/importmap pin chart.js

And now it works!

Keble answered 23/7, 2022 at 17:28 Comment(0)
P
2

As of August 2023, the setup for Chartkick (which uses chart.js) with Importmap is described in the Chartkick documentation, which worked for me.

Importmap
In config/importmap.rb, add:

pin "chartkick", to: "chartkick.js"
pin "Chart.bundle", to: "Chart.bundle.js"

And in app/javascript/application.js, add:

import "chartkick"
import "Chart.bundle"
Platter answered 13/8, 2023 at 12:44 Comment(1)
Man, thank you for recommending that. I was trying to integrate chart.js with importmap for like the second day and it's a nightmare. Besides the fact importmap is barely documented (the best documentation could be find here https://mcmap.net/q/362873/-how-to-add-custom-js-file-to-new-rails-7-project ), chart.js never worked for me through it. Chartkick is a lifesaver. I managed to finish what I wanted in 2 mins rather than days.Slub
B
1

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)
            }
          ]
        }
      }
    );
  }
}
Bump answered 19/2 at 11:2 Comment(2)
You are a saint. Thank you for sharing this. Wanted to avoid using Chartkick. I can confirm the instructions work!Liatrice
@Pieter Vogelaar Can you please guide chart.js 4.4 path which can be added in application.js file. Rails 7 app and there is package.json file where chart library is being included. When upgrading from version 2 to 4 there path is also changed. This path of package is been removed as per doc - "require chart.js/dist/Chart" Facing Uncaught ReferenceError: Chart is not definedThump

© 2022 - 2024 — McMap. All rights reserved.