我用的是‘chartjs-plugin-data Labels v2.1.0’插件,要创建幸运轮,你需要在标签栏中用img替换文本,该怎么做呢?

在常规JS中,标签行的值为‘2’、‘3’,依此类推,然后我想推送img 我怎么才能解决这个问题呢?我很快需要你的帮助

const spinWheel = document.getElementById("spinWheel");
const spinBtn = document.getElementById("spin_btn");
const text = document.getElementById("text");
const spinValues = [
    { minDegree: 31, maxDegree: 60, value: "Ноутбук игровой MSI Katana GF76 12UD-263RU" },
    { minDegree: 0, maxDegree: 30, value: "Lush3 + Ridge x 1 + Микрофон HyperX Quadcast S" },
    { minDegree: 61, maxDegree: 90, value: "Беззеркальная камера Canon EOS R50 Body" },
    { minDegree: 331, maxDegree: 360, value: "Lovense Webcam + Domi 2" },
    { minDegree: 301, maxDegree: 330, value: '"Капуста"' },
    { minDegree: 271, maxDegree: 300, value: '"Картошка"' },
    { minDegree: 211, maxDegree: 240, value: "Apple iPhone 15" },
    { minDegree: 241, maxDegree: 270, value: '"Морковь"' },
    { minDegree: 181, maxDegree: 210, value: "Cougar Throne – кресло для геймеров" },
    {
        minDegree: 151,
        maxDegree: 180,
        value: "Сертификат ВБ",
    },
];

const size = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10];
var spinColors = ["#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032"];

let spinChart = new Chart(spinWheel, {
    plugins: [ChartDataLabels],
    type: "pie",
    data: {
        labels: ['<img src="/img/11.png">', "2", "3", "4", "5", "6", "7", "8", "9", "10"],
        datasets: [
            {
                backgroundColor: spinColors,
                data: size,
            },
        ],
    },
    options: {
        responsive: true,
        animation: { duration: 0 },
        plugins: {
            tooltip: false,
            legend: {
                display: false,
            },
            datalabels: {
                rotation: 90,
                color: "#ffffff",
                formatter: (_, context) => context.chart.data.labels[context.dataIndex],
                font: { size: 24 },
            },
        },
    },
});
const generateValue = (angleValue) => {
    for (let i of spinValues) {
        if (angleValue >= i.minDegree && angleValue <= i.maxDegree) {
            text.innerHTML = `<p>Поздравляем, вы выиграли: <br> ${i.value} ! </p>`;
            spinBtn.disabled = false;
            break;
        }
    }
};
let count = 0;
let resultValue = 101;
spinBtn.addEventListener("click", () => {
    // spinBtn.disabled = false;
    // text.innerHTML = `<p>Желаю Удачи!</p>`;
    let randomDegree = Math.floor(Math.random() * (355 - 0 + 1) + 0);
    let rotationInterval = window.setInterval(() => {
        spinChart.options.rotation = spinChart.options.rotation + resultValue;
        spinChart.update();
        if (spinChart.options.rotation >= 360) {
            count += 1;
            resultValue -= 5;
            spinChart.options.rotation = 0;
        } else if (count > 15 && spinChart.options.rotation == randomDegree) {
            generateValue(randomDegree);
            clearInterval(rotationInterval);
            count = 0;
            resultValue = 101;
        }
    }, 10);
});
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.1.0/chartjs-plugin-datalabels.min.js"></script>

<div>
    <canvas id="spinWheel"></canvas>
</div>
<div id="spin_btn">spin_btn</div>
<div id="text">text</div>

推荐答案

似乎chartjs-plugin-datalabels还没有准备好处理图像.

如果你没有任何具体的理由使用这个插件,你可以使用chartjs-plugin-labels,它似乎更适合你目前的需求.

这是你的旋转轮,所有的标签都有相同的图像(chartjs标志).如果您想要更改任何图像或图像的大小,您可以更改插件选项.

