gridviz 3.0.34 → 3.0.36

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 (67) hide show
  1. package/README.md +129 -129
  2. package/dist/gridviz.js +17487 -17471
  3. package/dist/gridviz.min.js +2 -2
  4. package/package.json +73 -73
  5. package/src/button/Button.js +67 -67
  6. package/src/button/FullscreenButton.js +121 -121
  7. package/src/button/ZoomButtons.js +103 -103
  8. package/src/core/Dataset.js +154 -154
  9. package/src/core/Drawable.js +78 -78
  10. package/src/core/GeoCanvas.js +443 -443
  11. package/src/core/Layer.js +23 -23
  12. package/src/core/Legend.js +108 -108
  13. package/src/core/Map.js +568 -552
  14. package/src/core/MultiResolutionDataset.js +93 -93
  15. package/src/core/Style.js +98 -98
  16. package/src/core/Tooltip.js +182 -182
  17. package/src/dataset/COGeo.js +25 -25
  18. package/src/dataset/CSVGrid.js +112 -112
  19. package/src/dataset/GeoTIFF.js +122 -122
  20. package/src/dataset/JSGrid.js +55 -55
  21. package/src/dataset/TiledGrid.js +302 -302
  22. package/src/dataset/worker.js +18 -18
  23. package/src/index.js +79 -79
  24. package/src/layer/BackgroundLayer.js +173 -173
  25. package/src/layer/BackgroundLayerImage.js +81 -81
  26. package/src/layer/BackgroundLayerWMS.js +104 -104
  27. package/src/layer/GeoJSONLayer.js +276 -276
  28. package/src/layer/GridLayer.js +166 -166
  29. package/src/layer/LabelLayer.js +194 -194
  30. package/src/legend/ColorCategoryLegend.js +109 -109
  31. package/src/legend/ColorDiscreteLegend.js +100 -100
  32. package/src/legend/ColorLegend.js +115 -115
  33. package/src/legend/OrientationLegend.js +89 -89
  34. package/src/legend/SizeLegend.js +224 -224
  35. package/src/legend/TernaryLegend.js +302 -302
  36. package/src/style/CompositionStyle.js +291 -291
  37. package/src/style/DotDensityStyle.js +133 -133
  38. package/src/style/ImageStyle.js +100 -100
  39. package/src/style/Interpolator.js +258 -258
  40. package/src/style/IsoFenceStyle.js +217 -217
  41. package/src/style/JoyPlotStyle.js +180 -180
  42. package/src/style/LegoStyle.js +132 -132
  43. package/src/style/MosaicStyle.js +99 -99
  44. package/src/style/NinjaStarStyle.js +102 -102
  45. package/src/style/PillarStyle.js +207 -207
  46. package/src/style/SegmentStyle.js +105 -105
  47. package/src/style/ShadingRayStyle.js +280 -280
  48. package/src/style/ShadingStyle.js +72 -72
  49. package/src/style/ShapeColorSizeStyle.js +147 -147
  50. package/src/style/SideCategoryStyle.js +112 -112
  51. package/src/style/SideStyle.js +217 -217
  52. package/src/style/SideTanakaStyle.js +154 -154
  53. package/src/style/SquareColorCategoryWebGLStyle.js +307 -294
  54. package/src/style/SquareColorWebGLStyle.js +126 -126
  55. package/src/style/StrokeStyle.js +135 -135
  56. package/src/style/TextStyle.js +110 -110
  57. package/src/style/TimeSeriesStyle.js +222 -222
  58. package/src/utils/WebGLRectangleColoring.js +106 -106
  59. package/src/utils/WebGLSquareColoring.js +118 -118
  60. package/src/utils/WebGLSquareColoringAdvanced.js +227 -227
  61. package/src/utils/WebGLSquareColoringKS.js +153 -153
  62. package/src/utils/scale.js +255 -255
  63. package/src/utils/stretching.js +120 -120
  64. package/src/utils/ternary.js +208 -208
  65. package/src/utils/utils.js +125 -125
  66. package/src/utils/webGLUtils.js +73 -73
  67. package/src/utils/zoomUtils.js +142 -142
