gridviz 3.0.15 → 3.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +127 -128
  2. package/dist/gridviz.js +7958 -7861
  3. package/dist/gridviz.min.js +1 -1
  4. package/package.json +74 -74
  5. package/src/button/ZoomButtons.js +101 -96
  6. package/src/core/Dataset.js +154 -157
  7. package/src/core/Drawable.js +60 -61
  8. package/src/core/GeoCanvas.js +409 -370
  9. package/src/core/Layer.js +23 -25
  10. package/src/core/Legend.js +108 -110
  11. package/src/core/Map.js +494 -495
  12. package/src/core/MultiResolutionDataset.js +93 -97
  13. package/src/core/Style.js +98 -99
  14. package/src/dataset/CSVGrid.js +112 -112
  15. package/src/dataset/GeoTIFF.js +122 -123
  16. package/src/dataset/JSGrid.js +55 -54
  17. package/src/dataset/TiledGrid.js +289 -288
  18. package/src/dataset/worker.js +18 -0
  19. package/src/index.js +77 -71
  20. package/src/layer/BackgroundLayer.js +162 -158
  21. package/src/layer/BackgroundLayerImage.js +81 -84
  22. package/src/layer/BackgroundLayerWMS.js +104 -108
  23. package/src/layer/GeoJSONLayer.js +214 -220
  24. package/src/layer/GridLayer.js +148 -157
  25. package/src/layer/LabelLayer.js +194 -193
  26. package/src/legend/ColorCategoryLegend.js +109 -110
  27. package/src/legend/ColorDiscreteLegend.js +100 -102
  28. package/src/legend/ColorLegend.js +115 -118
  29. package/src/legend/OrientationLegend.js +89 -91
  30. package/src/legend/SizeLegend.js +224 -224
  31. package/src/legend/TernaryLegend.js +302 -210
  32. package/src/style/CompositionStyle.js +291 -288
  33. package/src/style/DotDensityStyle.js +133 -134
  34. package/src/style/ImageStyle.js +100 -102
  35. package/src/style/IsoFenceStyle.js +217 -203
  36. package/src/style/JoyPlotStyle.js +127 -128
  37. package/src/style/LegoStyle.js +110 -110
  38. package/src/style/MosaicStyle.js +99 -99
  39. package/src/style/NinjaStarStyle.js +102 -102
  40. package/src/style/PillarStyle.js +207 -208
  41. package/src/style/SegmentStyle.js +105 -105
  42. package/src/style/ShapeColorSizeStyle.js +147 -132
  43. package/src/style/SideCategoryStyle.js +112 -114
  44. package/src/style/SideStyle.js +212 -205
  45. package/src/style/SquareColorCategoryWebGLStyle.js +107 -108
  46. package/src/style/SquareColorWebGLStyle.js +126 -126
  47. package/src/style/StrokeStyle.js +135 -136
  48. package/src/style/TanakaStyle.js +69 -68
  49. package/src/style/TextStyle.js +110 -111
  50. package/src/style/TimeSeriesStyle.js +222 -229
  51. package/src/utils/scale.js +218 -228
  52. package/src/utils/stretching.js +120 -133
  53. package/src/utils/ternary.js +203 -204
  54. package/src/utils/utils.js +58 -63
  55. package/src/utils/webGLUtils.js +72 -70
