Created
February 15, 2018 11:05
-
-
Save stardines/c45b092fc4d9580d7a70484549e79644 to your computer and use it in GitHub Desktop.
chartjs all tooltips helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Chart.pluginService.register({ | |
beforeRender: function (chart) { | |
if (chart.config.options.showAllTooltips) { | |
// create an array of tooltips | |
// we can't use the chart tooltip because there is only one tooltip per chart | |
chart.pluginTooltips = []; | |
chart.config.data.datasets.forEach(function (dataset, i) { | |
chart.getDatasetMeta(i).data.forEach(function (sector, j) { | |
chart.pluginTooltips.push(new Chart.Tooltip({ | |
_chart: chart.chart, | |
_chartInstance: chart, | |
_data: chart.data, | |
_options: chart.options.tooltips, | |
_active: [sector] | |
}, chart)); | |
}); | |
}); | |
// turn off normal tooltips | |
chart.options.tooltips.enabled = false; | |
} | |
}, | |
afterDraw: function (chart, easing) { | |
if (chart.config.options.showAllTooltips) { | |
// we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once | |
if (!chart.allTooltipsOnce) { | |
if (easing !== 1) | |
return; | |
chart.allTooltipsOnce = true; | |
} | |
// turn on tooltips | |
chart.options.tooltips.enabled = true; | |
Chart.helpers.each(chart.pluginTooltips, function (tooltip) { | |
tooltip.initialize(); | |
tooltip.update(); | |
tooltip.pivot(); | |
tooltip.transition(easing).draw(); | |
}); | |
chart.options.tooltips.enabled = false; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, can you please license this code of yours?
Thanks