File size: 2,574 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { useCallback } from 'react';
import type uPlot from 'uplot';

export type ScaleGradientFunction = (
	u: uPlot,
	scaleKey: string,
	ori: number,
	scaleStops: [ number, string ][],
	discrete?: boolean
) => CanvasRenderingContext2D[ 'fillStyle' ];

export default function useScaleGradient( backupColor: string ) {
	return useCallback< ScaleGradientFunction >(
		( u, scaleKey, ori, scaleStops, discrete = false ) => {
			const ctx = document.createElement( 'canvas' ).getContext( '2d' );

			const scale = u.scales[ scaleKey ];
			// we want the stop below or at the scaleMax
			// and the stop below or at the scaleMin, else the stop above scaleMin
			let minStopIdx;
			let maxStopIdx;

			if ( scale.min === undefined ) {
				scale.min = 0;
			}

			if ( scale.max === undefined ) {
				scale.max = 0;
			}

			for ( let i = 0; i < scaleStops.length; i++ ) {
				const stopVal = scaleStops[ i ][ 0 ];

				if ( stopVal <= scale.min || minStopIdx == null ) {
					minStopIdx = i;
				}

				maxStopIdx = i;

				if ( stopVal >= scale.max ) {
					break;
				}
			}

			if ( minStopIdx === undefined ) {
				minStopIdx = 0;
			}

			if ( maxStopIdx === undefined ) {
				maxStopIdx = 0;
			}

			if ( minStopIdx === maxStopIdx ) {
				return scaleStops[ minStopIdx ][ 1 ];
			}

			let minStopVal = scaleStops[ minStopIdx ][ 0 ];
			let maxStopVal = scaleStops[ maxStopIdx ][ 0 ];

			if ( minStopVal === -Infinity ) {
				minStopVal = scale.min;
			}

			if ( maxStopVal === Infinity ) {
				maxStopVal = scale.max;
			}

			const minStopPos = u.valToPos( minStopVal, scaleKey, true );
			const maxStopPos = u.valToPos( maxStopVal, scaleKey, true );

			const range = minStopPos - maxStopPos;

			let x0;
			let y0;
			let x1;
			let y1;

			if ( ori === 1 ) {
				x0 = x1 = 0;
				y0 = minStopPos;
				y1 = maxStopPos;
			} else {
				y0 = y1 = 0;
				x0 = minStopPos;
				x1 = maxStopPos;
			}

			const gradient = ctx?.createLinearGradient( x0, y0, x1, y1 );

			let prevColor;

			for ( let i = minStopIdx || 0; i <= maxStopIdx; i++ ) {
				const s = scaleStops[ i ];
				let stopPos;

				if ( i === minStopIdx ) {
					stopPos = minStopPos;
				} else {
					stopPos = i === maxStopIdx ? maxStopPos : u.valToPos( s[ 0 ], scaleKey, true );
				}

				const pct = ( minStopPos - stopPos ) / range; // % of max value

				if ( discrete && i > minStopIdx ) {
					gradient?.addColorStop( pct, prevColor || 'rgba(0, 0, 0, 0)' );
				}

				gradient?.addColorStop( pct, ( prevColor = s[ 1 ] ) );
			}

			return gradient || backupColor;
		},
		[ backupColor ]
	);
}