【问题标题】:react-chartjs-2 vertical line when hovering over chart将鼠标悬停在图表上时 react-chartjs-2 垂直线
【发布时间】:2019-08-13 11:31:17
【问题描述】:

我正在尝试使用 react-chartjs-2 创建一个折线图,当将鼠标悬停在图表中的数据点上时它有一条垂直线。如下图所示:

Chart requirements

我尝试使用 chartjs-plugin-annotation 插件,但结果好坏参半。我设法创建了一条静态线,但不了解它的工作方式或原因。我应该如何实现这一目标?我有什么事吗?

const data = {
    labels: [...week(d)],
    datasets: [
        {
            ...
            data: [10000, 9500, 7000, 4500, 2500, 1500, 500, 0],
        }
    ]
};

var line = [{
    type: "line",
    mode: "vertical",

    // ???
    scaleID: "y-axis-0",
    value: -20000,

    borderColor: "#2984c5",
    borderWidth: 1,
}];

const options = {
    tooltips: {
        xPadding: 20,
        yPadding: 10,
        displayColors: false,
        bodyFontSize: 16,
        bodyFontStyle: 'bold',
    },
    annotation: {
        annotations: line,
    },
    scales: {
        yAxes: [{
            gridLines: {
                drawBorder: false,
                tickMarkLength: 0,
            },
            ticks: {
                fontSize: 14,
                padding: 15,
                max: data.maxY,
                min: 0,
                maxTicksLimit: 6,
                fontColor: "#485465"
            }
        }],
        xAxes: [{
            ticks: {
                padding: 5,
                fontSize: 14,
                fontColor: "#485465",
            },
            gridLines: {
                display: false,
            },
        },

        ],
    },
    responsive: false,
}

我的完整代码在这里:https://codesandbox.io/s/y28mk3rn4z

【问题讨论】:

  • 使用 chrome 72,当你将鼠标悬停在一个点上时,我没有得到垂直线..
  • @jsdeveloper 是的,这就是我需要的帮助
  • 嗯,明白了

标签: reactjs chartjs-2.6.0 react-chartjs


【解决方案1】:

下面的答案是正确的,它只需要一些调整,谢谢乔丹。

Chart.pluginService.register({
    afterDraw: function(chart, easing) {
        if (chart.tooltip._active && chart.tooltip._active.length) {
            const activePoint = chart.controller.tooltip._active[0];
            const ctx = chart.ctx;
            const x = activePoint.tooltipPosition().x;
            const topY = chart.scales['y-axis-0'].top;
            const bottomY = chart.scales['y-axis-0'].bottom;
            ctx.save();
            ctx.beginPath();
            ctx.moveTo(x, topY);
            ctx.lineTo(x, bottomY);
            ctx.lineWidth = 2;
            ctx.strokeStyle = '#e23fa9';
            ctx.stroke();
            ctx.restore();
        }
    }
});

在图表选项中,我们需要添加以下配置以通过悬停在附近显示线条。

tooltips: {
    mode: 'index',
    intersect: false
},
hover: {
    mode: 'index',
    intersect: false
}

PS:如果有多个图表一起绘制,那么代码和平可能会导致问题。如果我们想在特定图表(例如线条)上产生这种效果,那么我们可以添加以下条件。

if (
    chart.tooltip._active &&
    chart.tooltip._active.length &&
    chart.config.type === 'line'
)

这对我有用,希望对您有所帮助。

【讨论】:

    【解决方案2】:

    刚刚仔细研究了这件事,这会让你到达那里!它不会在线条的任一侧进行填充,但会创建垂直线!

    componentWillMount() {
        Chart.pluginService.register({
          afterDraw: function (chart, easing) {
            if (chart.tooltip._active && chart.tooltip._active.length) {
              const activePoint = chart.controller.tooltip._active[0];
              const ctx = chart.ctx;
              const x = activePoint.tooltipPosition().x;
              const topY = chart.scales['y-axis-1'].top;
              const bottomY = chart.scales['y-axis-1'].bottom;
              ctx.save();
              ctx.beginPath();
              ctx.moveTo(x, topY);
              ctx.lineTo(x, bottomY);
              ctx.lineWidth = 2;
              ctx.strokeStyle = '#e23fa9';
              ctx.stroke();
              ctx.restore();
            }
          }
        });
      }
    

    此外,对于拥有多个数据集的任何人,您都可以将其添加到您的选项中,以便一次在所有行上显示工具提示。

    tooltips: {
              mode: 'x'
            },
    

    【讨论】:

    • 这不会在图表中的任何点悬停时显示垂直线。仅当您将鼠标悬停在确切的数据点上时才会显示该线,这不是 OP 想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2018-01-23
    • 2013-12-12
    相关资源
    最近更新 更多