gpt4 book ai didi

javascript - 将鼠标悬停在谷歌图表上,在 Firefox 上表现得很奇怪

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:50 25 4
gpt4 key购买 nike

将悬停事件添加到 google chart svg 元素时,firefox 和 chrome 之间的结果不同(chrome 正在做我所期望的)。我想要实现的是用户能够将鼠标悬停在图表上而不关心他离线有多近 - 悬停应该流畅且易于使用。

这是相关的插件:https://plnkr.co/edit/2IF2BnX0tS2wznAUr5bs?p=preview

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<script>
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});

function drawChart() {
var dataTable = new google.visualization.DataTable({
cols: [
{ id: 'x', label: 'Num', type: 'number' },
{ id: 'y', label: 'Fn', type: 'number' }
]
});

for (var i = 0; i < 1000; i++) {
var xValue = { v: i };
var yValue = { v: i };

// add data row
dataTable.addRow([
xValue,
yValue
]);
}

var container = document.getElementById('chart_div');
var chart = new google.visualization.ChartWrapper({
chartType: 'LineChart',
dataTable: dataTable,
options: {
hAxis: {
gridlines: {
color: 'transparent'
},
title: 'Hover here is also fine'
},
tooltip: {
trigger: "none"
},
vAxis: {
gridlines: {
color: 'transparent'
},
title: 'Hover here is OK'
}
}
});

// add hover line
google.visualization.events.addOneTimeListener(chart, 'ready', function () {
var svgParent = container.getElementsByTagName('svg')[0];
var layout = chart.getChart().getChartLayoutInterface();
var lineHeight = layout.getBoundingBox('chartarea').height - 18;
var lineTop = layout.getBoundingBox('chartarea').top;

var hoverLine = container.getElementsByTagName('rect')[0].cloneNode(true);
hoverLine.setAttribute('y', lineTop);
hoverLine.setAttribute('height', lineHeight);
hoverLine.setAttribute('width', '1');
hoverLine.setAttribute('stroke', 'none');
hoverLine.setAttribute('stroke-width', '0');
hoverLine.setAttribute('fill', '#cccccc');
hoverLine.setAttribute('x', 0);
svgParent.appendChild(hoverLine);

svgParent.addEventListener("mousemove", function (e) {
hoverLine.setAttribute('x', e.offsetX);
});
});

chart.draw(container);
}
</script>
<div id="chart_div"></div>

将鼠标悬停在带有 chrome 的图表上以查看预期的行为。使用 firefox 将鼠标悬停在图表上以查看问题。知道如何解决这个问题吗?这是谷歌图表错误吗?我是否将事件监听器添加到了错误的元素?

最佳答案

您的 svg 中的子标签似乎正在捕获 mousemove 事件(由于事件传播而产生)。

只需向这些子元素添加一个指针事件:无(我建议为该 svg 元素使用一个 id)

svg *{
pointer-events: none;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
</head>

<body>
<script>
google.charts.load('current', {
callback: drawChart,
packages: ['corechart']
});

function drawChart() {
var dataTable = new google.visualization.DataTable({
cols: [
{id: 'x', label: 'Num', type: 'number'},
{id: 'y', label: 'Fn', type: 'number'}
]
});

for (var i = 0; i < 1000; i++) {
var xValue = { v: i };
var yValue = { v: i };

// add data row
dataTable.addRow([
xValue,
yValue
]);
}

var container = document.getElementById('chart_div');
var chart = new google.visualization.ChartWrapper({
chartType: 'LineChart',
dataTable: dataTable,
options: {
hAxis: {
gridlines: {
color: 'transparent'
},
title: 'Hover here is also fine'
},
tooltip: {
trigger: "none"
},
vAxis: {
gridlines: {
color: 'transparent'
},
title: 'Hover here is OK'
}
}
});

// add hover line
google.visualization.events.addOneTimeListener(chart, 'ready', function () {
var svgParent = container.getElementsByTagName('svg')[0];
var layout = chart.getChart().getChartLayoutInterface();
var lineHeight = layout.getBoundingBox('chartarea').height - 18;
var lineTop = layout.getBoundingBox('chartarea').top;

var hoverLine = container.getElementsByTagName('rect')[0].cloneNode(true);
hoverLine.setAttribute('y', lineTop);
hoverLine.setAttribute('height', lineHeight);
hoverLine.setAttribute('width', '1');
hoverLine.setAttribute('stroke', 'none');
hoverLine.setAttribute('stroke-width', '0');
hoverLine.setAttribute('fill', '#cccccc');
hoverLine.setAttribute('x', 0);
svgParent.appendChild(hoverLine);

svgParent.addEventListener("mousemove", function(e) {
hoverLine.setAttribute('x', e.offsetX);
});
});

chart.draw(container);
}
</script>
<div id="chart_div"></div>
</body>
</html>

关于javascript - 将鼠标悬停在谷歌图表上,在 Firefox 上表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43460819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com