简体   繁体   English

当我将鼠标悬停在 ChartJS 条形图中的条形上时,如何将光标更改为指针?

[英]How to change the cursor to a pointer when I hover over a bar in a ChartJS bar chart?

Here is my chart creation这是我的图表创建

createChart = function (name, contributions, idArray) {
    globalIdArray = idArray;
    var ctx = document.getElementById("myChart");
    var myChart = new Chart(ctx, {
        type: "bar",
        data: {
            labels: name,
            datasets: [{
                label: "Contributions",
                data: contributions,
                backgroundColor: [
                    'rgba(0, 255, 0, 0.2)',
                    'rgba(0, 255, 0, 0.2)',
                    'rgba(0, 255, 0, 0.2)',
                    'rgba(0, 255, 0, 0.2)',
                    'rgba(0, 255, 0, 0.2)'
                ]
            }]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            },
            onClick: handleClick,
            onHover: handleHover
        }
    });

This is the HTML, everything is JavaScript and resides inside the canvas element.这是 HTML,一切都是 JavaScript 并且驻留在 canvas 元素中。

<canvas chart-hover="onHover" id="myChart" width="200" height="200"></canvas>

I made it so when I click on a bar they act as a "drill in" and take my a page with more details.我做到了,所以当我点击一个栏时,它们充当“钻取”并带我一个包含更多详细信息的页面。 To do that I used this code:为此,我使用了以下代码:

function handleClick(e) {
            debugger;
            var activeElement = myChart.getElementAtEvent(e);
            var designerId = _idArray[activeElement[0]._index];
            window.location.href = "/Display/search/index?designerId=" + designerId;
        }

I'd like to make it so my cursor changes to a pointer to let the user know they can click the bar (as it would normally change with a link).我想让我的光标变成一个指针,让用户知道他们可以点击栏(因为它通常会随着链接而改变)。 I saw some examples of this on Stack Overflow but they were from the last year or two, the posts included comments asking for an updated version but no solutions.我在 Stack Overflow 上看到了一些这样的例子,但它们是过去一两年的,帖子包括要求更新版本的评论,但没有解决方案。

Here is another post I saw which I believe is the same thing I am trying to accomplish but it didn't work for me.这是我看到的另一篇文章,我相信它与我正在努力完成的事情相同,但它对我不起作用。

How to change cursor style to pointer on hovering over points of the chart? 如何将光标样式更改为悬停在图表点上的指针?

Does anyone know of how I can implement this, using the most recent live version of ChartJS?有谁知道我如何使用 ChartJS 的最新实时版本来实现这一点?

I needed the cursor to change only when over a bar or point on a line chart so came up with this simple solution.我只需要在折线图上的条形或点上更改光标,所以想出了这个简单的解决方案。 Place your function outside the chart declaration.将您的函数放在图表声明之外。

var chart = new Chart(ctx, {
    data: {...},
    options: {
     onHover: graphHover
    }
});
function graphHover(e, array) {
    if(array.length > 0) {
        e.target.style.cursor = 'pointer';
    }else {
        e.target.style.cursor = '';
    }
}

there is a cursor property in css that you can set.您可以在 css 中设置一个游标属性。 for example :例如 :

span.crosshair {
    cursor: crosshair;
}

span.help {
    cursor: help;
}

span.wait {
    cursor: wait;
}

Please refer to this link for more information请参阅链接以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM