javascript 如何在hotwired/stimulus控制器上使用chartjs-plugin-annotation

mzsu5hc0  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(89)

在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'
         } 
       }
     }
   }
 } 
}

但是,注解框打印时不会出现任何错误。
我错过什么了吗?

ktca8awb

ktca8awb1#

我可能错了,但我认为您的注解级别太高,需要给予一个ID,例如下面代码中的 box1

options: {
    plugins: {
      annotation: {
        annotations: {
          //Right there
          box1: {
            type: 'box',
            xMin: 1,
            xMax: 2,
            yMin: 50,
            yMax: 70,
            backgroundColor: 'rgba(255, 99, 132, 0.25)'
          }
        }
      }
    }
  }

You can find the documentation here

相关问题