canvas lineStyle spilling over between cells

I’m trying to write some tools for interactive data-viz for lazy scientists who refuse to learn anything new (notebook here) - more like matplotlib than most of the beautiful stuff that seems to get made on here - and I’m running into a problem I don’t fully understand.

I’ve written some basic code to add an x and y axis to the plot - and another routine which adds a line based on an array of x and y values. The latter can take an object containing some options for the plot like lineStyle, globalAlpha and lineWidth.

The problem occurs when I try to add more than one line - or just one line+the axes - to the plot. Perhaps easiest demonstrated in an image:

axes + two lines - one is supposed to be green and the other red, the axes are always black

it seems like maybe everything being evaluated at once is causing the lineStyle etc. of one plot command is spilling into the others. Any advice on what’s going on and how to alleviate it?

Your lineplot function is missing a call to ctx.beginPath(). You might also want to consider employing and ctx.restore() to prevent changes made to the context configuration from persisting through several function calls.

1 Like