简体   繁体   English

Google Charts Timelines 的虚拟行?

[英]Dummy rows for Google Charts Timelines?

I'm looking for a way to add a dummy row to Google Charts Timelines .我正在寻找一种向Google Charts Timelines添加虚拟行的方法。 Here is what I'm trying to accomplish:这是我想要完成的:

在此处输入图片说明

However, the dummy row should be transparent and should not have interactivity (no tooltip, no select event, etc.).但是,虚拟行应该是透明的并且不应该具有交互性(没有工具提示、没有选择事件等)。

Here is my workaround:这是我的解决方法:

在此处输入图片说明

This requires adding three more columns and you lose the tooltip generated by Charts.这需要再添加三列,并且您会丢失 Charts 生成的工具提示。 While this isn't an issue for me (as I will be customizing the tooltips), it may be for others.虽然这对我来说不是问题(因为我将自定义工具提示),但对其他人来说可能是问题。 Furthermore, although the dummy row is transparent, there is still interactivity (as indicated by the empty tooltip I circled).此外,虽然虚拟行是透明的,但仍然存在交互性(如我圈出的空工具提示所示)。 The workaround for this is to add the following code immediately before chart.draw(dataTable) :这种情况的解决方法是立即之前添加以下代码chart.draw(dataTable)

function onMouseOver(e) {
    var tooltips = document.getElementsByClassName('google-visualization-tooltip');

    for (var i = 0; i < tooltips.length; i++) {
        if (!tooltips[i].innerHTML) {
            tooltips[i].style.display = 'none';
        }
    }
}

function onReady() {
    google.visualization.events.addListener(chart, 'onmouseover', onMouseOver);
}

google.visualization.events.addListener(chart, 'ready', onReady);

While this is technically a solution to my problem, it's a hack at best.虽然这在技术上是我的问题的解决方案,但充其量只是一个黑客。 Is there no straightforward way to accomplish this with the API?有没有直接的方法可以用 API 来完成这个?

This is already a year old question, but maybe someone else will have the same problem.这已经是一个老问题了,但也许其他人会遇到同样的问题。 Your solution from second screen was very close.您在第二个屏幕上的解决方案非常接近。 You need to set tooltip value on normal rows to null (will display standard tooltip) and empty string on dummy items (tooltip will be hidden).您需要将普通行上的工具提示值设置为 null(将显示标准工具提示)并在虚拟项目上设置为空字符串(工具提示将被隐藏)。 The style column must have opacity set to 0 on dummy rows to hide the bar.样式列必须在虚拟行上将不透明度设置为 0 以隐藏栏。 Below is rewritten and fixed code from your example.以下是您示例中重写和修复的代码。

google.charts.load('current', { packages: ['timeline']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
    const container = document.getElementById('timeline');
    const chart = new google.visualization.Timeline(container);
    const dataTable = new google.visualization.DataTable();

    dataTable.addColumn({ type: 'string', label: 'President', id: 'President' });
    dataTable.addColumn({ type: 'string', id: 'Empty label' });
    dataTable.addColumn({ type: 'string', id: 'style', role: 'style' });
    dataTable.addColumn({ type: 'string', id: 'tooltip', role: 'tooltip', p: { html: true } });
    dataTable.addColumn({ type: 'date', label: 'Start', id: 'Start' });
    dataTable.addColumn({ type: 'date', label: 'End', id: 'End' });

    dataTable.addRows([
        ['A', '', 'opacity: 0', '', new Date('2018-06-05T00:00:00'), new Date('2018-06-05T00:00:00')],
        ['B', '', 'opacity: 0', '', new Date('2018-06-05T00:00:00'), new Date('2018-06-05T00:00:00')],
        ['C', '', 'opacity: 0', '', new Date('2018-06-05T00:00:00'), new Date('2018-06-05T00:00:00')],
        ['A', '', null, null, new Date('2018-06-05T01:00:00'), new Date('2018-06-05T02:00:00')],
        ['B', '', null, null, new Date('2018-06-05T01:00:00'), new Date('2018-06-05T02:00:00')],
        ['C', '', null, null, new Date('2018-06-05T01:00:00'), new Date('2018-06-05T02:00:00')],
    ]);
}

After tackling with this problem I prefer to use apexcharts package for timeline chart creating.解决了这个问题后,我更喜欢使用apexcharts包来创建时间线图表。 This package has more flexible interface to control the charts, min and max values of axis, tooltips, etc.这个包有更灵活的界面来控制图表、轴的最小值和最大值、工具提示等。

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

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