More refactoring for the js code

The code in charge of the graph has been encapsulated in a function that
returns a reference to the draw function.