const spinWheel = document.getElementById("spinWheel");
const spinBtn = document.getElementById("spin_btn");
const text = document.getElementById("text");
const spinValues = [
    { minDegree: 31, maxDegree: 60, value: "Ноутбук игровой MSI Katana GF76 12UD-263RU" },
    { minDegree: 0, maxDegree: 30, value: "Lush3 + Ridge x 1 + Микрофон HyperX Quadcast S" },
    { minDegree: 61, maxDegree: 90, value: "Беззеркальная камера Canon EOS R50 Body" },
    { minDegree: 331, maxDegree: 360, value: "Lovense Webcam + Domi 2" },
    { minDegree: 301, maxDegree: 330, value: '"Капуста"' },
    { minDegree: 271, maxDegree: 300, value: '"Картошка"' },
    { minDegree: 211, maxDegree: 240, value: "Apple iPhone 15" },
    { minDegree: 241, maxDegree: 270, value: '"Морковь"' },
    { minDegree: 181, maxDegree: 210, value: "Cougar Throne – кресло для геймеров" },
    { minDegree: 151,maxDegree: 180,value: "Сертификат ВБ" },
];

const size = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10];
var spinColors = ["#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032", "#303032"];

let spinChart = new Chart(spinWheel, {
    type: "pie",
    data: {
        labels: ['1', "2", "3", "4", "5", "6", "7", "8", "9", "10"],
        datasets: [
            {
                label: 'My first dataset',
                backgroundColor: spinColors,
                data: size,
            },
        ],
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        animation: { duration: 0 },
        plugins: {
            tooltip: false,
            legend: {
                display: false,
            },
            labels: {
                render: 'image',
                images: [
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                    { src: 'https://www.chartjs.org/img/chartjs-logo.svg', width: 32, height: 32 },
                ]
            },
        },
    },
});
const generateValue = (angleValue) => {
    for (let i of spinValues) {
        if (angleValue >= i.minDegree && angleValue <= i.maxDegree) {
            text.innerHTML = `<p>Поздравляем, вы выиграли: <br> ${i.value} ! </p>`;
            spinBtn.disabled = false;
            break;
        }
    }
};
let count = 0;
let resultValue = 101;
spinBtn.addEventListener("click", () => {
    // spinBtn.disabled = false;
    // text.innerHTML = `<p>Желаю Удачи!</p>`;
    let randomDegree = Math.floor(Math.random() * (355 - 0 + 1) + 0);
    let rotationInterval = window.setInterval(() => {
        spinChart.options.rotation = spinChart.options.rotation + resultValue;
        spinChart.update();
        if (spinChart.options.rotation >= 360) {
            count += 1;
            resultValue -= 5;
            spinChart.options.rotation = 0;
        } else if (count > 15 && spinChart.options.rotation == randomDegree) {
            generateValue(randomDegree);
            clearInterval(rotationInterval);
            count = 0;
            resultValue = 101;
        }
    }, 10);
});
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://unpkg.com/chart.js-plugin-labels-dv/dist/chartjs-plugin-labels.min.js"></script>

<div>
    <canvas id="spinWheel"></canvas>
</div>
<div id="spin_btn">spin_btn</div>
<div id="text">text</div>

Javascript相关问答推荐

将数据从strapi提取到next.js,但响应延迟API URL

Vega中的模运算符

Klaro与Angular的集成

如何在Angular中插入动态组件

Redux查询多个数据库Api reducerPath&

D3 Scale在v6中工作,但在v7中不工作

如何禁用附加图标点击的v—自动完成事件

如何使覆盖div与可水平滚动的父div相关?

如何在模块层面提供服务?

如何在Vue 3中创建自定义 Select 组件,并将选项作为HTML而不是props 传递?

PDF工具包阿拉伯字体的反转数字

在Reaction中的handleSubmit按钮内,useSelector值仍然为空

同一类的所有div';S的模式窗口

自定义图表工具提示以仅显示Y值

当标题被点击时,如何使内容出现在另一个div上?

FireBase云函数-函数外部的ENV变量

如何使用puppeteer操作所有选项

JavaScript:多个图像错误处理程序

将Windows XP转换为原始数据以在html前端中显示

错误400:当我试图在React中使用put方法时,该字段是必需的