@@ -1,228 +1,218 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
- import { extent, max } from "d3-array"
5
- import { scaleQuantile } from "d3-scale"
6
-
7
- /**
8
- * A scale is simply a function that map a domain to a range.
9
- * @typedef {function(number):number} Scale */
10
-
11
- /**
12
- * A scale whose range is a color (string).
13
- * @typedef {function(number):string} ColorScale */
14
-
15
-
16
- /**
17
- * @module utils
18
- */
19
-
20
-
21
- /**
22
- * Generic function for view scale - continuous or discrete
23
- *
24
- * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, minValue?:number, minSizePix?:number, maxSizeFactor?:number, range?:[number, number], domain?:[number, number], stretching?:function(number):number, classNumber?:number}} opts
25
- * @returns {function(Array.<import("../core/Dataset").Cell>):Scale}
26
- */
27
- export const viewScale = (opts) => {
28
- const valueFunction = opts.valueFunction
29
- const minValue = opts.minValue || 0
30
- const minSizePix = opts.minSizePix || 0
31
- const maxSizeFactor = opts.maxSizeFactor || 1
32
- const stretching = opts.stretching
33
- const range_ = opts.range
34
- const domain_ = opts.domain
35
- const classNumber = opts.classNumber
36
- return (cells, resolution, z) => {
37
- const domain = domain_ || [minValue, max(cells, valueFunction)]
38
- const range = range_ || [minSizePix * z, resolution * maxSizeFactor]
39
- const domainSize = domain[1] - domain[0], domainMin = domain[0]
40
- const rangeSize = range[1] - range[0], rangeMin = range[0]
41
- return t => {
42
- //scale to [0,1]
43
- t = (t - domainMin) / domainSize
44
- //stretch
45
- if (stretching) t = stretching(t)
46
- //classify
47
- if (classNumber) t = t == 1 ? 1 : Math.floor(t * (classNumber)) / (classNumber - 1)
48
- //scale to range
49
- return rangeMin + t * rangeSize
50
- }
51
- }
52
- }
53
-
54
-
55
-
56
- /**
57
- * Generic function for view scale - quantile
58
- *
59
- * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, classNumber?:number, minSizePix?:number, maxSizeFactor?:number }} opts
60
- * @returns {function(Array.<import("../core/Dataset").Cell>):Scale}
61
- */
62
- export const viewScaleQuantile = (opts) => {
63
- const valueFunction = opts.valueFunction
64
- const classNumber = opts.classNumber || 12
65
- const minSizePix = opts.minSizePix || 1
66
- const maxSizeFactor = opts.maxSizeFactor || 1
67
- const scale = scaleQuantile()
68
- return (cells, resolution, z) => {
69
- scale.domain(cells.map(valueFunction))
70
- const minSizeGeo = minSizePix * z, maxSizeGeo = resolution * maxSizeFactor
71
- scale.range(Array.from({ length: classNumber }, (_, i) => minSizeGeo + i * (maxSizeGeo - minSizeGeo) / (classNumber - 1)))
72
- scale.breaks = scale.quantiles()
73
- scale.values = scale.range()
74
- return scale;
75
- }
76
- }
77
-
78
-
79
-
80
-
81
-
82
-
83
- /**
84
- * Generic function for color view scale - continuous or discrete
85
- *
86
- * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, colorScale?:function(number):string, colors?:Array.<string>, stretching?:function(number):number }} opts
87
- * @returns {function(Array.<import("../core/Dataset").Cell>):ColorScale}
88
- */
89
- export const viewScaleColor = (opts) => {
90
- const valueFunction = opts.valueFunction
91
- const stretching = opts.stretching
92
- let colorScale = opts.colorScale || (() => "purple")
93
-
94
- //discrete colors case: build continuous color scale from discrete ones.
95
- const nbClass = opts.colors?.length
96
- if (opts.colors && nbClass)
97
- colorScale = t => opts.colors[t == 1 ? nbClass - 1 : Math.floor(t * nbClass)]
98
-
99
- return (cells) => {
100
- if (cells.length == 0 || !cells) return
101
- /** @type {[undefined, undefined] | [number, number]} */
102
- const domain = extent(cells, valueFunction)
103
- if (domain[0] == undefined) return
104
- const domainSize = domain[1] - domain[0]
105
- const scale = t => {
106
- //scale to [0,1]
107
- t = (t - domain[0]) / domainSize
108
- //stretch
109
- if (stretching) t = stretching(t)
110
- return colorScale(t)
111
- }
112
- //function that return the domain value from the [0,1] range.
113
- scale.invert = t => {
114
- if (stretching) t = stretching.invert(t)
115
- return domain[0] + t * domainSize
116
- }
117
- //discrete colors: return the breaks
118
- if (opts.colors && nbClass) {
119
- scale.breaks = []
120
- for (let i = 1; i < nbClass; i++)
121
- scale.breaks.push(scale.invert(i / nbClass))
122
- }
123
-
124
- return scale;
125
- }
126
- }
127
-
128
-
129
-
130
-
131
-
132
- /**
133
- * Generic function for color view scale - quantile
134
- *
135
- * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, classNumber?:number, colors?:Array.<string>, colorScale?:function(number):string }} opts
136
- * @returns {function(Array.<import("../core/Dataset").Cell>):ColorScale}
137
- */
138
- export const viewScaleColorQuantile = (opts) => {
139
- const valueFunction = opts.valueFunction
140
- const classNumber = opts.classNumber || 12
141
-
142
- let colors = opts.colors
143
- if (opts.colorScale) colors = discreteColors(opts.colorScale, classNumber)
144
- colors = colors || Array.from({ length: classNumber }, (_, i) => "rgb(" + Math.floor(255 * i / (classNumber - 1)) + ",150,150)")
145
-
146
- const scale = scaleQuantile().range(colors)
147
- return (cells) => {
148
- scale.domain(cells.map(valueFunction));
149
- scale.breaks = scale.quantiles()
150
- scale.colors = colors
151
- return scale;
152
- }
153
- }
154
-
155
-
156
-
157
-
158
- /**
159
- * Combine view scale functions
160
- *
161
- * @param {*} obj
162
- * @returns {function}
163
- */
164
- export const viewScaleCombination = (obj) => {
165
- //obj: prop and a function to call
166
- return (cells, resolution, z) => {
167
- const out = {}
168
- for (const p in obj) { out[p] = obj[p](cells, resolution, z) }
169
- return out
170
- }
171
- }
172
-
173
-
174
-
175
-
176
-
177
-
178
- /**
179
- * Return a classifier function from break values.
180
- * The classifier function returns the class id (from 0 to breaks.length) from a value to classifiy.
181
- * @param {Array.<number>} breaks the breaks
182
- */
183
- export function classifier(breaks) {
184
- const bl = breaks.length
185
- const classifier = value => {
186
- let i = 0
187
- while (i < bl) {
188
- const break_ = breaks[i]
189
- if (value <= break_) return i
190
- i++
191
- }
192
- return i
193
- }
194
- classifier.breaks = breaks
195
- return classifier
196
- }
197
-
198
-
199
-
200
- /**
201
- * Return a color classifier function from break values.
202
- * The classifier function returns the color from a value to classifiy.
203
- * There should be one color more than break values.
204
- * @param {Array.<number>} breaks the breaks
205
- * @param {Array.<string>} colors the colors
206
- */
207
- export function colorClassifier(breaks, colors) {
208
- const classifier_ = classifier(breaks)
209
- const colorClassifier = value => colors[classifier_(value)]
210
- colorClassifier.breaks = breaks
211
- colorClassifier.colors = colors
212
- return colorClassifier
213
- }
214
-
215
- /**
216
- * Make array of colors from a colorScale.
217
- * It is a kind of sampling, or un-interpolation
218
- *
219
- * @param {function(number):string} colorScale
220
- * @param {number} nb
221
- */
222
- export function discreteColors(colorScale, nb) {
223
- if (nb == 1) return [colorScale(0.5)]
224
- const out = []
225
- for (let i = 0; i < nb; i++)
226
- out.push(colorScale(i / (nb - 1)))
227
- return out
228
- }
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { extent, max } from 'd3-array'
5
+ import { scaleQuantile } from 'd3-scale'
6
+
7
+ /**
8
+ * A scale is simply a function that map a domain to a range.
9
+ * @typedef {function(number):number} Scale */
10
+
11
+ /**
12
+ * A scale whose range is a color (string).
13
+ * @typedef {function(number):string} ColorScale */
14
+
15
+ /**
16
+ * @module utils
17
+ */
18
+
19
+ /**
20
+ * Generic function for view scale - continuous or discrete
21
+ *
22
+ * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, minValue?:number, minSizePix?:number, maxSizeFactor?:number, range?:[number, number], domain?:[number, number], stretching?:function(number):number, classNumber?:number}} opts
23
+ * @returns {function(Array.<import("../core/Dataset").Cell>):Scale}
24
+ */
25
+ export const viewScale = (opts) => {
26
+ const valueFunction = opts.valueFunction
27
+ const minValue = opts.minValue || 0
28
+ const minSizePix = opts.minSizePix || 0
29
+ const maxSizeFactor = opts.maxSizeFactor || 1
30
+ const stretching = opts.stretching
31
+ const range_ = opts.range
32
+ const domain_ = opts.domain
33
+ const classNumber = opts.classNumber
34
+ return (cells, resolution, z) => {
35
+ const domain = domain_ || [minValue, max(cells, valueFunction)]
36
+ const range = range_ || [minSizePix * z, resolution * maxSizeFactor]
37
+ const domainSize = domain[1] - domain[0],
38
+ domainMin = domain[0]
39
+ const rangeSize = range[1] - range[0],
40
+ rangeMin = range[0]
41
+ return (t) => {
42
+ //scale to [0,1]
43
+ t = (t - domainMin) / domainSize
44
+ //stretch
45
+ if (stretching) t = stretching(t)
46
+ //classify
47
+ if (classNumber) t = t == 1 ? 1 : Math.floor(t * classNumber) / (classNumber - 1)
48
+ //scale to range
49
+ return rangeMin + t * rangeSize
50
+ }
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Generic function for view scale - quantile
56
+ *
57
+ * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, classNumber?:number, minSizePix?:number, maxSizeFactor?:number }} opts
58
+ * @returns {function(Array.<import("../core/Dataset").Cell>):Scale}
59
+ */
60
+ export const viewScaleQuantile = (opts) => {
61
+ const valueFunction = opts.valueFunction
62
+ const classNumber = opts.classNumber || 12
63
+ const minSizePix = opts.minSizePix || 1
64
+ const maxSizeFactor = opts.maxSizeFactor || 1
65
+ const scale = scaleQuantile()
66
+ return (cells, resolution, z) => {
67
+ scale.domain(cells.map(valueFunction))
68
+ const minSizeGeo = minSizePix * z,
69
+ maxSizeGeo = resolution * maxSizeFactor
70
+ scale.range(
71
+ Array.from(
72
+ { length: classNumber },
73
+ (_, i) => minSizeGeo + (i * (maxSizeGeo - minSizeGeo)) / (classNumber - 1)
74
+ )
75
+ )
76
+ scale.breaks = scale.quantiles()
77
+ scale.values = scale.range()
78
+ return scale
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Generic function for color view scale - continuous or discrete
84
+ *
85
+ * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, colorScale?:function(number):string, colors?:Array.<string>, stretching?:function(number):number }} opts
86
+ * @returns {function(Array.<import("../core/Dataset").Cell>):ColorScale}
87
+ */
88
+ export const viewScaleColor = (opts) => {
89
+ const valueFunction = opts.valueFunction
90
+ const stretching = opts.stretching
91
+ let colorScale = opts.colorScale || (() => 'purple')
92
+
93
+ //discrete colors case: build continuous color scale from discrete ones.
94
+ const nbClass = opts.colors?.length
95
+ if (opts.colors && nbClass)
96
+ colorScale = (t) => opts.colors[t == 1 ? nbClass - 1 : Math.floor(t * nbClass)]
97
+
98
+ return (cells) => {
99
+ if (cells.length == 0 || !cells) return
100
+ /** @type {[undefined, undefined] | [number, number]} */
101
+ const domain = extent(cells, valueFunction)
102
+ if (domain[0] == undefined) return
103
+ const domainSize = domain[1] - domain[0]
104
+ const scale = (t) => {
105
+ //scale to [0,1]
106
+ t = (t - domain[0]) / domainSize
107
+ //stretch
108
+ if (stretching) t = stretching(t)
109
+ return colorScale(t)
110
+ }
111
+ //function that return the domain value from the [0,1] range.
112
+ scale.invert = (t) => {
113
+ if (stretching) t = stretching.invert(t)
114
+ return domain[0] + t * domainSize
115
+ }
116
+ //discrete colors: return the breaks
117
+ if (opts.colors && nbClass) {
118
+ scale.breaks = []
119
+ for (let i = 1; i < nbClass; i++) scale.breaks.push(scale.invert(i / nbClass))
120
+ }
121
+
122
+ return scale
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Generic function for color view scale - quantile
128
+ *
129
+ * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, classNumber?:number, colors?:Array.<string>, colorScale?:function(number):string }} opts
130
+ * @returns {function(Array.<import("../core/Dataset").Cell>):ColorScale}
131
+ */
132
+ export const viewScaleColorQuantile = (opts) => {
133
+ const valueFunction = opts.valueFunction
134
+ const classNumber = opts.classNumber || 12
135
+
136
+ let colors = opts.colors
137
+ if (opts.colorScale) colors = discreteColors(opts.colorScale, classNumber)
138
+ colors =
139
+ colors ||
140
+ Array.from(
141
+ { length: classNumber },
142
+ (_, i) => 'rgb(' + Math.floor((255 * i) / (classNumber - 1)) + ',150,150)'
143
+ )
144
+
145
+ const scale = scaleQuantile().range(colors)
146
+ return (cells) => {
147
+ scale.domain(cells.map(valueFunction))
148
+ scale.breaks = scale.quantiles()
149
+ scale.colors = colors
150
+ return scale
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Combine view scale functions
156
+ *
157
+ * @param {*} obj
158
+ * @returns {function}
159
+ */
160
+ export const viewScaleCombination = (obj) => {
161
+ //obj: prop and a function to call
162
+ return (cells, resolution, z) => {
163
+ const out = {}
164
+ for (const p in obj) {
165
+ out[p] = obj[p](cells, resolution, z)
166
+ }
167
+ return out
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Return a classifier function from break values.
173
+ * The classifier function returns the class id (from 0 to breaks.length) from a value to classifiy.
174
+ * @param {Array.<number>} breaks the breaks
175
+ */
176
+ export function classifier(breaks) {
177
+ const bl = breaks.length
178
+ const classifier = (value) => {
179
+ let i = 0
180
+ while (i < bl) {
181
+ const break_ = breaks[i]
182
+ if (value <= break_) return i
183
+ i++
184
+ }
185
+ return i
186
+ }
187
+ classifier.breaks = breaks
188
+ return classifier
189
+ }
190
+
191
+ /**
192
+ * Return a color classifier function from break values.
193
+ * The classifier function returns the color from a value to classifiy.
194
+ * There should be one color more than break values.
195
+ * @param {Array.<number>} breaks the breaks
196
+ * @param {Array.<string>} colors the colors
197
+ */
198
+ export function colorClassifier(breaks, colors) {
199
+ const classifier_ = classifier(breaks)
200
+ const colorClassifier = (value) => colors[classifier_(value)]
201
+ colorClassifier.breaks = breaks
202
+ colorClassifier.colors = colors
203
+ return colorClassifier
204
+ }
205
+
206
+ /**
207
+ * Make array of colors from a colorScale.
208
+ * It is a kind of sampling, or un-interpolation
209
+ *
210
+ * @param {function(number):string} colorScale
211
+ * @param {number} nb
212
+ */
213
+ export function discreteColors(colorScale, nb) {
214
+ if (nb == 1) return [colorScale(0.5)]
215
+ const out = []
216
+ for (let i = 0; i < nb; i++) out.push(colorScale(i / (nb - 1)))
217
+ return out
218
+ }