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

Today’s goal was to come up with a reasonably automated way to specify formatting for the numeric values associated with the major tickmarks on each axis. I chose to implement settings for decimal places, overall precision width, and whether or not trailing zeros would be stripped. If the formatted string with decimal places is used unless it is longer than the formatted string at the specified precision, in which case the latter is used. Trailing zeroes are then stripped from each string.

This may result in different strings having different formats. First there’s always that one string that has fewer decimal places than all the others because it happens to end in a zero. Alternatively, some strings might be in fixed decimal while others may be in scientific notation. I therefore made a second pass that involved assigning consistent formatting based on the “worst” format of the bunch. If any value was in scientific notation I applied the toPrecision function to all values. If that didn’t apply, I applied the toFixed function so they all matched the value with the most decimal places. That could be annoying when a floating point operation leaves one string with a value like 12.0000001, as sometimes happen, but the extraneous digit would probably be to the right of whatever place the used specified to begin with. If all values were successfully stripped to the decimal point, the values are all displayed as whole numbers.

The first sieve, applied to individual values, is fairly quick. Remember that for the time being these functions are object methods. Once I get everything working I’ll clean it all up to hide the elements that should be private.

The next bit of code does the same thing to either the y- or the y-axis. It cycles through the values to generate a formatted string, then determines whether any string uses scientific notation using by search for the letter ‘e’, or whether any string includes trailing decimals by searching for the ‘.’ character. The latter search term is formatted as it is because the search function actually takes a regular expression as its parameter and the decimal point has a special meaning in regular expressions. Again, a second pass is made to apply the “worst” format consistently. The pixel with of each string is then calculated based on the prevailing font (which should be set just prior to this call) along with a value for the widest string in the group.

I experimented with the graphing tools in Excel to see how it formatted axis values under different combinations of conditions and found that, without going into an extreme amount of coding, it produces similar results in most cases. There is always more one could do, but this first pass is reasonably quick and effective. It often takes human intervention to get decent-looking labels anyway, this this approach seems good enough for now.

If you look carefully at the second block of code you might notice that it’s missing its ending brace. Tomorrow I’ll describe how the information I calculated today, along with the height and angle information, are used to figure out how much space is needed to draw all of the values.

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

Leave a Reply