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,132 +1,147 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
- import { Style } from '../core/Style.js'
5
-
6
- /**
7
- * A very generic style that shows grid cells with specific color, size and shape.
8
- * It can be used to show variables as cell colors, cell size, cell shape, or any combination of the three visual variables.
9
- *
10
- * @module style
11
- * @author Joseph Davies, Julien Gaffuri
12
- */
13
- export class ShapeColorSizeStyle extends Style {
14
- /** @param {object} opts */
15
- constructor(opts) {
16
- super(opts)
17
- opts = opts || {}
18
-
19
- /** A function returning the color of the cell.
20
- * @type {function(import('../core/Dataset.js').Cell, number, number, object):string} */
21
- this.color = opts.color || (() => "#EA6BAC") //(c,r,z,vs) => {}
22
-
23
- /** A function returning the size of a cell in geographical unit.
24
- * @type {function(import('../core/Dataset.js').Cell, number, number, object):number} */
25
- this.size = opts.size || ((cell, resolution) => resolution) //(c,r,z,vs) => {}
26
-
27
- /** A function returning the shape of a cell.
28
- * @type {function(import("../core/Dataset.js").Cell,number, number,object):import("../core/Style.js").Shape} */
29
- this.shape = opts.shape || (() => "square") //(c,r,z,vs) => {}
30
- }
31
-
32
- /**
33
- * Draw cells as squares, with various colors and sizes.
34
- *
35
- * @param {Array.<import("../core/Dataset.js").Cell>} cells
36
- * @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas
37
- * @param {number} resolution
38
- * @override
39
- */
40
- draw(cells, geoCanvas, resolution) {
41
-
42
- //filter
43
- if (this.filter) cells = cells.filter(this.filter)
44
-
45
- //zoom
46
- const z = geoCanvas.view.z
47
-
48
- //get view scale
49
- const viewScale = this.viewScale ? this.viewScale(cells, resolution, z) : undefined
50
-
51
- const r2 = resolution * 0.5
52
- for (let c of cells) {
53
- //color
54
- let col = this.color ? this.color(c, resolution, z, viewScale) : "black"
55
- if (!col || col === 'none') continue
56
-
57
- //size
58
- const size = this.size ? this.size(c, resolution, z, viewScale) : resolution
59
- if (!size) continue
60
-
61
- //shape
62
- const shape = this.shape ? this.shape(c, resolution, z, viewScale) : 'square'
63
- if (shape === 'none') continue
64
-
65
- //get offset
66
- const offset = this.offset(c, resolution, z)
67
-
68
- geoCanvas.ctx.fillStyle = col
69
- if (shape === 'square') {
70
- //draw square
71
- const d = resolution * (1 - size / resolution) * 0.5
72
- geoCanvas.ctx.fillRect(c.x + d + offset.dx, c.y + d + offset.dy, size, size)
73
- } else if (shape === 'circle') {
74
- //draw circle
75
- geoCanvas.ctx.beginPath()
76
- geoCanvas.ctx.arc(c.x + r2 + offset.dx, c.y + r2 + offset.dy, size * 0.5, 0, 2 * Math.PI, false)
77
- geoCanvas.ctx.fill()
78
- } else if (shape === 'donut') {
79
- //draw donut
80
- const xc = c.x + r2 + offset.dx,
81
- yc = c.y + r2 + offset.dy
82
- geoCanvas.ctx.beginPath()
83
- geoCanvas.ctx.moveTo(xc, yc)
84
- geoCanvas.ctx.arc(xc, yc, r2, 0, 2 * Math.PI)
85
- geoCanvas.ctx.arc(xc, yc, (1 - size / resolution) * r2, 0, 2 * Math.PI, true)
86
- geoCanvas.ctx.closePath()
87
- geoCanvas.ctx.fill()
88
- } else if (shape === 'diamond') {
89
- const s2 = size * 0.5
90
- geoCanvas.ctx.beginPath()
91
- geoCanvas.ctx.moveTo(c.x + r2 - s2, c.y + r2)
92
- geoCanvas.ctx.lineTo(c.x + r2, c.y + r2 + s2)
93
- geoCanvas.ctx.lineTo(c.x + r2 + s2, c.y + r2)
94
- geoCanvas.ctx.lineTo(c.x + r2, c.y + r2 - s2)
95
- geoCanvas.ctx.fill()
96
- } else if (shape === 'triangle_up') {
97
- const dr2 = (size - resolution) / 2
98
- geoCanvas.ctx.beginPath()
99
- geoCanvas.ctx.moveTo(c.x - dr2, c.y - dr2)
100
- geoCanvas.ctx.lineTo(c.x + r2, c.y + resolution + dr2)
101
- geoCanvas.ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
102
- geoCanvas.ctx.fill()
103
- } else if (shape === 'triangle_down') {
104
- const dr2 = (size - resolution) / 2
105
- geoCanvas.ctx.beginPath()
106
- geoCanvas.ctx.moveTo(c.x - dr2, c.y + resolution + dr2)
107
- geoCanvas.ctx.lineTo(c.x + r2, c.y - dr2)
108
- geoCanvas.ctx.lineTo(c.x + resolution + dr2, c.y + resolution + dr2)
109
- geoCanvas.ctx.fill()
110
- } else if (shape === 'triangle_left') {
111
- const dr2 = (size - resolution) / 2
112
- geoCanvas.ctx.beginPath()
113
- geoCanvas.ctx.moveTo(c.x + resolution + dr2, c.y + resolution + dr2)
114
- geoCanvas.ctx.lineTo(c.x - dr2, c.y + r2)
115
- geoCanvas.ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
116
- geoCanvas.ctx.fill()
117
- } else if (shape === 'triangle_right') {
118
- const dr2 = (size - resolution) / 2
119
- geoCanvas.ctx.beginPath()
120
- geoCanvas.ctx.moveTo(c.x - dr2, c.y - dr2)
121
- geoCanvas.ctx.lineTo(c.x + resolution + dr2, c.y + r2)
122
- geoCanvas.ctx.lineTo(c.x - dr2, c.y + resolution + dr2)
123
- geoCanvas.ctx.fill()
124
- } else {
125
- throw new Error('Unexpected shape:' + shape)
126
- }
127
- }
128
-
129
- //update legends
130
- this.updateLegends({ viewScale: viewScale, resolution: resolution, z: z, cells: cells })
131
- }
132
- }
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { Style } from '../core/Style.js'
5
+
6
+ /**
7
+ * A very generic style that shows grid cells with specific color, size and shape.
8
+ * It can be used to show variables as cell colors, cell size, cell shape, or any combination of the three visual variables.
9
+ *
10
+ * @module style
11
+ * @author Joseph Davies, Julien Gaffuri
12
+ */
13
+ export class ShapeColorSizeStyle extends Style {
14
+ /** @param {object} opts */
15
+ constructor(opts) {
16
+ super(opts)
17
+ opts = opts || {}
18
+ /** @type {(function(import('../core/Dataset.js').Cell, number, number, object):string) | string} */
19
+ this.color = opts.color || '#EA6BAC'
20
+
21
+ /** @type {(function(import('../core/Dataset.js').Cell, number, number, object):number) | number} */
22
+ this.size = opts.size || ((cell, resolution) => resolution)
23
+
24
+ /** @type {(function(import("../core/Dataset.js").Cell,number, number,object):import("../core/Style.js").Shape) | string} */
25
+ this.shape = opts.shape || 'square'
26
+ }
27
+
28
+ /**
29
+ * Draw cells as squares, with various colors and sizes.
30
+ *
31
+ * @param {Array.<import("../core/Dataset.js").Cell>} cells - The grid cells to draw.
32
+ * @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas - The canvas to draw on.
33
+ * @param {number} resolution - Resolution of the grid.
34
+ * @override
35
+ */
36
+ draw(cells, geoCanvas, resolution) {
37
+ //filter
38
+ if (this.filter) cells = cells.filter(this.filter)
39
+
40
+ //zoom
41
+ const z = geoCanvas.view.z
42
+
43
+ //get view scale
44
+ const viewScale = this.viewScale ? this.viewScale(cells, resolution, z) : undefined
45
+
46
+ const r2 = resolution * 0.5
47
+
48
+ // Precompute if color, size, and shape are functions, for efficiency
49
+ const isColorFunction = typeof this.color === 'function'
50
+ const isSizeFunction = typeof this.size === 'function'
51
+ const isShapeFunction = typeof this.shape === 'function'
52
+
53
+ const defaultColor = this.color || 'black'
54
+ const defaultSize = this.size || resolution
55
+ const defaultShape = this.shape || 'square'
56
+
57
+ // Optimized
58
+ const colorFunction = isColorFunction ? this.color : null
59
+ const sizeFunction = isSizeFunction ? this.size : null
60
+ const shapeFunction = isShapeFunction ? this.shape : null
61
+
62
+ for (let c of cells) {
63
+ // Determine color
64
+ //@ts-ignore
65
+ const col = colorFunction ? colorFunction(c, resolution, z, viewScale) : defaultColor
66
+ if (!col || col === 'none') continue
67
+
68
+ // Determine size
69
+ //@ts-ignore
70
+ const size = sizeFunction ? sizeFunction(c, resolution, z, viewScale) : defaultSize
71
+ if (!size) continue
72
+
73
+ // Determine shape
74
+ //@ts-ignore
75
+ const shape = shapeFunction ? shapeFunction(c, resolution, z, viewScale) : defaultShape
76
+ if (shape === 'none') continue
77
+
78
+ //get offset
79
+ const offset = this.offset(c, resolution, z)
80
+
81
+ //get context
82
+ const ctx = geoCanvas.offscreenCtx
83
+ ctx.fillStyle = col
84
+ if (shape === 'square') {
85
+ //draw square
86
+ const d = resolution * (1 - size / resolution) * 0.5
87
+ ctx.fillRect(c.x + d + offset.dx, c.y + d + offset.dy, size, size)
88
+ } else if (shape === 'circle') {
89
+ //draw circle
90
+ ctx.beginPath()
91
+ ctx.arc(c.x + r2 + offset.dx, c.y + r2 + offset.dy, size * 0.5, 0, 2 * Math.PI, false)
92
+ ctx.fill()
93
+ } else if (shape === 'donut') {
94
+ //draw donut
95
+ const xc = c.x + r2 + offset.dx,
96
+ yc = c.y + r2 + offset.dy
97
+ ctx.beginPath()
98
+ ctx.moveTo(xc, yc)
99
+ ctx.arc(xc, yc, r2, 0, 2 * Math.PI)
100
+ ctx.arc(xc, yc, (1 - size / resolution) * r2, 0, 2 * Math.PI, true)
101
+ ctx.closePath()
102
+ ctx.fill()
103
+ } else if (shape === 'diamond') {
104
+ const s2 = size * 0.5
105
+ ctx.beginPath()
106
+ ctx.moveTo(c.x + r2 - s2, c.y + r2)
107
+ ctx.lineTo(c.x + r2, c.y + r2 + s2)
108
+ ctx.lineTo(c.x + r2 + s2, c.y + r2)
109
+ ctx.lineTo(c.x + r2, c.y + r2 - s2)
110
+ ctx.fill()
111
+ } else if (shape === 'triangle_up') {
112
+ const dr2 = (size - resolution) / 2
113
+ ctx.beginPath()
114
+ ctx.moveTo(c.x - dr2, c.y - dr2)
115
+ ctx.lineTo(c.x + r2, c.y + resolution + dr2)
116
+ ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
117
+ ctx.fill()
118
+ } else if (shape === 'triangle_down') {
119
+ const dr2 = (size - resolution) / 2
120
+ ctx.beginPath()
121
+ ctx.moveTo(c.x - dr2, c.y + resolution + dr2)
122
+ ctx.lineTo(c.x + r2, c.y - dr2)
123
+ ctx.lineTo(c.x + resolution + dr2, c.y + resolution + dr2)
124
+ ctx.fill()
125
+ } else if (shape === 'triangle_left') {
126
+ const dr2 = (size - resolution) / 2
127
+ ctx.beginPath()
128
+ ctx.moveTo(c.x + resolution + dr2, c.y + resolution + dr2)
129
+ ctx.lineTo(c.x - dr2, c.y + r2)
130
+ ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
131
+ ctx.fill()
132
+ } else if (shape === 'triangle_right') {
133
+ const dr2 = (size - resolution) / 2
134
+ ctx.beginPath()
135
+ ctx.moveTo(c.x - dr2, c.y - dr2)
136
+ ctx.lineTo(c.x + resolution + dr2, c.y + r2)
137
+ ctx.lineTo(c.x - dr2, c.y + resolution + dr2)
138
+ ctx.fill()
139
+ } else {
140
+ throw new Error('Unexpected shape:' + shape)
141
+ }
142
+ }
143
+
144
+ //update legends
145
+ this.updateLegends({ viewScale: viewScale, resolution: resolution, z: z, cells: cells })
146
+ }
147
+ }
@@ -1,114 +1,112 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
- import { SideStyle } from './SideStyle.js'
5
-
6
- /**
7
- * A style to show the sides of grid cells based on their different categories.
8
- *
9
- * @module style
10
- * @author Julien Gaffuri
11
- */
12
- export class SideCategoryStyle extends SideStyle {
13
- /** @param {object} opts */
14
- constructor(opts) {
15
- super(opts)
16
- opts = opts || {}
17
-
18
- /** A function returning the category code of a cell.
19
- * @type {function(import('../core/Dataset.js').Cell, number, number):string} */
20
- this.code = opts.code
21
-
22
- /**
23
- * The dictionary (string -> color) which give the color of each category.
24
- * @type {object} */
25
- this.color = opts.color
26
-
27
- }
28
-
29
- /**
30
- * @param {Array.<import("../core/Dataset.js").Cell>} cells
31
- * @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas
32
- * @param {number} resolution
33
- */
34
- draw(cells, geoCanvas, resolution) {
35
-
36
- //filter
37
- if (this.filter) cells = cells.filter(this.filter)
38
-
39
- //
40
- const z = geoCanvas.view.z
41
-
42
- //build sides
43
-
44
- /** @type {Array.<import('./SideStyle.js').Side>} */
45
- const sides = SideStyle.buildSides(cells, resolution)
46
- if (sides.length == 0) return
47
-
48
- //get side view scale
49
- const viewScale = this.viewScale ? this.viewScale(sides, resolution, z) : undefined
50
-
51
- //draw sides
52
-
53
- geoCanvas.ctx.lineCap = 'butt'
54
- const r2 = resolution*0.5
55
- for (let side of sides) {
56
-
57
- //get category codes for both cells
58
- const code1 = side.c1 ? this.code(side.c1, resolution, z) : undefined
59
- const code2 = side.c2 ? this.code(side.c2, resolution, z) : undefined
60
- if (code1 == code2) continue
61
-
62
- //width
63
- /** @type {number|undefined} */
64
- const wG = this.width ? this.width(side, resolution, z, viewScale) : undefined
65
- if (!wG || wG <= 0) continue
66
- const w2 = wG * 0.5
67
-
68
- //set width
69
- geoCanvas.ctx.lineWidth = wG
70
-
71
- //draw segment with correct orientation
72
- if (side.or === 'h') {
73
- //top line
74
- if (code2) {
75
- geoCanvas.ctx.beginPath()
76
- geoCanvas.ctx.strokeStyle = this.color[code2]
77
- geoCanvas.ctx.moveTo(side.x-r2, side.y + w2)
78
- geoCanvas.ctx.lineTo(side.x+r2, side.y + w2)
79
- geoCanvas.ctx.stroke()
80
- }
81
-
82
- //bottom line
83
- if (code1) {
84
- geoCanvas.ctx.beginPath()
85
- geoCanvas.ctx.strokeStyle = this.color[code1]
86
- geoCanvas.ctx.moveTo(side.x-r2, side.y - w2)
87
- geoCanvas.ctx.lineTo(side.x+r2, side.y - w2)
88
- geoCanvas.ctx.stroke()
89
- }
90
- } else {
91
- //right line
92
- if (code2) {
93
- geoCanvas.ctx.beginPath()
94
- geoCanvas.ctx.strokeStyle = this.color[code2]
95
- geoCanvas.ctx.moveTo(side.x + w2, side.y-r2)
96
- geoCanvas.ctx.lineTo(side.x + w2, side.y+r2)
97
- geoCanvas.ctx.stroke()
98
- }
99
-
100
- //left line
101
- if (code1) {
102
- geoCanvas.ctx.beginPath()
103
- geoCanvas.ctx.strokeStyle = this.color[code1]
104
- geoCanvas.ctx.moveTo(side.x - w2, side.y-r2)
105
- geoCanvas.ctx.lineTo(side.x - w2, side.y+r2)
106
- geoCanvas.ctx.stroke()
107
- }
108
- }
109
- }
110
-
111
- //update legends
112
- this.updateLegends({ style: this, resolution: resolution, z: z, viewScale: viewScale })
113
- }
114
- }
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { SideStyle } from './SideStyle.js'
5
+
6
+ /**
7
+ * A style to show the sides of grid cells based on their different categories.
8
+ *
9
+ * @module style
10
+ * @author Julien Gaffuri
11
+ */
12
+ export class SideCategoryStyle extends SideStyle {
13
+ /** @param {object} opts */
14
+ constructor(opts) {
15
+ super(opts)
16
+ opts = opts || {}
17
+
18
+ /** A function returning the category code of a cell.
19
+ * @type {function(import('../core/Dataset.js').Cell, number, number):string} */
20
+ this.code = opts.code
21
+
22
+ /**
23
+ * The dictionary (string -> color) which give the color of each category.
24
+ * @type {object} */
25
+ this.color = opts.color
26
+ }
27
+
28
+ /**
29
+ * @param {Array.<import("../core/Dataset.js").Cell>} cells
30
+ * @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas
31
+ * @param {number} resolution
32
+ */
33
+ draw(cells, geoCanvas, resolution) {
34
+ //filter
35
+ if (this.filter) cells = cells.filter(this.filter)
36
+
37
+ //
38
+ const z = geoCanvas.view.z
39
+ const ctx = geoCanvas.offscreenCtx
40
+
41
+ //build sides
42
+
43
+ /** @type {Array.<import('./SideStyle.js').Side>} */
44
+ const sides = SideStyle.buildSides(cells, resolution)
45
+ if (sides.length == 0) return
46
+
47
+ //get side view scale
48
+ const viewScale = this.viewScale ? this.viewScale(sides, resolution, z) : undefined
49
+
50
+ //draw sides
51
+
52
+ ctx.lineCap = 'butt'
53
+ const r2 = resolution * 0.5
54
+ for (let side of sides) {
55
+ //get category codes for both cells
56
+ const code1 = side.c1 ? this.code(side.c1, resolution, z) : undefined
57
+ const code2 = side.c2 ? this.code(side.c2, resolution, z) : undefined
58
+ if (code1 == code2) continue
59
+
60
+ //width
61
+ /** @type {number|undefined} */
62
+ const wG = this.width ? this.width(side, resolution, z, viewScale) : undefined
63
+ if (!wG || wG <= 0) continue
64
+ const w2 = wG * 0.5
65
+
66
+ //set width
67
+ ctx.lineWidth = wG
68
+
69
+ //draw segment with correct orientation
70
+ if (side.or === 'h') {
71
+ //top line
72
+ if (code2) {
73
+ ctx.beginPath()
74
+ ctx.strokeStyle = this.color[code2]
75
+ ctx.moveTo(side.x - r2, side.y + w2)
76
+ ctx.lineTo(side.x + r2, side.y + w2)
77
+ ctx.stroke()
78
+ }
79
+
80
+ //bottom line
81
+ if (code1) {
82
+ ctx.beginPath()
83
+ ctx.strokeStyle = this.color[code1]
84
+ ctx.moveTo(side.x - r2, side.y - w2)
85
+ ctx.lineTo(side.x + r2, side.y - w2)
86
+ ctx.stroke()
87
+ }
88
+ } else {
89
+ //right line
90
+ if (code2) {
91
+ ctx.beginPath()
92
+ ctx.strokeStyle = this.color[code2]
93
+ ctx.moveTo(side.x + w2, side.y - r2)
94
+ ctx.lineTo(side.x + w2, side.y + r2)
95
+ ctx.stroke()
96
+ }
97
+
98
+ //left line
99
+ if (code1) {
100
+ ctx.beginPath()
101
+ ctx.strokeStyle = this.color[code1]
102
+ ctx.moveTo(side.x - w2, side.y - r2)
103
+ ctx.lineTo(side.x - w2, side.y + r2)
104
+ ctx.stroke()
105
+ }
106
+ }
107
+ }
108
+
109
+ //update legends
110
+ this.updateLegends({ style: this, resolution: resolution, z: z, viewScale: viewScale })
111
+ }
112
+ }