ChartJS Vue2正在尝试设置图表标题

pkln4tw6  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(180)

不像这样工作:

<template>
  <Bar
    :chart-options="chartOptions"
    :chart-data="$attrs['chart-data'] || chartData"
    :chart-id="chartId"
    :dataset-id-key="datasetIdKey"
    :plugins="plugins"
    :css-classes="cssClasses"
    :styles="styles"
    :width="width"
    :height="height"
  />
</template>

<script>
import { Bar } from 'vue-chartjs/legacy'
import 'chart.js/auto'

export default {
  name: 'BarChart',
  components: { Bar },
  props: {
    chartId: {
      type: String,
      default: 'bar-chart',
    },
    datasetIdKey: {
      type: String,
      default: 'label',
    },
    width: {
      type: Number,
      default: 400,
    },
    height: {
      type: Number,
      default: 400,
    },
    cssClasses: {
      default: '',
      type: String,
    },
    styles: {
      type: Object,
      default: () => {},
    },
    plugins: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      chartData: {},
      chartOptions: {
        responsive: true,
        title: {
          display: true,
          text: 'Custom Chart Title',
        },
      },
    }
  },
}
</script>

尝试使用以下内容进行编辑:https://codesandbox.io/s/exciting-ully-2wst65?file=/src/components/Bar.vue,但没有任何工作。
所有信息收集自:https://vue-chartjs.org/api/如果导入auto,则不需要注册组件。
也许一些想法?

nbewdwxp

nbewdwxp1#

您在v3中使用v2语法,工具提示、标题、图例和抽取等内部插件已从options名称空间移至options.plugins名称空间。
所以你需要把title选项嵌套在options对象内的一个plugins对象中。

rhfm7lfc

rhfm7lfc2#

对于像我这样正在寻找使用vue-chartjs V4的解决方案的人来说,要显示Title,您需要传递插件对象,例如:

plugins: {
  type: Array,
  default: () => [Title]
}

然后传递到chartOptions中,支持title配置,例如:

chartOptions: {
  responsive: true,
  maintainAspectRatio: false,
  plugins: {
    title: {
      display: true,
      text: "Chart Title",
    },
  },
}

sandBox full example

相关问题