C3图表-可点击的工具提示内容

Vik*_*don 5 javascript jquery c3

我正在使用c3.js制作图表。我必须使工具提示的内容易于理解。到目前为止,只有当我将鼠标悬停在图表上时,工具提示才可见。我单击工具提示中的链接时会显示一些信息。我从c3文档中找不到任何帮助。我正在处理的代码片段如下所示。

$scope.timelineConfig.tooltip.contents = function (data, 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;
    text = "<div id='tooltip' class='d3-tip'>"; 
    title = dates[data[0].index];
    text += "<span class='info'><b><u>Date</u></b></span><br>";
    text += "<span class='info'>"+ title +"</span><br>";
    text += "<span class='info'><b><u>Features</u> : </b> " + features[data[0].index] + "</span><br>";
    text += "<span class='info'><b><u>Enhancements</u> : </b> " + defects[data[0].index] + "</span><br>";
    text += "</div>";
    return text;
};
Run Code Online (Sandbox Code Playgroud)

我必须使内容(<span><b><u>Features...</u></b></span>)可点击。

pot*_*ngs 4

首先(如果您还没有这样做)覆盖工具提示位置,以便当您尝试单击它时它不会继续逃跑。

tooltip: {
    position: function () {
        var position = c3.chart.internal.fn.tooltipPosition.apply(this, arguments);
        position.top = 0;
        return position;
    },
Run Code Online (Sandbox Code Playgroud)

然后,您需要重写 hideTooltip 函数,以便在检测到您的单击事件之前它不会关闭。

var originalHideTooltip = chart.internal.hideTooltip
chart.internal.hideTooltip = function () {
    setTimeout(originalHideTooltip, 100)
};
Run Code Online (Sandbox Code Playgroud)

然后,您只需要覆盖样式pointer-events(以便鼠标事件不会被忽略),然后像通常在 jQuery 中那样附加处理程序

$(".c3-tooltip-container")
    .css("pointer-events", "auto")
    .on('click', '.info:eq(2)', function () {
        // add click functionality here. you could pass in additional data using the span attributes
        alert($(this).text())
    })
Run Code Online (Sandbox Code Playgroud)

根据需要修改选择器(例如添加图表包装器 ID...)


小提琴 - http://jsfiddle.net/5vbeb4k8/