I have a google scatterchart that I want to change the color of a point if I click on it, and to reset the points to their original color if I don't click on any point (e.g. if I click on the background).
From what I see on the api, clicking the background should fire a select event that returns a null selection. However, my select handler only ever gets called if I click on a point, not if I click on the graph's background. (I can tell by looking at the console output --- it should print "A" if my handler fires --- as well as the graph.) Clicking anywhere other than on a point doesn't fire the handler at all.
How can I get if I'm clicking on a non-point part of the graph or not?
function makeGraph(row_data, x_axis_title, y_axis_title) {
var data = new google.visualization.DataTable();
data.addColumn('number', x_axis_title);
data.addColumn('number', y_axis_title); // Required to be a number
data.addColumn( {'type': 'string', 'role': 'style'} ); // Defines point style
let pointStyle = 'point { size: 12; shape-type: circle; fill-color: #FFFFFF; color: #CCCCCC }';
let focusedPointStyle = 'point { size: 14; shape-type: circle; fill-color: #B5D989; color: #CCCCCC }';
for (let [index, row_data_i] of Object.entries(row_data)) {
row_data_i[2] = pointStyle;
}
data.addRows(row_data);
var options = {
chart: { title: "myGraph" }
};
var chart = new google.visualization.ScatterChart(document.getElementById(div_id_for_graph));
// SELECT HANDLER
function selectHandler() {
console.log("A");
let selected_graph_item = chart.getSelection()[0];
// Need to reset all points to standard formatting
for (let i = 0; i < row_data.length; i++)
data.setCell(i, 3, pointStyle);
if (selected_graph_item === undefined) { // Didn't click on a point, but on blank graph space
pass;
}
else {
data.setCell(selected_graph_item["row"], 3, focusedPointStyle);
}
chart.draw(data, options); // Redraw so point coloring gets updated
}
// Listen for the 'select' event, and call my function selectHandler() when
// the user selects something on the chart.
google.visualization.events.addListener(chart, 'select', selectHandler);
// DRAW CHART
chart.draw(data, options);
}