@@ -1,72 +1,72 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
- import { SideStyle } from './SideStyle.js'
5
- import { max } from 'd3-array'
6
-
7
- /** @typedef {{ x:number, y:number, or:"v"|"h", c1:(import('../core/Dataset.js').Cell)|undefined, c2:(import('../core/Dataset.js').Cell)|undefined }} Side */
8
-
9
-
10
- /**
11
- * @module style
12
- * @author Julien Gaffuri
13
- */
14
- export class ShadingStyle extends SideStyle {
15
-
16
- /** @param {object} opts
17
- * @param {string} opts.elevation
18
- * @param {boolean} opts.diamond
19
- * @param {function} opts.scale
20
- * @param {function} opts.width
21
- * @param {number} opts.reliefDirection
22
- * @param {string} opts.colorTopLeft
23
- * @param {string} opts.colorBottomRight
24
- */
25
- constructor(opts = {}) {
26
- super(opts)
27
-
28
- /** The cell elevation field name
29
- * @type {string} */
30
- const elevation = opts.elevation
31
-
32
- // compute side value as elevation difference and attach to side
33
- const sideValue = (/** @type {Side} */ side) => {
34
- if (!side.c1) side.v = 0
35
- else if (!side.c2) side.v = 0
36
- else if (!side.c1[elevation]) side.v = 0
37
- else if (!side.c2[elevation]) side.v = 0
38
- else side.v = +side.c2[elevation] - side.c1[elevation]
39
- return side.v
40
- }
41
-
42
- // compute maximum side value for normalization
43
- this.viewScale = sides => max(sides, s => sideValue(s))
44
-
45
- // get colors
46
- let colorTopLeft = opts.colorTopLeft || '255,255,255'
47
- let colorBottomRight = opts.colorBottomRight || '0,0,0'
48
-
49
- // revert colors (to revert the relief, show depressions as hills)
50
- const revert = opts.revert == undefined? false : opts.revert
51
- if(revert) {
52
- let a = colorTopLeft
53
- colorTopLeft = colorBottomRight
54
- colorBottomRight = a
55
- }
56
-
57
- //
58
- const scale = opts.scale
59
-
60
- this.color = (side, resolution, z, max) => {
61
- if (side.v == 0) return
62
- let coeff = Math.abs(side.v / max)
63
- if(scale) coeff = scale(coeff)
64
- if ((side.v < 0 && side.or === 'h') || (side.v > 0 && side.or === 'v'))
65
- return 'rgba(' + colorTopLeft + ',' + coeff + ')'
66
- return 'rgba(' + colorBottomRight + ',' + coeff + ')'
67
- }
68
-
69
- this.width = (_, r, z) => opts.width | Math.min(2 * z, r / 3)
70
- this.diamond = opts.diamond
71
- }
72
- }
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { SideStyle } from './SideStyle.js'
5
+ import { max } from 'd3-array'
6
+
7
+ /** @typedef {{ x:number, y:number, or:"v"|"h", c1:(import('../core/Dataset.js').Cell)|undefined, c2:(import('../core/Dataset.js').Cell)|undefined }} Side */
8
+
9
+
10
+ /**
11
+ * @module style
12
+ * @author Julien Gaffuri
13
+ */
14
+ export class ShadingStyle extends SideStyle {
15
+
16
+ /** @param {object} opts
17
+ * @param {string} opts.elevation
18
+ * @param {boolean} opts.diamond
19
+ * @param {function} opts.scale
20
+ * @param {function} opts.width
21
+ * @param {number} opts.reliefDirection
22
+ * @param {string} opts.colorTopLeft
23
+ * @param {string} opts.colorBottomRight
24
+ */
25
+ constructor(opts = {}) {
26
+ super(opts)
27
+
28
+ /** The cell elevation field name
29
+ * @type {string} */
30
+ const elevation = opts.elevation
31
+
32
+ // compute side value as elevation difference and attach to side
33
+ const sideValue = (/** @type {Side} */ side) => {
34
+ if (!side.c1) side.v = 0
35
+ else if (!side.c2) side.v = 0
36
+ else if (!side.c1[elevation]) side.v = 0
37
+ else if (!side.c2[elevation]) side.v = 0
38
+ else side.v = +side.c2[elevation] - side.c1[elevation]
39
+ return side.v
40
+ }
41
+
42
+ // compute maximum side value for normalization
43
+ this.viewScale = sides => max(sides, s => sideValue(s))
44
+
45
+ // get colors
46
+ let colorTopLeft = opts.colorTopLeft || '255,255,255'
47
+ let colorBottomRight = opts.colorBottomRight || '0,0,0'
48
+
49
+ // revert colors (to revert the relief, show depressions as hills)
50
+ const revert = opts.revert == undefined? false : opts.revert
51
+ if(revert) {
52
+ let a = colorTopLeft
53
+ colorTopLeft = colorBottomRight
54
+ colorBottomRight = a
55
+ }
56
+
57
+ //
58
+ const scale = opts.scale
59
+
60
+ this.color = (side, resolution, z, max) => {
61
+ if (side.v == 0) return
62
+ let coeff = Math.abs(side.v / max)
63
+ if(scale) coeff = scale(coeff)
64
+ if ((side.v < 0 && side.or === 'h') || (side.v > 0 && side.or === 'v'))
65
+ return 'rgba(' + colorTopLeft + ',' + coeff + ')'
66
+ return 'rgba(' + colorBottomRight + ',' + coeff + ')'
67
+ }
68
+
69
+ this.width = (_, r, z) => opts.width | Math.min(2 * z, r / 3)
70
+ this.diamond = opts.diamond
71
+ }
72
+ }
@@ -1,147 +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
- /** @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
+ //@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,112 +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
- * @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
- }
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
+ }