如何修复Chart JS图表上列值显示在错误位置的问题?

huangapple go评论51阅读模式
英文:

How can I fix the issue where the column values on my Chart JS chart are displayed in the wrong place?

问题

我在NodeJS上编写了一个Telegram机器人,可以根据请求显示销售柱状图。

第一个图显示了正确的列值排列。第二个图是不正确的。只有在处理第一个查询时才会显示正确的图表,并且所有重复的查询都会绘制错误的图表(列值显示不正确)。

const { ChartJSNodeCanvas } = require('chartjs-node-canvas');

const width = 600; // 图表宽度
const height = 400; // 图表高度

const chartCallback = (ChartJS) => {
    ChartJS.defaults.font.size = 14; // 设置默认字体大小
};

const canvas = new ChartJSNodeCanvas({ width, height, chartCallback });

const chartData = {
    labels: ['План', 'Факт', 'Прогноз'],
    datasets: [
        {
            label: 'Сумма',
            data: [salesTarget, monthSum, monthForcastSum],
            backgroundColor: 'rgba(54, 162, 235, 0.5)', // 柱状图背景颜色
            borderColor: 'rgba(54, 162, 235, 1)', // 柱状图边框颜色
            borderWidth: 1, // 柱状图边框宽度
        },
    ],
};

const chartOptions = {
    responsive: false, // 禁用自动响应
    scales: {
        y: {
            beginAtZero: true, // 从零开始Y轴
            ticks: {
                callback: (value) => value.toLocaleString('ru-RU'), // 格式化Y轴值
            },
        },
    },
    plugins: {
        datalabels: {
            anchor: 'end',
            align: 'top',
            font: {
                size: 20,
                weight: 'bold',
            },
            formatter: (value) => value.toLocaleString('ru-RU'), // 格式化值标签
            color: 'rgba(54, 162, 235, 1)', // 文本颜色
            textAlign: 'center',
        },
        title: {
            display: true,
            text: `Продажи за ${currentMonth} ${moment().format('YYYY')} (${moment().format('DD.MM - HH:mm')})`,
            color: 'rgba(54, 162, 235, 1)',
            font: {
                size: 20,
                weight: 'bold',
                lineHeight: 1.2,
            },
        }
    },
};

const configuration = {
    type: 'bar', // 柱状图类型
    data: chartData,
    options: chartOptions,
    plugins: [ChartDataLabels], // 注册插件
};

const image = await canvas.renderToBuffer(configuration);

告诉我如何解决这个问题。

英文:

I wrote a telegram bot on NodeJS that shows a bar graph of sales on request.

如何修复Chart JS图表上列值显示在错误位置的问题?

如何修复Chart JS图表上列值显示在错误位置的问题?

The first graph shows the correct arrangement of column values. The second one is incorrect. The correct graph appears only when the first query is processed, and all repeated queries draw the wrong graph (with incorrect display of column values).

const { ChartJSNodeCanvas } = require('chartjs-node-canvas');
const width = 600; // Chart width
const height = 400; // Chart height
const chartCallback = (ChartJS) => {
ChartJS.defaults.font.size = 14; // Set default font size
};
const canvas = new ChartJSNodeCanvas({ width, height, chartCallback });
const chartData = {
labels: ['План', 'Факт', 'Прогноз'],
datasets: [
{
label: 'Сумма',
data: [salesTarget, monthSum, monthForcastSum],
backgroundColor: 'rgba(54, 162, 235, 0.5)', // Bar background color
borderColor: 'rgba(54, 162, 235, 1)', // Bar border color
borderWidth: 1, // Bar border width
},
],
};
const chartOptions = {
responsive: false, // Disable automatic responsiveness
scales: {
y: {
beginAtZero: true, // Start Y axis from zero
ticks: {
callback: (value) => value.toLocaleString('ru-RU'), // Format Y axis values
},
},
},
plugins: {
datalabels: {
anchor: 'end',
align: 'top',
font: {
size: 20,
weight: 'bold',
},
formatter: (value) => value.toLocaleString('ru-RU'), // Format value labels
color: 'rgba(54, 162, 235, 1)', //'black', // Label text color
textAlign: 'center',
},
title: {
display: true,
text: `Продажи за ${currentMonth} ${moment().format('YYYY')} (${moment().format('DD.MM - HH:mm')})`,
color: 'rgba(54, 162, 235, 1)',
font: {
size: 20,
weight: 'bold',
lineHeight: 1.2,
},
}
},
};
const configuration = {
type: 'bar', // Bar chart type
data: chartData,
options: chartOptions,
plugins: [ChartDataLabels], // Register the plugin
};
const image = await canvas.renderToBuffer(configuration);

Tell me how to deal with this problem.

答案1

得分: 0

const chartOptions = {
//...
plugins: {
datalabels: {
anchor: 'start', // Заменить 'end' на 'start'
//...
},
//...
},
};

英文:
const chartOptions = {
//...
plugins: {
datalabels: {
anchor: 'start', // Заменить 'end' на 'start'
//...
},
//...
},

};

huangapple
  • 本文由 发表于 2023年6月1日 18:52:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定