gpt4 book ai didi

javascript - 如何在鼠标悬停在栏上时显示标签

转载 作者:可可西里 更新时间:2023-11-01 02:51:54 25 4
gpt4 key购买 nike

我用 chartist.js 制作了一个条形图。

现在我想在条上添加一些监听事件。

当鼠标悬停在栏上时,如何让标签显示出来?

最佳答案

您有 2 个选择 -


选项 1


您可以使用一个工具提示插件。您可以在这里找到它 - https://github.com/Globegitter/chartist-plugin-tooltip

添加 CSS 和 JS 文件后,您应该能够像这样调用插件 - Chartist.plugins.tooltip()

这是他们的 Plugins 中的示例页 -

var chart = new Chartist.Line('.ct-chart', {
labels: [1, 2, 3],
series: [
[
{meta: 'description', value: 1 },
{meta: 'description', value: 5},
{meta: 'description', value: 3}
],
[
{meta: 'other description', value: 2},
{meta: 'other description', value: 4},
{meta: 'other description', value: 2}
]
]
}, {
low: 0,
high: 8,
fullWidth: true,
plugins: [
Chartist.plugins.tooltip()
]
});

这将是更容易和更好的选择。


选项 2


如果你想自己做一些事情,你可以在draw事件的回调上绑定(bind)mouseovermouseout事件-

var data = {
labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
series: [
[1, 2, 4, 8, 6, -2, -1, -4, -6, -2]
]
};

var options = {
high: 10,
low: -10,
axisX: {
labelInterpolationFnc: function(value, index) {
return index % 2 === 0 ? value : null;
}
}
};

var chart = new Chartist.Bar('.ct-chart', data, options);

// Based on ty's comment
chart.on('created', function(bar) {
$('.ct-bar').on('mouseover', function() {
$('#tooltip').html('<b>Selected Value: </b>' + $(this).attr('ct:value'));
});

$('.ct-bar').on('mouseout', function() {
$('#tooltip').html('<b>Selected Value:</b>');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/chartist.js/0.9.5/chartist.min.js"></script>
<link href="https://cdn.jsdelivr.net/chartist.js/0.9.5/chartist.min.css" rel="stylesheet" />
<div id="tooltip"><b>Selected Value:</b>
</div>
<div class="ct-chart"></div>

更新:根据 ty 的评论更新了代码

关于javascript - 如何在鼠标悬停在栏上时显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562140/

25 4 0