在Rails 7中,我试图创建一个Stimulus控制器来呈现chart.js图表。我的代码看起来像
# config/importmap.rb
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "chart.js", to: "https://ga.jspm.io/npm:[email protected]/dist/chart.js"
pin "@kurkle/color", to: "https://ga.jspm.io/npm:@kurkle/[email protected]/dist/color.esm.js"
pin "chartjs-plugin-annotation", to: "https://ga.jspm.io/npm:[email protected]/dist/chartjs-plugin-annotation.esm.js"
pin "chart.js/helpers", to: "https://ga.jspm.io/npm:[email protected]/helpers/helpers.js"
pin "chartjs-adapter-date-fns", to: "https://ga.jspm.io/npm:[email protected]/dist/chartjs-adapter-date-fns.esm.js"
pin "@babel/runtime/helpers/esm/assertThisInitialized", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/assertThisInitialized.js"
pin "@babel/runtime/helpers/esm/classCallCheck", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/classCallCheck.js"
pin "@babel/runtime/helpers/esm/createClass", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/createClass.js"
pin "@babel/runtime/helpers/esm/createForOfIteratorHelper", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/createForOfIteratorHelper.js"
pin "@babel/runtime/helpers/esm/createSuper", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/createSuper.js"
pin "@babel/runtime/helpers/esm/defineProperty", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/defineProperty.js"
pin "@babel/runtime/helpers/esm/inherits", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/inherits.js"
pin "@babel/runtime/helpers/esm/typeof", to: "https://ga.jspm.io/npm:@babel/[email protected]/helpers/esm/typeof.js"
pin "date-fns", to: "https://ga.jspm.io/npm:[email protected]/esm/index.js"
# application/javascript/controllers/chart_controller.js
import { Controller } from "@hotwired/stimulus"
import { Chart, registerables } from "chart.js"
Chart.register(...registerables)
import annotationPlugin from "chartjs-plugin-annotation"
Chart.register(annotationPlugin)
import "chartjs-adapter-date-fns"
export default class extends Controller {
static targets = [ "canvas" ]
connect() {
this.chart = new Chart(this.canvasTarget.getContext("2d"), {
type: "line",
data: this.chartData,
options: {
...
annotation: {
annotations: {
type: 'box',
ScaleID: 'y',
borderWidth: 0,
drawTime: 'beforeDraw',
xMin: '2023-09-03 10:35:17',
xMax: '2023-09-18 10:35:17',
yMin: 40,
yMax: 70,
backgroundColor: '#ecf0f1'
}
}
}
}
}
}
但是,注解框打印时不会出现任何错误。
我错过什么了吗?
1条答案
按热度按时间ktca8awb1#
我可能错了,但我认为您的注解级别太高,需要给予一个ID,例如下面代码中的 box1
You can find the documentation here