在Rails7中使用Chart.js和ImportMap

wlzqhblo  于 2023-01-26  发布在  Chart.js
关注(0)|答案(1)|浏览(360)

我试过:

  1. ./bin/importmap pin chart.js

它将此添加到config/importmap.rb中

  1. pin "chart.js", to: "https://ga.jspm.io/npm:chart.js@3.9.1/dist/chart.mjs"

在application.js中,我添加了:

  1. import 'chart.js'

当我尝试创建一个新的图表时,我得到"图表未定义"。
如果我尝试

  1. import Chart from 'chart.js'

我得到"请求的模块'chart.js'不提供名为'default'的导出"

  1. import Chart from 'chart.js/auto'

显示"无法解析模块说明符'chart.js/auto'"

fsi0uk1n

fsi0uk1n1#

我有同样的斗争这个周末,并不断发生在这篇文章,所以张贴我的解决方案在这里。
有几件事值得注意:

  1. chart.js似乎有一些硬编码的附件文件没有在CDN中注册为依赖项。例如,我的文件在 * c1719c72.js * 和 * chunks/helpers. segment. js * 上保持404ing。因此,如果在运行bin/importmap pin时不使用 *--download * 标记,效果会更好。Using chart.js with importmaps in rail 7.
    1.从chart.js导入Chart需要一些技巧:Chart.js - Where do I find which components should be registered?。特别是下面barchart_controller. js的第3 - 4行。

下面是我在一个相当普通的带有导入Map的Rails7构建中使用的文件内容的摘要:

config/importmap.rb-注:运行bin/importmap pin chart.js应该引入一个依赖项(@kurkle/color),一定要跳过--下载,否则您将陷入痛苦的境地。

  1. # Pin npm packages by running ./bin/importmap
  2. pin "application", preload: true
  3. pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
  4. pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
  5. pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
  6. pin_all_from "app/javascript/controllers", under: "controllers"
  7. pin "chart.js", to: "https://ga.jspm.io/npm:chart.js@4.2.0/dist/chart.js"
  8. pin "@kurkle/color", to: "https://ga.jspm.io/npm:@kurkle/color@0.3.2/dist/color.esm.js"

app/javascript/application.js

  1. // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
  2. import "@hotwired/turbo-rails"
  3. import "controllers"
  4. import "chart.js"

app/javascript/controllers/barchart_controller.js

  1. import {Controller} from "@hotwired/stimulus"
  2. import { Chart, registerables } from "chart.js";
  3. Chart.register(...registerables);
  4. export default class extends Controller {
  5. static targets = ['myChart'];
  6. canvasContext() {
  7. return this.myChartTarget.getContext('2d');
  8. }
  9. connect() {
  10. new Chart(this.canvasContext(), {
  11. type: 'bar',
  12. data: {
  13. labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
  14. datasets: [{
  15. label: '# of Votes',
  16. data: [12, 19, 3, 5, 2, 3],
  17. backgroundColor: [
  18. 'rgba(255, 99, 132, 0.2)',
  19. 'rgba(54, 162, 235, 0.2)',
  20. 'rgba(255, 206, 86, 0.2)',
  21. 'rgba(75, 192, 192, 0.2)',
  22. 'rgba(153, 102, 255, 0.2)',
  23. 'rgba(255, 159, 64, 0.2)'
  24. ],
  25. borderColor: [
  26. 'rgba(255, 99, 132, 1)',
  27. 'rgba(54, 162, 235, 1)',
  28. 'rgba(255, 206, 86, 1)',
  29. 'rgba(75, 192, 192, 1)',
  30. 'rgba(153, 102, 255, 1)',
  31. 'rgba(255, 159, 64, 1)'
  32. ],
  33. borderWidth: 1
  34. }]
  35. },
  36. options: {
  37. scales: {
  38. y: {
  39. beginAtZero: true
  40. }
  41. }
  42. }
  43. });
  44. }
  45. }

我的模型的app/views/widgets/show.html.erb中div的内容

  1. <div data-controller="barchart">
  2. <canvas id="bar-chart" data-barchart-target="myChart" width="800" height="450"></canvas>
  3. </div>

不要忘记重新构建资产(bin/rails资产:precompile)。

展开查看全部

相关问题