使用Rail 7中的ImportMaps的Chart.js

发布于 2025-02-04 08:20:00 字数 453 浏览 6 评论 0原文

正在尝试将ChartJS与Rails 7中的

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

ImportMaps

    import 'chart.js'

集成对于ES模块,

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

我缺少某些内容,或者还无法整合图表。JS与Rails 7

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妖妓 2025-02-11 08:20:00

我能够通过从CDN获取图书馆而不是下载它来解决此问题。为此,我首先取消了下载的版本:

bin/importmap unpin chart.js --download

然后我将其重新固定而不下载:

bin/importmap pin chart.js

现在它可以使用!

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!

梦里泪两行 2025-02-11 08:20:00

截至2023年8月,在

importMap
在config/importmap.rb中,add:

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

以及在app/javascript/application.js中,添加:

import "chartkick"
import "Chart.bundle"

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"
书信已泛黄 2025-02-11 08:20:00

手动下载和固定来自JSDELIVR.com的软件包为我工作。

下载curl中的供应商/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

手动将图表中的导入语句更改为:js.js文件中:

import {Color as t} from "kurklecolor"

手动将销钉添加到您的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

现在,您只需遵循官方Chart.js文档即可。下面的一个工作示例。

添加到您的视图中:

<canvas id="acquisitions"></canvas>

添加到刺激控制器中:

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)
            }
          ]
        }
      }
    );
  }
}

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)
            }
          ]
        }
      }
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文