最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chartjs backgroundColor for line chart does not appear in Vue app - Stack Overflow

programmeradmin5浏览0评论

I'm trying to render a background (gradient, but simple color also doesn't work) for a line chart using chart.js 3.6.0 in a Vue 3 app. Users can toggle between a bar chart and a line chart (for the same dataset). Interestingly it works for the bar chart:

But the line chart has no gradient:

This is the data configuration:

data: {
    labels: props.labels as string[],
    datasets: [
        {
            data: props.data as number[],
            backgroundColor: getBackgroundColor(), // returns the gradient
            borderColor: getBorderColor(), // returns a color
            fill: true // tried all possible values for this
        }
    ]
}

I have tried to remove everything that might cause an issue, but even the most basic configuration with hard-coded data doesn't work.

The canvas is accessed using a ref:

<template>
    <canvas class="statistics-canvas" ref="canvas" width="100%" height="100%"></canvas>
</template>

<script lang="ts">
    // ...
    setup() {
        const canvas = ref(document.createElement("canvas"));
        let chart: Chart;

        onMounted(() => createChart());

        function createChart() {
            chart = new Chart(canvas.value, {/*...*/});
        }

        return {
            canvas
        };
    }
</script>

Any ideas about what might be causing this? I have also tried coping the configuration from the chartjs docs (.html), but also no luck.

I'm trying to render a background (gradient, but simple color also doesn't work) for a line chart using chart.js 3.6.0 in a Vue 3 app. Users can toggle between a bar chart and a line chart (for the same dataset). Interestingly it works for the bar chart:

But the line chart has no gradient:

This is the data configuration:

data: {
    labels: props.labels as string[],
    datasets: [
        {
            data: props.data as number[],
            backgroundColor: getBackgroundColor(), // returns the gradient
            borderColor: getBorderColor(), // returns a color
            fill: true // tried all possible values for this
        }
    ]
}

I have tried to remove everything that might cause an issue, but even the most basic configuration with hard-coded data doesn't work.

The canvas is accessed using a ref:

<template>
    <canvas class="statistics-canvas" ref="canvas" width="100%" height="100%"></canvas>
</template>

<script lang="ts">
    // ...
    setup() {
        const canvas = ref(document.createElement("canvas"));
        let chart: Chart;

        onMounted(() => createChart());

        function createChart() {
            chart = new Chart(canvas.value, {/*...*/});
        }

        return {
            canvas
        };
    }
</script>

Any ideas about what might be causing this? I have also tried coping the configuration from the chartjs docs (https://www.chartjs/docs/latest/charts/line.html), but also no luck.

Share asked Nov 22, 2021 at 22:12 KugelKugel 8389 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Please check the registrar file for chart js in your project. Add Filler to Chart.register()

import {
  ...,
  Filler,
} from 'chart.js';

Chart.register(
...,
Filler,
);
发布评论

评论列表(0)

  1. 暂无评论