Turning the Graph Function Into an Object in JavaScript, Part 4

Picking up from last week I’ve finished turning the JavaScript graphing widget into an object, the results of which are shown below. There is a long list of TODOs included in the code that is not close to being exhaustive; I’ll be working those off over the course of the next few posts.

The fun part for me was leveraging a functional parameter. I’d done that somewhere in the mists of time but it was nice to implement it here per my reading and have it work on the first try. See lines 374 and 379 below.

The current form of the graph is meant to support a single type of plot, which is the single curve in segmented colors as shown. I’ve set the object up so it plots the y-value at every pixel along the x-axis and draws lines from point to point after the initial moveTo call. The vagaries of the way pixels work on canvases and the fact that lines and characters are antialiased this technique yields a fairly thick plotted line.

I’ve broken the original functions down so the calculation of the pixel location on the graph is separated from the calculation of the effective value to plot, and that is separated from the function used to repeatedly call for the next point to be plotted. A package like Microsoft Excel plots only from a discrete set of values calculated in cells, and there is usually a fair amount of distance between plotted points on the x-axis. I’ve stubbed in a plotting call that draws specific figures at a specified point (a ‘+’ and an ‘X’ to start) but have not yet implemented anything that plots successive such values from a list. That functionality will definitely be included as I describe how I did the individual sections of curve fit and show the resulting function along with discrete control (x,y values from the steam tables used to derive the actual curve fit) and test (x,y values from the steam tables in the same range of x values but not used to derive the actual curve fit) points.

This entry was posted in Software and tagged , , . Bookmark the permalink.

Leave a Reply