首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Chartjs 2.9.x with React中使用Chartjs-plugin-注释0.5.7

在Chartjs 2.9.x with React中使用Chartjs-plugin-注释0.5.7
EN

Stack Overflow用户
提问于 2021-11-27 10:38:15
回答 1查看 1.4K关注 0票数 0

我无法在Chartjs中配置plugin插件注释。

从文档中,我安装了V0.5.7,因为我使用的是Chart.js V2.9.4。

在这里,我的配置:

注册插件:

代码语言:javascript
复制
import Chart from "chart.js";
import annotationPlugin from "chartjs-plugin-annotation";

Chart.plugins.register(annotationPlugin);
//Chart.pluginService.register(annotationPlugin); //also tried this but doesn't work

在这里,选项配置(简化),我也尝试将“烦恼”包装在“插件”中,但它不起作用:

代码语言:javascript
复制
  scales: {
    yAxes: [
      {
        scaleLabel: {
          ...
        },
        ticks: {
          ...
        },
        gridLines: {
          ...
        },
      },
    ],
    xAxes: [
      {
        gridLines: {
          ...
        },
        ticks: {
          ...
        },
      },
    ],
  },
  maintainAspectRatio: false,
  legend: {
    ...
    labels: {
      ...
    },
  },
  tooltips: {
    ...
  },
  annotation: {
    annotations: [
      {
        type: "line",
        mode: "horizontal",
        scaleID: "yAxes",
        value: 2.62,
        borderColor: "white",
        borderWidth: 4,
      },
    ],
  },
  hover: {
    ...
  },

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-27 11:27:48

看起来,您正在尝试实现注释插件,就好像要将它集成到当前的chart.js版本中一样。例如,在plugins中定义注释。

这是一个适用于给定版本的示例。请注意,我立即在annotation中添加了options对象,这给出了想要的注释。(在我的例子中是以红线的形式出现的。)

代码语言:javascript
复制
import React, { useEffect, useRef } from "react";
import Chart from "chart.js";
import annotationPlugin from "chartjs-plugin-annotation";

Chart.pluginService.register(annotationPlugin);

const LineGraph = () => {
  const chartRef = useRef(null);

  useEffect(() => {
    if (chartRef?.current) {
      const chartToDraw = chartRef.current.getContext("2d");

      new Chart(chartToDraw, {
        type: "line",
        data: {
          labels: ["Jan", "Feb", "March"],
          datasets: [
            {
              label: "Sales",
              data: [86, 67, 91]
            }
          ]
        },
        options: {
          annotation: {
            drawTime: "afterDatasetsDraw", // (default)
            events: ["click"],
            dblClickSpeed: 350, // ms (default)
            annotations: [
              {
                drawTime: "afterDraw",
                id: "a-line-1",
                type: "line",
                mode: "horizontal",
                scaleID: "y-axis-0",
                value: "25",
                borderColor: "red",
                borderWidth: 2
              }
            ]
          }
        }
      });
    }
  }, []);

  return (
    <div className={{ width: "100%", height: 500 }}>
      <canvas id="myChart" ref={chartRef} />
    </div>
  );
};

export default LineGraph;

您可以看到它在这里工作:https://codesandbox.io/s/chart-js-2-9-4-annotation-example-f3h7d?file=/src/LineGraph.js

0.5.7: Chart.js插件注释文档:https://www.chartjs.org/chartjs-plugin-annotation/0.5.7/

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70134014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档