使用Chart.js、react-chart-js和chartjs-plugin-annotation时,注解消息不会出现,

vwoqyblh  于 2023-04-20  发布在  Chart.js
关注(0)|答案(1)|浏览(239)

显示代码

  1. import React from "react";
  2. import {
  3. Chart as ChartJS,
  4. CategoryScale,
  5. LinearScale,
  6. PointElement,
  7. LineElement,
  8. Title,
  9. Tooltip,
  10. Legend,
  11. } from "chart.js";
  12. import { Line } from "react-chartjs-2";
  13. import annotationPlugin from "chartjs-plugin-annotation";
  14. ChartJS.register(
  15. CategoryScale,
  16. LinearScale,
  17. PointElement,
  18. LineElement,
  19. Title,
  20. Tooltip,
  21. Legend,
  22. annotationPlugin
  23. );
  24. export const options = {
  25. responsive: true,
  26. plugins: {
  27. legend: {
  28. position: "top",
  29. },
  30. title: {
  31. display: true,
  32. text: "Chart.js Line Chart",
  33. },
  34. annotation: {
  35. annotations: {
  36. line1: {
  37. adjustScaleRange: true,
  38. drawTime: "afterDatasetsDraw",
  39. type: "line",
  40. scaleID: "x",
  41. value: 1,
  42. borderColor: "rgba(104,1)",
  43. label: {
  44. enabled: true,
  45. content: "Hi !!",
  46. backgroundColor: "rgba(255, 26, 104, 0.8)",
  47. color: "black",
  48. },
  49. },
  50. },
  51. },
  52. },
  53. };
  54. const labels = [1, 2, 3, 4];
  55. export const data = {
  56. labels,
  57. datasets: [
  58. {
  59. label: "My First Dataset",
  60. data: [65, 59, 80, 81, 56, 55, 40],
  61. fill: false,
  62. borderColor: "rgb(75, 192, 192)",
  63. tension: 0.1,
  64. },
  65. ],
  66. };
  67. export function ChartPrueba() {
  68. return (
  69. <div>
  70. <h1>Example react-chartjs-2 Chart with the annotation plugin</h1>
  71. <Line options={options} data={data} />;
  72. </div>
  73. );
  74. }

我尝试禁用tailwindcss并直接将其导入到应用程序中,我使用Vite,这些是依赖项

  1. "dependencies": {
  2. "chart.js": "^4.2.1",
  3. "chartjs-plugin-annotation": "^2.2.1",
  4. "react": "^18.2.0",
  5. "react-chartjs-2": "^5.2.0",
  6. "react-dom": "^18.2.0"
  7. },

Stack让我多加一些我不知道为什么的词。所有的东西都已经在那里了,所以我随机加了一些词。Stack让我多加一些我不知道为什么的词。所有的东西都已经在那里了,所以我随机加了一些词。Stack让我多加一些我不知道为什么的词。所有的东西都已经在那里了,所以我随机加了一些词。Stack让我多加一些我不知道为什么的词。一切都已经存在,所以我随机添加单词。堆栈要求我添加更多的单词,我不知道为什么。一切都已经存在,所以我随机添加单词。堆栈要求我添加更多的单词,我不知道为什么。一切都已经存在,所以我随机添加单词。
}

clj7thdc

clj7thdc1#

要显示线注解的标签,需要设置display: true而不是enabled选项。
因此,您的标签配置应为:

  1. label: {
  2. display: true, // <-- NOT enabled
  3. content: "Hi !!",
  4. backgroundColor: "rgba(255, 26, 104, 0.8)",
  5. color: "black",
  6. },

相关问题