简体   繁体   English

注释不显示在 vue-chartjs 中

[英]Annotation don't show in vue-chartjs

I'm using vue-chartjs .我正在使用vue-chartjs I need annotations in my charts.我需要在图表中添加注释。 I'm imported an annotation plugin我导入了一个注释插件

import chartjsPluginAnnotation from "chartjs-plugin-annotation"

Then add a plugin on mounted然后在mounted上添加一个插件

this.addPlugin(chartjsPluginAnnotation)

Also, I added an annotation object to the options另外,我在选项中添加了一个注释对象

plugins: {
  annotation: {
          drawTime: 'afterDraw',
            annotations: [
              {
                type: "line",
                id: 'BTV',
                mode: "horizontal",
                display: true,
                scaleID: "y-axis-0",
                borderColor: "red",
                value: 17000,
                borderDash: 4,
                label: {
                  content: 'aa',
                  enabled: true,
                  position: "top",
                  xAdjust: 15,
                  backgroundColor: '#4ecca3',
                  fontSize: 10,
                }
              }
          ]
  },
}

It works in all manuals what I find, but doesn't work in my project它适用于我找到的所有手册,但不适用于我的项目

If you are using the "chart.js" v2.9.4(latest), just DOWNGRADE it to v2.9.3 .如果您使用的是“chart.js” v2.9.4(最新),只需将其降级到 v2.9.3 。 Maybe they have some issues on that version.也许他们在那个版本上有一些问题。

Please check my "package.json" below.请在下面检查我的“package.json”。

"dependencies": {
  "chart.js": "^2.9.3",
  "chartjs-plugin-annotation": "^0.5.7",
  "vue": "^2.6.11",
  "vue-chartjs": "^3.5.1" 
}

It works properly for me.它适合我。

One problem I see is that you've defined annotation under plugins whereas it should be defined directly under options .我看到的一个问题是您已经在plugins下定义了annotation ,而它应该直接options下定义。 (I know this is confusing, because some of the chartjs-annotation-plugin documentation still shows an example with annotation as a property of plugins rather than options .) (我知道这很令人困惑,因为一些 chartjs-annotation-plugin 文档仍然显示了一个示例,其中annotation作为plugins的属性而不是options 。)

There is, however, another issue which does appear to stem from a change that was made in Chart.js 2.9.4.然而,还有另一个问题似乎源于 Chart.js 2.9.4 中所做的更改。 The method of cloning an object was modified , such that it now utilizes Object.create() , which copies the properties from the source object into the target object's prototype . 克隆对象的方法已修改,现在使用Object.create() ,它将属性从源对象复制到目标对象的原型中 The problem, in the context of a Vue app, is that the options that you pass to the renderChart() method is very likely a Vue observer (eg, a prop or a member of data ), which means that its properties are all set (by Vue, under the hood) using Object.defineProperty() .问题是,在 Vue 应用程序的上下文中,您传递给renderChart()方法的options很可能是 Vue 观察者(例如,prop 或data的成员),这意味着它的属性都已设置(由 Vue,在Object.defineProperty() )使用Object.defineProperty() Here's why that is important:这就是为什么这很重要:

Setting a property to an object creates an own property.为对象设置属性会创建自己的属性。 The only exception to the getting and setting behavior rules is when there is an inherited property with a getter or a setter.获取和设置行为规则的唯一例外是当存在带有 getter 或 setter 的继承属性时。

(From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain ) (来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

The reason this is a problem is that, when Chart.js initializes a chart, it creates the options by recursively merging your configuration options with a bunch of global defaults;这是一个问题的原因是,当 Chart.js 初始化图表时,它通过递归地将配置选项与一堆全局默认值合并来创建选项; part of this merging behavior involves the aforementioned cloning if the property is a non-standard option, which will render your annotation object with its properties copied to its prototype, but with no properties of its own.如果属性是非标准选项,则此合并行为的一部分涉及上述克隆,这将呈现您的annotation对象,其属性复制到其原型,但没有自己的属性。 Therefore, when the annotation plugin is initialized, it finds an annotation object, but one without any properties of its own, and when the annotation plugin initializes its configuration, it merges your annotation object with its defaults - which includes an empty annotations array.因此,当注解插件初始化时,它会找到一个注解对象,但它没有自己的任何属性,并且当注解插件初始化配置时,它将您的annotation对象与其默认值合并 - 其中包括一个空的annotations数组。

It seems to me that downgrading Chart.js to 2.9.3 should work for you, provided that you also move your annotation property from plugins to directly under options .在我看来,将 Chart.js 降级到 2.9.3 应该对您有用,前提是您还将annotation属性从plugins移动到options正下方。 An alternative - if you want to continue using the latest version of Chart.js - is to ensure that the options you pass to the renderChart() method are non-reactive (that is, not a Vue observer).另一种选择——如果你想继续使用最新版本的 Chart.js——是确保你传递给renderChart()方法的选项是非反应性的(即,不是 Vue 观察者)。 One way to accomplish that would be to copy your reactive annotation object's properties into a POJO (Plain Old Javascript Object), eg, by using Object.assign() :实现这一点的一种方法是将反应式annotation对象的属性复制到 POJO(Plain Old Javascript Object)中,例如,通过使用Object.assign()

this.renderChart(this.chartData, { 
  ...this.options, 
  annotation: Object.assign({}, this.options.annotation) 
})

I have filed an issue on the Chart.js GitHub: https://github.com/chartjs/Chart.js/issues/8382我在 Chart.js GitHub 上提交了一个问题: https : //github.com/chartjs/Chart.js/issues/8382

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM