Charts google可视化工具提示如何计算左侧和顶部的值?

Charts google可视化工具提示如何计算左侧和顶部的值?,charts,google-visualization,react-google-charts,Charts,Google Visualization,React Google Charts,google可视化工具提示如何计算样式的左上角值? 示例-关于调试,我可以看到:- <div class="google-visualization-tooltip" style="width: 1px; height: 0px; left: 453.25px; top: 44.91px;"</div> 在'corechart'包中的大多数图表, 用于获取各种图表元素的位置 首先,获取图表的布局界面 var chartLayout = chart.getChartLayout

google可视化工具提示如何计算样式的左上角值? 示例-关于调试,我可以看到:-

<div class="google-visualization-tooltip" style="width: 1px; height: 0px; left: 453.25px; top: 44.91px;"</div>

'corechart'
包中的大多数图表,
用于获取各种图表元素的位置

首先,获取图表的布局界面

var chartLayout = chart.getChartLayoutInterface();
布局界面具有方法-->
getBoundingBox(id)

其中
id
是图表元素的字符串id。
要查找点的位置,请将此格式用于
id
-->
点#系列#行
-->
点#0

我们可以在MouseOver上使用图表事件
onmouseover
来了解点何时悬停,
此时将显示工具提示。
onmouseover
事件发送一个参数,其中包含数据表中的行和列,
正在悬停的点的

因此,我们可以获得布局、找到点并定位工具提示

google.visualization.events.addListener(chart, 'onmouseover', function (sender) {
  // ensure point is hovered
  if (sender.row !== null) {
    var padding = 16;
    var chartLayout = chart.getChartLayoutInterface();
    var pointBounds = chartLayout.getBoundingBox('point#' + (sender.column - 1) + '#' + sender.row);
    var tooltip = chart.getContainer().getElementsByClassName('google-visualization-tooltip');
    if (tooltip.length > 0) {
      var tooltipBounds = tooltip[0].getBoundingClientRect();
      tooltip[0].style.top = (pointBounds.top - tooltipBounds.height - padding) + 'px';
      tooltip[0].style.left = ((pointBounds.left + (pointBounds.width / 2)) - (tooltipBounds.width  / 2)) + 'px';
    }
  }
});
请参阅以下工作片段

google.charts.load('current'{
软件包:['corechart']
}).然后(函数(){
var data=new google.visualization.DataTable({
“科尔斯”:[
{“标签”:“x”,“类型”:“编号”},
{“标签”:“y”,“类型”:“编号”}
],
“行”:[
{“c”:[{“v”:2015},{“v”:1}]},
{“c”:[{“v”:2016},{“v”:2}]},
{“c”:[{“v”:2017},{“v”:3}]},
{“c”:[{“v”:2018},{“v”:4}]},
{“c”:[{“v”:2019},{“v”:5}]},
{“c”:[{“v”:2020},{“v”:6}]}
]
});
var chart=new google.visualization.LineChart(document.getElementById('chart_div'));
google.visualization.events.addListener(图表,'onmouseover',函数(发送方){
//确保点处于悬停状态
if(sender.row!==null){
var=16;
var chartLayout=chart.getChartLayoutInterface();
var pointBounds=chartLayout.getBoundingBox('point#'+(sender.column-1)+'#'+sender.row);
var tooltip=chart.getContainer().getElementsByClassName('google-visualization-tooltip');
如果(tooltip.length>0){
var tooltipBounds=工具提示[0]。getBoundingClientRect();
工具提示[0]。style.top=(pointBounds.top-tooltipBounds.height-padding)+“px”;
工具提示[0]。style.left=((pointBounds.left+(pointBounds.width/2))-(tooltipBounds.width/2))+'px';
}
}
});
变量选项={
图表区:{
底数:32,
左:32,
右:32,
前48名,
宽度:“100%”,
身高:“100%”
},
哈克斯:{
格式:“0”,
ticks:data.getdistinctValue(0)
},
图例:{
位置:'顶部'
},
点数:4,
工具提示:{
伊什特尔:没错,
触发器:“两个”
}
};
图表绘制(数据、选项);
});

这个问题运气好吗?