Interacting with C3 Tooltip
C3 tooltip customization
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div id="chart"></div>
CSS
.c3-tooltip-container {
background-color: #ccc;
border: solid 1px black;
padding: 20px;
pointer-events: auto !important;
}
JavaScript
/* Question: how to make the tooltip disappear again, either by clicking or with a timeout? */
var features = dates = defects = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
];
// tooltip onclick
window.action = function() {
// do something
// ...
clearTimeout(timeout);
hideTooltip();
}
// store for timer
var timeout;
var chart = c3.generate({
data: {
columns: [
['data1', 30000, 20000, 10000, 40000, 15000, 250000],
['data2', 100, 200, 100, 40, 150, 250]
],
},
tooltip: {
position: function () {
var position = c3.chart.internal.fn.tooltipPosition.apply(this, arguments);
position.top = 0;
return position;
},
contents: function (data, defaultTitleFormat, defaultValueFormat, color) {
// restart timeout
clearTimeout(timeout);
timeout = setTimeout(hideTooltip, 5000); // auto-hide after 5 seconds
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' onclick='window.action()'>";
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;
}
}
});
// disable default hide
c3.chart.internal.fn.hideTooltip = function (){};
// hide tooltip manually
var hideTooltip = function()...