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

0

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

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"

1 Comment

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 stackoverflow.com/a/72855705/3604050 ), 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.
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)
            }
          ]
        }
      }
    );
  }
}

2 Comments

You are a saint. Thank you for sharing this. Wanted to avoid using Chartkick. I can confirm the instructions work!
@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 defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.