ecomfe / echarts-stat

Statistics tool for Apache ECharts
597 stars 309 forks source link

ecstat 使用 histogram时,计算分组起始值时 存在0.1+0.2 != 0.3的 #34

Open zhang867dong opened 3 years ago

zhang867dong commented 3 years ago

如源代码中出现的rangeArray[i - 1] + step,

`function computeBins(data, optOrMethod) { var opt = typeof optOrMethod === 'string' ? { method: optOrMethod } : (optOrMethod || {});

    var threshold = opt.method == null
        ? thresholdMethod.squareRoot
        : thresholdMethod[opt.method];
    var dimensions = normalizeDimensions(opt.dimensions);

    var values = dataPreprocess(data, {
        dimensions: dimensions,
        toOneDimensionArray: true
    });
    var maxValue = max(values);
    var minValue = min(values);
    var binsNumber = threshold(values, minValue, maxValue);
    var tickStepResult = tickStep(minValue, maxValue, binsNumber);
    var step = tickStepResult.step;
    var toFixedPrecision = tickStepResult.toFixedPrecision;

    // return the xAxis coordinate for each bins, except the end point of the value
    var rangeArray = range(
        // use function toFixed() to avoid data like '0.700000001'
        +((Math.ceil(minValue / step) * step).toFixed(toFixedPrecision)),
        +((Math.floor(maxValue / step) * step).toFixed(toFixedPrecision)),
        step,
        toFixedPrecision
    );

    var len = rangeArray.length;

    var bins = new Array(len + 1);

    for (var i = 0; i <= len; i++) {
        bins[i] = {};
        bins[i].sample = [];
        bins[i].x0 = i > 0
            ? rangeArray[i - 1]
            : (rangeArray[i] - minValue) === step
            ? minValue
            : _(rangeArray[i] - step);_
        bins[i].x1 = i < len
            ? rangeArray[i]
            : (maxValue - rangeArray[i-1]) === step
            ? maxValue
            : _rangeArray[i - 1] + step;_
    }

    for (var i = 0; i < values.length; i++) {
        if (minValue <= values[i] && values[i] <= maxValue) {
            bins[bisect(rangeArray, values[i], 0, len)].sample.push(values[i]);
        }
    }

    var data = map(bins, function (bin) {
        // use function toFixed() to avoid data like '6.5666638489'
        return [
            +((bin.x0 + bin.x1) / 2).toFixed(toFixedPrecision),
            bin.sample.length,
            bin.x0,
            bin.x1,
            bin.x0 + ' - ' + bin.x1
        ];
    });

    var customData = map(bins, function (bin) {
        return [bin.x0, bin.x1, bin.sample.length];
    });

    return {
        bins: bins,
        data: data,
        customData: customData
    };
}`