在尝试渲染一张新图表时渲染一张图表后 chart.js 给出 "Check that a complete date adapter is provided."
After rendering one chart when trying to render a new one chart.js gives "Check that a complete date adapter is provided."
我制作了一个脚本,它呈现了一个包含多条线和日期的图表,我可以 运行 这个脚本一次完全没问题,但是当我第二次尝试 运行 它时,它给出了一个错误: “错误:未实现此方法:检查是否提供了完整的日期适配器。”。我找不到适合我情况的任何内容。
[enter image description here][1]const {
MessageEmbed,
MessageAttachment
} = require("discord.js");
const {
ChartJSNodeCanvas
} = require("chartjs-node-canvas");
const canvas = require('canvas'); // important
var groupArray = require('group-array');
const fs = require('fs')
const axios = require('axios');
require('chartjs-adapter-moment')
const generateCanva = async (labels, datas, names, interaction) => {
const renderer = new ChartJSNodeCanvas({
width: 800,
height: 300,
backgroundColour: "white"
});
parsedLabels = []
if (Array.isArray(labels[0])) {
for (alabel in labels) {
parsedLabels = labels[alabel].reduce(
(acc, item) => {
return acc.includes(item) ? acc : [...acc, item]
},
[...parsedLabels]
)
}
} else {
parsedLabels = labels
}
parsedLabels = parsedLabels.sort(function (a, b) {
return new Date(b) - new Date(a);
}).reverse();
parsedLabelsDate = []
for (x in parsedLabels) {
parsedLabelsDate.push(new Date(parsedLabels[x]))
}
chartObject = {
type: "line", // Show a bar chart
backgroundColor: "rgb(255,255,255)",
options: {
scales: {
x: {
type: 'time',
}
}
},
data: {
labels: parsedLabelsDate,
datasets: [],
},
}
colors = ['rgb(240,128,128)', 'rgb(240,230,140)', 'rgb(152,251,152)', 'rgb(0,206,209)', 'rgb(135,206,250)', 'rgb(238,130,238)']
if (Array.isArray(labels[0])) {
for (var i in labels) {
parcedDatas = []
for (x in datas[i]) {
parcedDatas.push({
x: new Date(labels[i][x]),
y: datas[i][x]
})
}
dataset = {
label: names[i],
data: parcedDatas,
fill: false,
borderColor: colors[i],
tension: 0.1
}
console.log(dataset)
chartObject.data.datasets.push(dataset)
}
} else {
dataset = {
label: interaction.author.username,
data: datas,
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}
chartObject.data.datasets.push(dataset)
}
const image = await renderer.renderToBuffer(
// Build your graph passing option you want
chartObject
);
return new MessageAttachment(image, "graph.png");
};
通过移动解决了问题:
const renderer = new ChartJSNodeCanvas({
width: 800,
height: 300,
backgroundColour: "white"
});
在函数之外。
我制作了一个脚本,它呈现了一个包含多条线和日期的图表,我可以 运行 这个脚本一次完全没问题,但是当我第二次尝试 运行 它时,它给出了一个错误: “错误:未实现此方法:检查是否提供了完整的日期适配器。”。我找不到适合我情况的任何内容。
[enter image description here][1]const {
MessageEmbed,
MessageAttachment
} = require("discord.js");
const {
ChartJSNodeCanvas
} = require("chartjs-node-canvas");
const canvas = require('canvas'); // important
var groupArray = require('group-array');
const fs = require('fs')
const axios = require('axios');
require('chartjs-adapter-moment')
const generateCanva = async (labels, datas, names, interaction) => {
const renderer = new ChartJSNodeCanvas({
width: 800,
height: 300,
backgroundColour: "white"
});
parsedLabels = []
if (Array.isArray(labels[0])) {
for (alabel in labels) {
parsedLabels = labels[alabel].reduce(
(acc, item) => {
return acc.includes(item) ? acc : [...acc, item]
},
[...parsedLabels]
)
}
} else {
parsedLabels = labels
}
parsedLabels = parsedLabels.sort(function (a, b) {
return new Date(b) - new Date(a);
}).reverse();
parsedLabelsDate = []
for (x in parsedLabels) {
parsedLabelsDate.push(new Date(parsedLabels[x]))
}
chartObject = {
type: "line", // Show a bar chart
backgroundColor: "rgb(255,255,255)",
options: {
scales: {
x: {
type: 'time',
}
}
},
data: {
labels: parsedLabelsDate,
datasets: [],
},
}
colors = ['rgb(240,128,128)', 'rgb(240,230,140)', 'rgb(152,251,152)', 'rgb(0,206,209)', 'rgb(135,206,250)', 'rgb(238,130,238)']
if (Array.isArray(labels[0])) {
for (var i in labels) {
parcedDatas = []
for (x in datas[i]) {
parcedDatas.push({
x: new Date(labels[i][x]),
y: datas[i][x]
})
}
dataset = {
label: names[i],
data: parcedDatas,
fill: false,
borderColor: colors[i],
tension: 0.1
}
console.log(dataset)
chartObject.data.datasets.push(dataset)
}
} else {
dataset = {
label: interaction.author.username,
data: datas,
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}
chartObject.data.datasets.push(dataset)
}
const image = await renderer.renderToBuffer(
// Build your graph passing option you want
chartObject
);
return new MessageAttachment(image, "graph.png");
};
通过移动解决了问题:
const renderer = new ChartJSNodeCanvas({
width: 800,
height: 300,
backgroundColour: "white"
});
在函数之外。