MathML + SVG for graphing a function, part one

For years, I’ve wanted to think about using content MathML and SVG to plot a y = f(x) function on a pair of axes. The SVG part of this is not too hard (alas, I lost every copy of my SVG grapher years ago, and will have to rewrite it), but MathML is a whole different ball of wax.

So, with a little bit of thought (and a need to do something different than I’ve done recently), I came up with this: contentMathEval.js. This script takes a MathML content node and attempts to evaluate its value with a given set of variable inputs. It also offers a way to get a composite JavaScript function, equivalent to the MathML content node and its descendants.

Pretty cool, huh? It doesn’t cover all of MathML, not by a long shot. It’s a starting point, for the most basic of functions in MathML (specifically, those covered by ECMAScript’s Math object, and basic arithmetic).

I have a demo of contentMathEval.js here, and contentMathEval.js includes some internal documentation on how to use it. Most of it hasn’t been tested yet.

I don’t plan on making immediate improvements to this script, but I will take code contributions if anyone’s interested. Something more advanced (with support for my BigDecimal library, and probably derivatives and integrals) I will probably tackle at a future date.

4 thoughts on “MathML + SVG for graphing a function, part one”

  1. Hi,
    trying out your example, I get the following pop-up:
    ({x:3, Hi,
    trying out your example, I get the following pop-up:
    ({x:3, <:{value:undefined}}) 6 6 ({x:3, y:{value:6}}) ({x:4, y:{value:undefined}}) 13 ({x:4,y:{value:13}}) while mathTest01.xml only shows "xy23". Using Firefox (From Alex: That's expected. The source code of the XML file shows what it's testing.)

  2. Sorry for the beginners question. I have the following problem with other MathML links too.
    When I click on the link “a demo of contentMathEval.js” the only thing I see in the browser is an alert message box and then only the text “yx23” in the browser window. I’m using FF on Win/2k and have already installed the MathML fonts.
    I also have created some basic MathML docs and they work fine only when I name them with *.xhtml extension. If I use *.xml extension their display is not proper (I see only XML text – no any rendered MathML)
    Thanks in advance!
    (From Alex: Part of the reason is that the document is using content MathML, as opposed to what Firefox expects, which is presentation MathML. As for the .xml problem, you need to load the MathML style sheet.)

Comments are closed.