Syntax cleanup: fix undeclared variables

Fix undeclared and unintentional global variables by explicitly
declaring the variables or replacing with the intended variable. The
only case of the latter with functional implications is from
`graph_plot.js`, where an index from a nested filter was being used in
the outer filter instead of the index for that scope.