简体   繁体   English

Google 可视化双 Y 轴图表将线与特定条对齐

[英]Google visualization dual Y axis chart align line with specific bar

I have attached snippet for a dual Y axis chart.我附上了双 Y 轴图表的片段。

The orange dot for Ontime% Goal corresponds with the blue bar for Ontime %. Ontime% Goal 的橙色圆点与 Ontime % 的蓝色条相对应。 Both have been assigned to targetAxisIndex: 0两者都已分配给targetAxisIndex: 0

Can I shift/move the dot to align above the blue bar?我可以移动/移动点以对齐蓝条上方吗? (see attached picture for desired position). (所需位置见附图)。

Thank you as always to the experts out there!一如既往地感谢那里的专家!

在此处输入图片说明

 google.charts.load('current', {'packages':['corechart', 'bar']}); google.charts.setOnLoadCallback(drawStuff); function drawStuff() { var button = document.getElementById('change-chart'); var chartDiv = document.getElementById('chart_div'); var data = google.visualization.arrayToDataTable([ ['Type', 'Ontime%', 'Count', 'Ontime% Goal'], ['AE', 90, 500, 100] ]); var classicOptions = { width: 900, series: { 0: {targetAxisIndex: 0, type: 'bars'}, 1: {targetAxisIndex: 1, type: 'bars'}, 2: {targetAxisIndex: 0, type: 'line', pointSize: 8, pointShape: { type: 'circle' } }, }, title: 'Ontime % on the left, Count on the right', bar:{ width: "60%" }, vAxis: { minValue: 0 }, vAxes: { // Adds titles to each axis. 0: {title: 'Ontime %'}, 1: {title: 'Count'} } }; function drawClassicChart() { var classicChart = new google.visualization.ColumnChart(chartDiv); classicChart.draw(data, classicOptions); button.innerText = 'Change to Material'; button.onclick = drawMaterialChart; } drawClassicChart(); };
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <br><br> <div id="chart_div" style="width: 800px; height: 500px;"></div>

nothing out of the box will allow you to adjust the position of the point.没有任何开箱即用的东西可以让您调整点的位置。

you can move it manually, on the chart's ready event.您可以在图表的就绪事件上手动移动它。
but the chart will move it back when the user hovers the point.但是当用户悬停该点时,图表会将其移回。

you can use a MutationObserver to move the point when the chart moves it back,当图表向后移动时,您可以使用MutationObserver移动该点,
but this will just cause it to blink from one spot to the other while it is being hovered.但这只会导致它在悬停时从一个点闪烁到另一个点。

not much you can do, unless you disable tooltips.除非禁用工具提示,否则您无能为力。

see following working snippet,请参阅以下工作片段,
hover the point to see it move...将点悬停以查看其移动...

 google.charts.load('current', { packages: ['corechart'] }).then(function () { //var button = document.getElementById('change-chart'); var chartDiv = document.getElementById('chart_div'); var data = google.visualization.arrayToDataTable([ ['Type', 'Ontime%', 'Count', 'Ontime% Goal'], ['AE', 90, 500, 100] ]); var classicOptions = { width: 900, series: { 0: {targetAxisIndex: 0, type: 'bars'}, 1: {targetAxisIndex: 1, type: 'bars'}, 2: { targetAxisIndex: 0, type: 'line', pointSize: 8, pointShape: {type: 'circle'}, }, }, title: 'Ontime % on the left, Count on the right', bar:{ width: "60%" }, vAxis: { minValue: 0 }, vAxes: { 0: {title: 'Ontime %'}, 1: {title: 'Count'} } }; function drawClassicChart() { var classicChart = new google.visualization.ColumnChart(chartDiv); google.visualization.events.addListener(classicChart, 'ready', function () { var chartLayout = classicChart.getChartLayoutInterface(); var bounds = chartLayout.getBoundingBox('bar#0#0'); var observer = new MutationObserver(function () { var circles = chartDiv.getElementsByTagName('circle'); if (circles.length > 1) { circles[1].setAttribute('cx', (bounds.left + (bounds.width / 2))); } }); observer.observe(chartDiv, { childList: true, subtree: true }); }); classicChart.draw(data, classicOptions); //button.innerText = 'Change to Material'; //button.onclick = drawMaterialChart; } drawClassicChart(); });
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>

best case, you could disable the chart's tooltips,最好的情况,你可以禁用图表的工具提示,
then add your own custom tooltips,然后添加您自己的自定义工具提示,
for both the point and columns, etc...对于点和列等...

the chart does provide mouseover and mouseout events,该图表确实提供了mouseovermouseover mouseout事件,
not sure its worth the effort...不确定它是否值得努力......

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

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