内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - Annotation don't show in vue-chartjs - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Annotation don't show in vue-chartjs - Stack Overflow

programmeradmin0浏览0评论

I'm using 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

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

I'm using 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

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

Share Improve this question edited Jan 18, 2021 at 7:29 Martin Brisiak 4,06112 gold badges39 silver badges51 bronze badges asked Nov 17, 2020 at 15:42 Emil RotatewEmil Rotatew 195 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

If you are using the "chart.js" v2.9.4(latest), just DOWNGRADE it to v2.9.3 . Maybe they have some issues on that version.

Please check my "package.json" below.

"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. (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.)

There is, however, another issue which does appear to stem from a change that was made in 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. 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 (e.g., a prop or a member of data), which means that its properties are all set (by Vue, under the hood) using 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.

(From https://developer.mozilla/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; 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. 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.

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. 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). One way to acplish that would be to copy your reactive annotation object's properties into a POJO (Plain Old Javascript Object), e.g., by using 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./chartjs/Chart.js/issues/8382

发布评论

评论列表(0)

  1. 暂无评论