I am trying to make a 'searchable' bar graph in ChartJS, where the user can dynamically search in an input field (onkeyup) for names of x-axis labels. When that label or labels are 'found', that bar (or those bars) are highlighted a different color.
I am not sure how to link the 'input' text and the function that highlights the bar.
Fiddle in progress https://jsfiddle.net/hironymous_vis/mk0sLfgt/9/
Currently, the search does not work because the input does not link to the function that highlights the bar.
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search...">
and
function highlightBar(s) {
// this clears off any tooltip highlights
chart1.update();
chart1.activeElements = [];
// reset any coloring because of selection
chart1.datasets.forEach(function(dataset) {
dataset.bars.forEach(function (bar) {
if (bar.selected) {
bar.fillColor = bar.selected.fillColor;
bar.strokeColor = bar.selected.strokeColor;
delete bar.selected;
}
})
});
var index = chart1.scale.xLabels.indexOf(this.value);
chart1.datasets.forEach(function (dataset) {
var selectedBar = dataset.bars[index];
// store the current values
selectedBar.selected = {
fillColor: selectedBar.fillColor,
strokeColor: selectedBar.strokeColor
}
// now set the color
selectedBar.fillColor = "red";
selectedBar.strokeColor = "red";
});
chart1.update();
}
document.getElementById("myInput").onchange = highlightBar.bind(document.getElementById("myInput"));
document.getElementById("myInput").onchange();
Specifically, lines 21 and the function starting on Line 66 in my fiddle. The function is taken from this question: How to highlight single bar in bar chart using Chartjs). The difference between this question and that question is that that question had a select dropdown input. I am, instead, trying to have an onkeyup dynamic input.
Thanks in advance