static computeValueGrid()

in resources/perf.webkit.org/public/v3/components/time-series-chart.js [775:814]


    static computeValueGrid(min, max, maxLabels, formatter)
    {
        const diff = max - min;
        if (!diff)
            return [];

        const diffPerLabel = diff / maxLabels;

        // First, reduce the diff between 1 and 1000 using a power of 1000 or 1024.
        // FIXME: Share this code with Metric.makeFormatter.
        const maxAbsValue = Math.max(Math.abs(min), Math.abs(max));
        let scalingFactor = 1;
        const divisor = formatter.divisor;
        while (maxAbsValue * scalingFactor < 1)
            scalingFactor *= formatter.divisor;
        while (maxAbsValue * scalingFactor > divisor)
            scalingFactor /= formatter.divisor;
        const scaledDiff = diffPerLabel * scalingFactor;

        // Second, compute the smallest number greater than the scaled diff
        // which is a product of a power of 10, 2, and 5.
        // These numbers are all factors of the decimal numeral system, 10.
        const digitsInScaledDiff = Math.ceil(Math.log(scaledDiff) / Math.log(10));
        let step = Math.pow(10, digitsInScaledDiff);
        if (step / 5 >= scaledDiff)
            step /= 5; // The most significant digit is 2
        else if (step / 2 >= scaledDiff)
            step /= 2 // The most significant digit is 5
        step /= scalingFactor;

        const gridValues = [];
        let currentValue = Math.ceil(min / step) * step;
        while (currentValue <= max) {
            let unscaledValue = currentValue;
            gridValues.push({value: unscaledValue, label: formatter(unscaledValue, maxAbsValue)});
            currentValue += step;
        }

        return gridValues;
    }