The short answer seems to be that you should capture the chart when it's created, so for example:
var chart;
nv.addGraph(function () {
chart = nv.models.lineChart();
//...
return chart;
});
// "at some point" - by the time an `on click' is called,
// `chart` will be usable.
If you need more rigour with the validity of chart
being defined,
the nv.addGraph()
function has an optional second argument that is a callback:
It will be called with the generated chart as its only argument. The callback is called immediately following graph generation, and before generation of any other graphs that may be scheduled for generation.
As described on the NVD3 wiki.
So this can be used to capture a reference or use it immediately:
const charts = [];
function capture(chart) {
charts.push(chart);
}
nv.addGraph(function () {
const chart = nv.models.lineChart();
//...
return chart;
},
capture);