aboutsummaryrefslogtreecommitdiff
path: root/src/client/northstar/model/binRanges/QuantitativeVisualBinRange.ts
blob: c579c8e5ff07bc5c6f331731b07639b48453f39d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { QuantitativeBinRange } from '../idea/idea';
import { VisualBinRange } from './VisualBinRange';
import { format } from "d3-format";

export class QuantitativeVisualBinRange extends VisualBinRange {

    public DataBinRange: QuantitativeBinRange;

    constructor(dataBinRange: QuantitativeBinRange) {
        super();
        this.DataBinRange = dataBinRange;
    }

    public AddStep(value: number): number {
        return value + this.DataBinRange.step!;
    }

    public GetValueFromIndex(index: number): number {
        return this.DataBinRange.minValue! + (index * this.DataBinRange.step!);
    }

    public GetLabel(value: number): string {
        return QuantitativeVisualBinRange.NumberFormatter(value);
    }

    public static NumberFormatter(val: number): string {
        if (val === 0) {
            return "0";
        }
        if (val < 1) {
            /*if (val < Math.abs(0.001))  {
                return val.toExponential(2);
            }*/
            return format(".3")(val);
        }
        return format("~s")(val);
    }

    public GetBins(): number[] {
        let bins = new Array<number>();

        for (let v: number = this.DataBinRange.minValue!; v < this.DataBinRange.maxValue!; v += this.DataBinRange.step!) {
            bins.push(v);
        }
        return bins;
    }

    public static Initialize(dataMinValue: number, dataMaxValue: number, targetBinNumber: number, isIntegerRange: boolean): QuantitativeVisualBinRange {
        let extent = QuantitativeVisualBinRange.getExtent(dataMinValue, dataMaxValue, targetBinNumber, isIntegerRange);
        let dataBinRange = new QuantitativeBinRange();
        dataBinRange.minValue = extent[0];
        dataBinRange.maxValue = extent[1];
        dataBinRange.step = extent[2];

        return new QuantitativeVisualBinRange(dataBinRange);
    }

    private static getExtent(dataMin: number, dataMax: number, m: number, isIntegerRange: boolean): number[] {
        if (dataMin === dataMax) {
            // dataMin -= 0.1;
            dataMax += 0.1;
        }
        let span = dataMax - dataMin;

        let step = Math.pow(10, Math.floor(Math.log10(span / m)));
        let err = m / span * step;

        if (err <= .15) {
            step *= 10;
        }
        else if (err <= .35) {
            step *= 5;
        }
        else if (err <= .75) {
            step *= 2;
        }

        if (isIntegerRange) {
            step = Math.ceil(step);
        }
        let ret: number[] = new Array<number>(3);
        let minDivStep = Math.floor(dataMin / step);
        let maxDivStep = Math.floor(dataMax / step);
        ret[0] = minDivStep * step; // Math.floor(Math.Round(dataMin, 8)/step)*step;
        ret[1] = maxDivStep * step + step; // Math.floor(Math.Round(dataMax, 8)/step)*step + step;
        ret[2] = step;

        return ret;
    }
}