ChartsJS Annotations Plugin - 您可以创建一个工具提示来伴随注释吗?

cyb*_*g95 5 javascript charts tooltip chart.js chartjs-plugin-annotation

我试图弄清楚当我将鼠标悬停/鼠标悬停在我绘制的垂直线注释上时如何添加工具提示:

annotation: {
   annotations: [{
      "drawTime": "afterDatasetsDraw",
      "type": "line",
      "mode": "vertical",
      "scaleID": "x-axis-0",
      "value": "2020-04-01 12:20:27.709523",
      "borderWidth": 2,
      "borderColor": "red",
      "label": {
        "content": "Example Content",
        "enabled": true,
        "position": "top"
      },
      onMouseover: function(e) {
        console.log("I AM GETTING CALLED!");
      },
   },
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这是如何做到的吗?

Cri*_*sty 0

不直接回答您的问题,但您可以通过访问上下文和(文档)以您喜欢的任何方式(在 ChartJS v3 中)修改图表chart和注释element

我用它来改变悬停时的注释样式(在 TypeScript 中):

enter: (context) => {
    const id = context.element.options.id!;
    ((context.chart.options.plugins!.annotation!.annotations as any)[id] as AnnotationOptions).borderColor = 'blue';
    context.chart.canvas.style.cursor = 'pointer';
    context.chart.update();
},
leave: (context) => {
    const id = context.element.options.id!;
    ((context.chart.options.plugins!.annotation!.annotations as any)[id] as AnnotationOptions).borderColor = 'rgb(255, 99, 132)';
    context.chart.canvas.style.cursor = 'default';
    context.chart.update();
},
Run Code Online (Sandbox Code Playgroud)