GEt悬停数据点的X轴值

问题描述 投票:4回答:1

当用户在线图中悬停一个数据点时,我想获得该特定数据点的X轴值,以便我可以根据该值定位工具提示。有没有办法可以做到这一点?我们的想法是获取X中的值并减去工具提示容器宽度以将其定位在该点的左侧,以避免覆盖数据的工具提示出现问题。

我的代码到目前为止:

http://jsfiddle.net/owhxgaqm/292/

// c3 - custom tooltip
var chart = c3.generate({
    bindto: '#chart-test',
    data: {
        columns: [
            ['data1', 100, 200, 150, 300, 200],
            ['data2', 400, 500, 250, 700, 3000000000000], ]
    },
    tooltip: {
         position: function(data, width, height, thisElement) {
          var containerWidth, tooltipWidth, x, y;
          element = document.getElementById("chart-test");
          containerWidth = element.clientWidth;
          tooltipWidth = element.querySelector('.c3-tooltip').clientWidth * 2;
          x = parseInt(thisElement.getAttribute('x'));
          console.log("x is " + x)
          console.log("tooltipWidth is " + tooltipWidth)
          console.log("data is " + data)
          console.log("thisElement is " + thisElement)
          if (x + tooltipWidth > containerWidth) {
            x = containerWidth - tooltipWidth - 2;
          }
          y = thisElement.getAttribute('y');
          y = y - height;
          return {
            top: y,
            left: x
          };
        }
    }
});
c3.js
1个回答
0
投票

可能这可能有帮助!!!

tooltip: {
    show: true,
    grouped: true,
    contents: function (d, defaultTitleFormat, defaultValueFormat, color) {
            var $$ = this, config = $$.config, titleFormat = config.tooltip_format_title || defaultTitleFormat, nameFormat = config.tooltip_format_name || function (name) {
                            return name;
                    }, valueFormat = config.tooltip_format_value || defaultValueFormat, text, i, title, value, name, bgcolor;
            for (i = 0; i < d.length; i++) {
                    if (!(d[i] && (d[i].value || d[i].value === 0))) {
                            continue;
                    }
                    if (!text) {
                            title = titleFormat ? titleFormat(d[i].x) : d[i].x;
                            text = '<table class=\'' + $$.CLASS.tooltip + '\'>' + (title || title === 0 ? '<tr><th colspan=\'2\'>' + title + '</th></tr>' : '');
                    }
                    name = d[i].name;
                    value = d[i].value, d[i].ratio, d[i].id, d[i].index;
                    bgcolor = $$.levelColor ? $$.levelColor(d[i].value) : color(d[i].id);
                    text += '<tr class=\'' + $$.CLASS.tooltipName + '-' + d[i].id + '\'>';
                    text += '<td class=\'name\'><span style=\'background-color:' + bgcolor + '\'></span>' + name + '</td>';
                    text += '<td class=\'value\'>' + value + '</td>';
                    text += '</tr>';
            }
            return text + '</table>';
    }
}

上面的代码用于操作与原始内容完全相同的内容,并且可以通过编辑进行相应的修改。根据您的问题,您可以提供填充以避免工具提示与数据点重叠。只需在上面的表格标记上指定样式即可。

© www.soinside.com 2019 - 2024. All rights reserved.