dygraphs

How to access original numberValueFormatter in dygraph?


I have a simple dygraph with a valueFormatter like this

axes: {
    y: {
        valueFormatter: function(val, opts, seriesName, dygraph, row, col) {
            if (seriesName === "Pass") {
                return val > 0 ? "Pass" : "Fail";
            } else {
                var x = this.attrs_.axes.y.valueFormatter(val, opts);
                return x;
            }
        },
    }
},

I want to call the original utils.numberValueFormatter from dygraph-utils for everything except the "Pass" series, but the above is the only way I could access it.

Is there a better/official way to call the default formatters?

EDIT:
After some tinkering I managed to get rid of the long reference chain and store the undamaged original numberValueFormatter in the graph.

Some more code for context.

g = new Dygraph(
    document.getElementById("graphdiv"),
    TheDataForDygraph,
    {
        series: {
            Pass: {
                drawPointCallback: Dygraph.Circles.HEXAGON,
                pointSize: 5,
                drawHighlightPointCallback: Dygraph.Circles.STAR,
                highlightCircleSize: 3,
            },
        },
        axes: {
            y: {
                valueFormatter: function(val, opts, seriesName, dygraph, row, col) {
                    if (seriesName === "Pass") {
                        return val > 0 ? "Pass" : "Fail";
                    } else {
                        var x = this.numberValueFormatter(val, opts);
                        return x;
                    }
                },
            }
        },
        labels: TheLabelsForDygraph,
        ylabel: 'Measurements',
    }
);

//Dirty hack, I don't know how else to get the original formatter.
g.numberValueFormatter = g.attrs_.axes.y.valueFormatter;

Solution

  • You could try getOptionForAxis:

    const valueFormatter = g.getOptionForAxis('valueFormatter', 'y');
    

    To make sure it's the original, you could use a fresh object:

    const valueFormatter = new Dygraph(
      document.createElement('div'), [[0]]
    ).getOptionForAxis('valueFormatter', 'y');
    

    Alternatively, if you're in an ES module context, you could try importing the symbol directly:

    import {numberValueFormatter} from 'dygraphs/src/dygraph-utils';