Chart.js提示 – 悬停时标签不显示

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

Chart.js Tooltip - labels on hover does not appear

问题

我想在鼠标悬停在柱状图上时显示numberValue的值作为标签。我已经尝试了许多方法,但在柱状图上悬停时没有显示任何内容。以下是代码部分:

getBarChart() {
    this.http.get(API).subscribe({
      next: (data) => {
        this.totalAmount= data;
        let splitted = this.totalAmount.toString().split(",");
        let numberValue= splitted[0];
        let totalAmountPerMonth = splitted[1];
        let month = splitted[2];

        this.barChart.data.labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

        let dataValues = new Array(12).fill(null);
        let monthIndex = parseInt(month) - 1;
        dataValues[monthIndex] = totalAmountPerMonth;

        this.barChart.data.datasets = [
          {
            label: "Total Amount",
            data: dataValues,
            backgroundColor: DARK_BLUE
          }
        ];
        this.barChart.update();
      },
      error: (err) => {
        console.log(err);
      }
    });

    Chart.register(BarController);
    Chart.register(CategoryScale);
    Chart.register(LinearScale);
    Chart.register(BarElement);

    this.barChart = new Chart("barChart", {
      type: 'bar',
      data: {
        labels: [],
        datasets: []
      },
      options: {
        aspectRatio: 2.5,
        scales: {
          x: {
            title: {
              display: true,
              text: "Month"
            }
          },
          y: {
            title: {
              display: true,
              text: "Total Amount"
            }
          }
        },
        plugins: {
          legend: {
            display: false
          },
          tooltip: {
            callbacks: {
              label: (context) => {
                const value = context.dataset.data[context.dataIndex];
                const numberValue = value !== null ? value : 'N/A';
                return `Number: ${numberValue}`;
              }
            }
          }
        },
      }
    });
  }

希望这可以帮助你在柱状图上显示numberValue的值作为标签。

英文:

I want to display the value of the numberValue as a label on hover on the bar chart. I have tried many ways of doing this, but nothing appears on hover on the bar. Here is the code:

getBarChart() {
this.http.get(API).subscribe({
next: (data) => {
this.totalAmount= data;
let splitted = this.totalAmount.toString().split(",");
let numberValue= splitted[0];
let totalAmountPerMonth = splitted[1];
let month = splitted[2];
this.barChart.data.labels = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
let dataValues = new Array(12).fill(null);
let monthIndex = parseInt(month) - 1;
dataValues[monthIndex] = totalAmountPerMonth;
this.barChart.data.datasets = [
{
label: "Total Amount",
data: dataValues,
backgroundColor: DARK_BLUE
}
];
this.barChart.update();
},
error: (err) => {
console.log(err);
}
});
Chart.register(BarController);
Chart.register(CategoryScale);
Chart.register(LinearScale);
Chart.register(BarElement);
this.barChart = new Chart("barChart", {
type: 'bar',
data: {
labels: [],
datasets: []
},
options: {
aspectRatio: 2.5,
scales: {
x: {
title: {
display: true,
text: "Month"
}
},
y: {
title: {
display: true,
text: "Total Amount"
}
}
},
plugins: {
legend: {
display: false
},
tooltip: {
callbacks: {
label: (context) => {
const value = context.dataset.data[context.dataIndex];
const numberValue = value !== null ? value : 'N/A';
return `Number: ${numberValue}`;
}
}
}
},
}
});
}

答案1

得分: 1

你需要导入并注册 Tooltip 才能使其显示。

英文:

You need to import and register the Tooltip for it to appear

huangapple
  • 本文由 发表于 2023年5月28日 18:14:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350981.html
匿名

发表评论

匿名网友

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

确定