* Large data down sampling using largest-triangle-three-buckets * @param {string} valueDimension * @param {number} targetCount
(
valueDimension: DimensionIndex,
rate: number
)
| 868 | * @param {number} targetCount |
| 869 | */ |
| 870 | lttbDownSample( |
| 871 | valueDimension: DimensionIndex, |
| 872 | rate: number |
| 873 | ): DataStore { |
| 874 | const target = this.clone([valueDimension], true); |
| 875 | const targetStorage = target._chunks; |
| 876 | const dimStore = targetStorage[valueDimension]; |
| 877 | const len = this.count(); |
| 878 | |
| 879 | let sampledIndex = 0; |
| 880 | |
| 881 | const frameSize = Math.floor(1 / rate); |
| 882 | |
| 883 | let currentRawIndex = this.getRawIndex(0); |
| 884 | let maxArea; |
| 885 | let area; |
| 886 | let nextRawIndex; |
| 887 | |
| 888 | const newIndices = new (getIndicesCtor(this._rawCount))(Math.min((Math.ceil(len / frameSize) + 2) * 2, len)); |
| 889 | |
| 890 | // First frame use the first data. |
| 891 | newIndices[sampledIndex++] = currentRawIndex; |
| 892 | for (let i = 1; i < len - 1; i += frameSize) { |
| 893 | const nextFrameStart = Math.min(i + frameSize, len - 1); |
| 894 | const nextFrameEnd = Math.min(i + frameSize * 2, len); |
| 895 | |
| 896 | const avgX = (nextFrameEnd + nextFrameStart) / 2; |
| 897 | let avgY = 0; |
| 898 | |
| 899 | for (let idx = nextFrameStart; idx < nextFrameEnd; idx++) { |
| 900 | const rawIndex = this.getRawIndex(idx); |
| 901 | const y = dimStore[rawIndex] as number; |
| 902 | if (isNaN(y)) { |
| 903 | continue; |
| 904 | } |
| 905 | avgY += y as number; |
| 906 | } |
| 907 | avgY /= (nextFrameEnd - nextFrameStart); |
| 908 | |
| 909 | const frameStart = i; |
| 910 | const frameEnd = Math.min(i + frameSize, len); |
| 911 | |
| 912 | const pointAX = i - 1; |
| 913 | const pointAY = dimStore[currentRawIndex] as number; |
| 914 | |
| 915 | maxArea = -1; |
| 916 | |
| 917 | nextRawIndex = frameStart; |
| 918 | |
| 919 | let firstNaNIndex = -1; |
| 920 | let countNaN = 0; |
| 921 | // Find a point from current frame that construct a triangle with largest area with previous selected point |
| 922 | // And the average of next frame. |
| 923 | for (let idx = frameStart; idx < frameEnd; idx++) { |
| 924 | const rawIndex = this.getRawIndex(idx); |
| 925 | const y = dimStore[rawIndex] as number; |
| 926 | if (isNaN(y)) { |
| 927 | countNaN++; |
no test coverage detected