ChartJS 如何改变工具提示的背景颜色?

ruoxqz4g  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(203)

我已经在chartOptions中添加了tooltips.backgroundColor,但仍然不起作用,所以有人可以帮助我吗?
这是我的代码

<template>
  <Doughnut
    :chart-options="chartOptions"
    :chart-data="chartData"
    :chart-id="'doughnut-chart'"
    :styles="styles"
    :width="width"
    :height="height"
  />
</template>

<script lang="ts">
import { defineComponent, type PropType } from "vue";
import TypographyComponent from "@/core/ui/components/typography/Typography.component.vue";
import { Doughnut } from "vue-chartjs";
import {
  Chart as ChartJS,
  Title,
  Tooltip,
  Legend,
  ArcElement,
  CategoryScale,
  type Plugin,
} from "chart.js";

ChartJS.register(Title, Tooltip, Legend, ArcElement, CategoryScale);

export default defineComponent({
  name: "ProgressChartComponent",
  components: { Doughnut, TypographyComponent },
  props: {
    width: {
      type: Number,
      default: 400,
    },
    height: {
      type: Number,
      default: 400,
    },
    styles: {
      type: Object as PropType<Partial<CSSStyleDeclaration>>,
      default: () => {},
    },
    chartData: {
      type: Object,
      required: false,
      default: () => {},
    },
  },
  setup() {
    const chartOptions = {
      responsive: true,
      maintainAspectRatio: false,
      cutout: "64%",
      tooltips: {
        enabled: false,
        backgroundColor: "#227799",
      },
    };
    return {
      chartOptions,
    };
  },
});

</script>

...
wnrlj8wa

wnrlj8wa1#

假设您使用的是Chart.js v3,请注意工具提示是在命名空间options.plugins.tooltip中定义的,而不是像代码中那样在options.tooltips中定义的。因此,chartOptions需要进行如下更改:

const chartOptions = {
  responsive: true,
  maintainAspectRatio: false,
  cutout: "64%",
  plugins: {
    tooltip: {
      backgroundColor: "#227799"
    }
  }
};

有关详细信息,请参阅Chart.js文档中的“工具提示配置”。

相关问题