gridviz 3.0.19 → 3.0.21
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.
- package/README.md +128 -128
- package/dist/gridviz.js +7875 -7857
- package/dist/gridviz.min.js +1 -1
- package/package.json +74 -74
- package/src/button/ZoomButtons.js +101 -101
- package/src/core/Dataset.js +154 -154
- package/src/core/Drawable.js +78 -60
- package/src/core/GeoCanvas.js +430 -430
- package/src/core/Layer.js +23 -23
- package/src/core/Legend.js +108 -108
- package/src/core/Map.js +516 -516
- package/src/core/MultiResolutionDataset.js +93 -93
- package/src/core/Style.js +98 -98
- package/src/dataset/CSVGrid.js +112 -112
- package/src/dataset/GeoTIFF.js +122 -122
- package/src/dataset/JSGrid.js +55 -55
- package/src/dataset/TiledGrid.js +302 -302
- package/src/dataset/worker.js +18 -18
- package/src/index.js +77 -77
- package/src/layer/BackgroundLayer.js +162 -162
- package/src/layer/BackgroundLayerImage.js +81 -81
- package/src/layer/BackgroundLayerWMS.js +104 -104
- package/src/layer/GeoJSONLayer.js +214 -214
- package/src/layer/GridLayer.js +148 -148
- package/src/layer/LabelLayer.js +194 -194
- package/src/legend/ColorCategoryLegend.js +109 -109
- package/src/legend/ColorDiscreteLegend.js +100 -100
- package/src/legend/ColorLegend.js +115 -115
- package/src/legend/OrientationLegend.js +89 -89
- package/src/legend/SizeLegend.js +224 -224
- package/src/legend/TernaryLegend.js +302 -302
- package/src/style/CompositionStyle.js +291 -291
- package/src/style/DotDensityStyle.js +133 -133
- package/src/style/ImageStyle.js +100 -100
- package/src/style/IsoFenceStyle.js +217 -217
- package/src/style/JoyPlotStyle.js +127 -127
- package/src/style/LegoStyle.js +110 -110
- package/src/style/MosaicStyle.js +99 -99
- package/src/style/NinjaStarStyle.js +102 -102
- package/src/style/PillarStyle.js +207 -207
- package/src/style/SegmentStyle.js +105 -105
- package/src/style/ShapeColorSizeStyle.js +147 -147
- package/src/style/SideCategoryStyle.js +112 -112
- package/src/style/SideStyle.js +212 -212
- package/src/style/SquareColorCategoryWebGLStyle.js +107 -107
- package/src/style/SquareColorWebGLStyle.js +126 -126
- package/src/style/StrokeStyle.js +135 -135
- package/src/style/TanakaStyle.js +69 -69
- package/src/style/TextStyle.js +110 -110
- package/src/style/TimeSeriesStyle.js +222 -222
- package/src/utils/scale.js +218 -218
- package/src/utils/stretching.js +120 -120
- package/src/utils/ternary.js +208 -208
- package/src/utils/utils.js +58 -58
- package/src/utils/webGLUtils.js +72 -72
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { Style } from '../core/Style.js'
|
|
5
|
-
import { makeWebGLCanvas } from '../utils/webGLUtils.js'
|
|
6
|
-
import { WebGLSquareColoringAdvanced } from '../utils/WebGLSquareColoringAdvanced.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Style based on webGL
|
|
10
|
-
* To show cells as colored squares, with computation of the colors on GPU side (faster than JavaScript side).
|
|
11
|
-
* Alls squares with the same size
|
|
12
|
-
*
|
|
13
|
-
* @module style
|
|
14
|
-
* @author Julien Gaffuri
|
|
15
|
-
*/
|
|
16
|
-
export class SquareColorWebGLStyle extends Style {
|
|
17
|
-
/** @param {object} opts */
|
|
18
|
-
constructor(opts) {
|
|
19
|
-
super(opts)
|
|
20
|
-
opts = opts || {}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* A function returning a t value (within [0,1]) for a cell.
|
|
24
|
-
* @type {function(import('../core/Dataset.js').Cell,number,number,object):number} */
|
|
25
|
-
this.tFun = opts.tFun //(c,r,z,vs) => {}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Distribution stretching method.
|
|
29
|
-
* The stretching is performed on GPU side (fragment shader).
|
|
30
|
-
* @type {{ fun:string, alpha:number }} */
|
|
31
|
-
this.stretching = opts.stretching
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* The sample of the color ramp.
|
|
35
|
-
* The color is computed on GPU side (fragment shader) based on those values (linear interpolation).
|
|
36
|
-
* @type {Array.<string>} */
|
|
37
|
-
this.colors =
|
|
38
|
-
opts.colors ||
|
|
39
|
-
[
|
|
40
|
-
'rgb(158, 1, 66)',
|
|
41
|
-
'rgb(248, 142, 83)',
|
|
42
|
-
'rgb(251, 248, 176)',
|
|
43
|
-
'rgb(137, 207, 165)',
|
|
44
|
-
'rgb(94, 79, 162)',
|
|
45
|
-
].reverse()
|
|
46
|
-
if (opts.color)
|
|
47
|
-
this.colors = [
|
|
48
|
-
opts.color(0),
|
|
49
|
-
opts.color(0.2),
|
|
50
|
-
opts.color(0.4),
|
|
51
|
-
opts.color(0.6),
|
|
52
|
-
opts.color(0.8),
|
|
53
|
-
opts.color(1),
|
|
54
|
-
]
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Define the opacity of the style, within [0,1].
|
|
58
|
-
* If this opacity is defined, the individual color opacity will be ignored.
|
|
59
|
-
* @type {function(number,number):number} */
|
|
60
|
-
this.opacity = opts.opacity // (r,z) => ...
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* A function returning the size of the cells, in geographical unit. All cells have the same size.
|
|
64
|
-
* @type {function(number,number):number} */
|
|
65
|
-
this.size = opts.size // (resolution, z) => ...
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @param {Array.<import("../core/Dataset.js").Cell>} cells
|
|
70
|
-
* @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas
|
|
71
|
-
* @param {number} resolution
|
|
72
|
-
*/
|
|
73
|
-
draw(cells, geoCanvas, resolution) {
|
|
74
|
-
//filter
|
|
75
|
-
if (this.filter) cells = cells.filter(this.filter)
|
|
76
|
-
|
|
77
|
-
//
|
|
78
|
-
const z = geoCanvas.view.z
|
|
79
|
-
|
|
80
|
-
//get view scale
|
|
81
|
-
const viewScale = this.viewScale ? this.viewScale(cells, resolution, z) : undefined
|
|
82
|
-
|
|
83
|
-
//create canvas and webgl renderer
|
|
84
|
-
//for opacity control, see: https://webglfundamentals.org/webgl/lessons/webgl-and-alpha.html
|
|
85
|
-
const cvWGL = makeWebGLCanvas(
|
|
86
|
-
geoCanvas.w + '',
|
|
87
|
-
geoCanvas.h + '',
|
|
88
|
-
this.opacity != undefined ? { premultipliedAlpha: false } : undefined
|
|
89
|
-
)
|
|
90
|
-
if (!cvWGL) {
|
|
91
|
-
console.error('No webGL')
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
//add vertice and fragment data
|
|
96
|
-
const r2 = resolution / 2
|
|
97
|
-
const verticesBuffer = []
|
|
98
|
-
const tBuffer = []
|
|
99
|
-
for (let cell of cells) {
|
|
100
|
-
const t = this.tFun(cell, resolution, z, viewScale)
|
|
101
|
-
if (t == null || t == undefined) continue
|
|
102
|
-
verticesBuffer.push(cell.x + r2, cell.y + r2)
|
|
103
|
-
tBuffer.push(t > 1 ? 1 : t < 0 ? 0 : t)
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
//compute pixel size
|
|
107
|
-
const sizeGeo = this.size ? this.size(resolution, z) : resolution + 0.2 * z
|
|
108
|
-
|
|
109
|
-
//compute opacity
|
|
110
|
-
const op = this.opacity ? this.opacity(resolution, z) : undefined
|
|
111
|
-
|
|
112
|
-
//
|
|
113
|
-
const wgp = new WebGLSquareColoringAdvanced(cvWGL.gl, this.colors, this.stretching, sizeGeo / z, op)
|
|
114
|
-
|
|
115
|
-
//draw
|
|
116
|
-
wgp.draw(verticesBuffer, tBuffer, geoCanvas.getWebGLTransform())
|
|
117
|
-
|
|
118
|
-
// draw in canvas geo
|
|
119
|
-
// NOTE: drawing each tile this way is very inefficient. WebGL is best used with fewer, heavier/larger draw calls.
|
|
120
|
-
geoCanvas.initCanvasTransform()
|
|
121
|
-
geoCanvas.offscreenCtx.drawImage(cvWGL.canvas, 0, 0)
|
|
122
|
-
|
|
123
|
-
//update legends
|
|
124
|
-
this.updateLegends({ style: this, resolution: resolution, z: z, viewScale: viewScale })
|
|
125
|
-
}
|
|
126
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { Style } from '../core/Style.js'
|
|
5
|
+
import { makeWebGLCanvas } from '../utils/webGLUtils.js'
|
|
6
|
+
import { WebGLSquareColoringAdvanced } from '../utils/WebGLSquareColoringAdvanced.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Style based on webGL
|
|
10
|
+
* To show cells as colored squares, with computation of the colors on GPU side (faster than JavaScript side).
|
|
11
|
+
* Alls squares with the same size
|
|
12
|
+
*
|
|
13
|
+
* @module style
|
|
14
|
+
* @author Julien Gaffuri
|
|
15
|
+
*/
|
|
16
|
+
export class SquareColorWebGLStyle extends Style {
|
|
17
|
+
/** @param {object} opts */
|
|
18
|
+
constructor(opts) {
|
|
19
|
+
super(opts)
|
|
20
|
+
opts = opts || {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A function returning a t value (within [0,1]) for a cell.
|
|
24
|
+
* @type {function(import('../core/Dataset.js').Cell,number,number,object):number} */
|
|
25
|
+
this.tFun = opts.tFun //(c,r,z,vs) => {}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Distribution stretching method.
|
|
29
|
+
* The stretching is performed on GPU side (fragment shader).
|
|
30
|
+
* @type {{ fun:string, alpha:number }} */
|
|
31
|
+
this.stretching = opts.stretching
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The sample of the color ramp.
|
|
35
|
+
* The color is computed on GPU side (fragment shader) based on those values (linear interpolation).
|
|
36
|
+
* @type {Array.<string>} */
|
|
37
|
+
this.colors =
|
|
38
|
+
opts.colors ||
|
|
39
|
+
[
|
|
40
|
+
'rgb(158, 1, 66)',
|
|
41
|
+
'rgb(248, 142, 83)',
|
|
42
|
+
'rgb(251, 248, 176)',
|
|
43
|
+
'rgb(137, 207, 165)',
|
|
44
|
+
'rgb(94, 79, 162)',
|
|
45
|
+
].reverse()
|
|
46
|
+
if (opts.color)
|
|
47
|
+
this.colors = [
|
|
48
|
+
opts.color(0),
|
|
49
|
+
opts.color(0.2),
|
|
50
|
+
opts.color(0.4),
|
|
51
|
+
opts.color(0.6),
|
|
52
|
+
opts.color(0.8),
|
|
53
|
+
opts.color(1),
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Define the opacity of the style, within [0,1].
|
|
58
|
+
* If this opacity is defined, the individual color opacity will be ignored.
|
|
59
|
+
* @type {function(number,number):number} */
|
|
60
|
+
this.opacity = opts.opacity // (r,z) => ...
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A function returning the size of the cells, in geographical unit. All cells have the same size.
|
|
64
|
+
* @type {function(number,number):number} */
|
|
65
|
+
this.size = opts.size // (resolution, z) => ...
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @param {Array.<import("../core/Dataset.js").Cell>} cells
|
|
70
|
+
* @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas
|
|
71
|
+
* @param {number} resolution
|
|
72
|
+
*/
|
|
73
|
+
draw(cells, geoCanvas, resolution) {
|
|
74
|
+
//filter
|
|
75
|
+
if (this.filter) cells = cells.filter(this.filter)
|
|
76
|
+
|
|
77
|
+
//
|
|
78
|
+
const z = geoCanvas.view.z
|
|
79
|
+
|
|
80
|
+
//get view scale
|
|
81
|
+
const viewScale = this.viewScale ? this.viewScale(cells, resolution, z) : undefined
|
|
82
|
+
|
|
83
|
+
//create canvas and webgl renderer
|
|
84
|
+
//for opacity control, see: https://webglfundamentals.org/webgl/lessons/webgl-and-alpha.html
|
|
85
|
+
const cvWGL = makeWebGLCanvas(
|
|
86
|
+
geoCanvas.w + '',
|
|
87
|
+
geoCanvas.h + '',
|
|
88
|
+
this.opacity != undefined ? { premultipliedAlpha: false } : undefined
|
|
89
|
+
)
|
|
90
|
+
if (!cvWGL) {
|
|
91
|
+
console.error('No webGL')
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
//add vertice and fragment data
|
|
96
|
+
const r2 = resolution / 2
|
|
97
|
+
const verticesBuffer = []
|
|
98
|
+
const tBuffer = []
|
|
99
|
+
for (let cell of cells) {
|
|
100
|
+
const t = this.tFun(cell, resolution, z, viewScale)
|
|
101
|
+
if (t == null || t == undefined) continue
|
|
102
|
+
verticesBuffer.push(cell.x + r2, cell.y + r2)
|
|
103
|
+
tBuffer.push(t > 1 ? 1 : t < 0 ? 0 : t)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
//compute pixel size
|
|
107
|
+
const sizeGeo = this.size ? this.size(resolution, z) : resolution + 0.2 * z
|
|
108
|
+
|
|
109
|
+
//compute opacity
|
|
110
|
+
const op = this.opacity ? this.opacity(resolution, z) : undefined
|
|
111
|
+
|
|
112
|
+
//
|
|
113
|
+
const wgp = new WebGLSquareColoringAdvanced(cvWGL.gl, this.colors, this.stretching, sizeGeo / z, op)
|
|
114
|
+
|
|
115
|
+
//draw
|
|
116
|
+
wgp.draw(verticesBuffer, tBuffer, geoCanvas.getWebGLTransform())
|
|
117
|
+
|
|
118
|
+
// draw in canvas geo
|
|
119
|
+
// NOTE: drawing each tile this way is very inefficient. WebGL is best used with fewer, heavier/larger draw calls.
|
|
120
|
+
geoCanvas.initCanvasTransform()
|
|
121
|
+
geoCanvas.offscreenCtx.drawImage(cvWGL.canvas, 0, 0)
|
|
122
|
+
|
|
123
|
+
//update legends
|
|
124
|
+
this.updateLegends({ style: this, resolution: resolution, z: z, viewScale: viewScale })
|
|
125
|
+
}
|
|
126
|
+
}
|
package/src/style/StrokeStyle.js
CHANGED
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { Style } from '../core/Style.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @module style
|
|
8
|
-
* @author Julien Gaffuri
|
|
9
|
-
*/
|
|
10
|
-
export class StrokeStyle extends Style {
|
|
11
|
-
/** @param {object} opts */
|
|
12
|
-
constructor(opts) {
|
|
13
|
-
super(opts)
|
|
14
|
-
opts = opts || {}
|
|
15
|
-
|
|
16
|
-
/** A function returning the color of the cell.
|
|
17
|
-
* @type {function(import('../core/Dataset.js').Cell,number,number,object):string} */
|
|
18
|
-
this.strokeColor = opts.strokeColor || (() => '#666') //(c,r,z,vs) => {}
|
|
19
|
-
|
|
20
|
-
/** A function returning the size of a cell in geographical unit.
|
|
21
|
-
* @type {function(import('../core/Dataset.js').Cell,number,number,object):number} */
|
|
22
|
-
this.size = opts.size || ((cell, resolution) => resolution) //(c,r,z,vs) => {}
|
|
23
|
-
|
|
24
|
-
/** The stroke line width in geographical unit.
|
|
25
|
-
* @type {function(import('../core/Dataset.js').Cell,number,number,object):number} */
|
|
26
|
-
this.strokeWidth = opts.strokeWidth || ((cell, resolution, z) => z * 1.5) //(c,r,z,vs) => {}
|
|
27
|
-
|
|
28
|
-
/** A function returning the shape of a cell.
|
|
29
|
-
* @type {function(import("../core/Dataset.js").Cell,number,number,object):import("../core/Style.js").Shape} */
|
|
30
|
-
this.shape = opts.shape || (() => 'square') //(c,r,z,vs) => {}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Draw cells as squares, with various colors and size.
|
|
35
|
-
*
|
|
36
|
-
* @param {Array.<import("../core/Dataset").Cell>} cells
|
|
37
|
-
* @param {import("../core/GeoCanvas").GeoCanvas} geoCanvas
|
|
38
|
-
* @param {number} resolution
|
|
39
|
-
*/
|
|
40
|
-
draw(cells, geoCanvas, resolution) {
|
|
41
|
-
//filter
|
|
42
|
-
if (this.filter) cells = cells.filter(this.filter)
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
const z = geoCanvas.view.z
|
|
46
|
-
const ctx = geoCanvas.offscreenCtx
|
|
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
|
-
const col = this.strokeColor ? this.strokeColor(c, resolution, z, viewScale) : undefined
|
|
55
|
-
if (!col || col === 'none') continue
|
|
56
|
-
ctx.strokeStyle = col
|
|
57
|
-
|
|
58
|
-
//size - in geo unit
|
|
59
|
-
const size = this.size ? this.size(c, resolution, z, viewScale) : resolution
|
|
60
|
-
|
|
61
|
-
//width
|
|
62
|
-
const wi = this.strokeWidth ? this.strokeWidth(c, resolution, z, viewScale) : 1 * z
|
|
63
|
-
if (!wi || wi <= 0) continue
|
|
64
|
-
ctx.lineWidth = wi
|
|
65
|
-
|
|
66
|
-
//shape
|
|
67
|
-
const shape = this.shape ? this.shape(c, resolution, z, viewScale) : 'square'
|
|
68
|
-
if (shape === 'none') continue
|
|
69
|
-
|
|
70
|
-
//get offset
|
|
71
|
-
const offset = this.offset(c, resolution, z)
|
|
72
|
-
|
|
73
|
-
if (shape === 'square') {
|
|
74
|
-
//draw square
|
|
75
|
-
const d = resolution * (1 - size / resolution) * 0.5
|
|
76
|
-
ctx.beginPath()
|
|
77
|
-
ctx.rect(c.x + d + offset.dx, c.y + d + offset.dy, size, size)
|
|
78
|
-
ctx.stroke()
|
|
79
|
-
} else if (shape === 'circle') {
|
|
80
|
-
//draw circle
|
|
81
|
-
ctx.beginPath()
|
|
82
|
-
ctx.arc(c.x + r2 + offset.dx, c.y + r2 + offset.dy, size * 0.5, 0, 2 * Math.PI, false)
|
|
83
|
-
ctx.stroke()
|
|
84
|
-
} else if (shape === 'diamond') {
|
|
85
|
-
const s2 = size * 0.5
|
|
86
|
-
ctx.beginPath()
|
|
87
|
-
ctx.moveTo(c.x + r2 - s2, c.y + r2)
|
|
88
|
-
ctx.lineTo(c.x + r2, c.y + r2 + s2)
|
|
89
|
-
ctx.lineTo(c.x + r2 + s2, c.y + r2)
|
|
90
|
-
ctx.lineTo(c.x + r2, c.y + r2 - s2)
|
|
91
|
-
ctx.lineTo(c.x + r2 - s2, c.y + r2)
|
|
92
|
-
ctx.stroke()
|
|
93
|
-
} else if (shape === 'donut') {
|
|
94
|
-
console.error('Not implemented')
|
|
95
|
-
} else if (shape === 'triangle_up') {
|
|
96
|
-
const dr2 = (size - resolution) / 2
|
|
97
|
-
ctx.beginPath()
|
|
98
|
-
ctx.moveTo(c.x - dr2, c.y - dr2)
|
|
99
|
-
ctx.lineTo(c.x + r2, c.y + resolution + dr2)
|
|
100
|
-
ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
|
|
101
|
-
ctx.closePath()
|
|
102
|
-
ctx.stroke()
|
|
103
|
-
} else if (shape === 'triangle_down') {
|
|
104
|
-
const dr2 = (size - resolution) / 2
|
|
105
|
-
ctx.beginPath()
|
|
106
|
-
ctx.moveTo(c.x - dr2, c.y + resolution + dr2)
|
|
107
|
-
ctx.lineTo(c.x + r2, c.y - dr2)
|
|
108
|
-
ctx.lineTo(c.x + resolution + dr2, c.y + resolution + dr2)
|
|
109
|
-
ctx.closePath()
|
|
110
|
-
ctx.stroke()
|
|
111
|
-
} else if (shape === 'triangle_left') {
|
|
112
|
-
const dr2 = (size - resolution) / 2
|
|
113
|
-
ctx.beginPath()
|
|
114
|
-
ctx.moveTo(c.x + resolution + dr2, c.y + resolution + dr2)
|
|
115
|
-
ctx.lineTo(c.x - dr2, c.y + r2)
|
|
116
|
-
ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
|
|
117
|
-
ctx.closePath()
|
|
118
|
-
ctx.stroke()
|
|
119
|
-
} else if (shape === 'triangle_right') {
|
|
120
|
-
const dr2 = (size - resolution) / 2
|
|
121
|
-
ctx.beginPath()
|
|
122
|
-
ctx.moveTo(c.x - dr2, c.y - dr2)
|
|
123
|
-
ctx.lineTo(c.x + resolution + dr2, c.y + r2)
|
|
124
|
-
ctx.lineTo(c.x - dr2, c.y + resolution + dr2)
|
|
125
|
-
ctx.closePath()
|
|
126
|
-
ctx.stroke()
|
|
127
|
-
} else {
|
|
128
|
-
throw new Error('Unexpected shape:' + shape)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
//update legends
|
|
133
|
-
this.updateLegends({ style: this, resolution: resolution, z: z, viewScale: viewScale })
|
|
134
|
-
}
|
|
135
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { Style } from '../core/Style.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @module style
|
|
8
|
+
* @author Julien Gaffuri
|
|
9
|
+
*/
|
|
10
|
+
export class StrokeStyle extends Style {
|
|
11
|
+
/** @param {object} opts */
|
|
12
|
+
constructor(opts) {
|
|
13
|
+
super(opts)
|
|
14
|
+
opts = opts || {}
|
|
15
|
+
|
|
16
|
+
/** A function returning the color of the cell.
|
|
17
|
+
* @type {function(import('../core/Dataset.js').Cell,number,number,object):string} */
|
|
18
|
+
this.strokeColor = opts.strokeColor || (() => '#666') //(c,r,z,vs) => {}
|
|
19
|
+
|
|
20
|
+
/** A function returning the size of a cell in geographical unit.
|
|
21
|
+
* @type {function(import('../core/Dataset.js').Cell,number,number,object):number} */
|
|
22
|
+
this.size = opts.size || ((cell, resolution) => resolution) //(c,r,z,vs) => {}
|
|
23
|
+
|
|
24
|
+
/** The stroke line width in geographical unit.
|
|
25
|
+
* @type {function(import('../core/Dataset.js').Cell,number,number,object):number} */
|
|
26
|
+
this.strokeWidth = opts.strokeWidth || ((cell, resolution, z) => z * 1.5) //(c,r,z,vs) => {}
|
|
27
|
+
|
|
28
|
+
/** A function returning the shape of a cell.
|
|
29
|
+
* @type {function(import("../core/Dataset.js").Cell,number,number,object):import("../core/Style.js").Shape} */
|
|
30
|
+
this.shape = opts.shape || (() => 'square') //(c,r,z,vs) => {}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Draw cells as squares, with various colors and size.
|
|
35
|
+
*
|
|
36
|
+
* @param {Array.<import("../core/Dataset").Cell>} cells
|
|
37
|
+
* @param {import("../core/GeoCanvas").GeoCanvas} geoCanvas
|
|
38
|
+
* @param {number} resolution
|
|
39
|
+
*/
|
|
40
|
+
draw(cells, geoCanvas, resolution) {
|
|
41
|
+
//filter
|
|
42
|
+
if (this.filter) cells = cells.filter(this.filter)
|
|
43
|
+
|
|
44
|
+
//
|
|
45
|
+
const z = geoCanvas.view.z
|
|
46
|
+
const ctx = geoCanvas.offscreenCtx
|
|
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
|
+
const col = this.strokeColor ? this.strokeColor(c, resolution, z, viewScale) : undefined
|
|
55
|
+
if (!col || col === 'none') continue
|
|
56
|
+
ctx.strokeStyle = col
|
|
57
|
+
|
|
58
|
+
//size - in geo unit
|
|
59
|
+
const size = this.size ? this.size(c, resolution, z, viewScale) : resolution
|
|
60
|
+
|
|
61
|
+
//width
|
|
62
|
+
const wi = this.strokeWidth ? this.strokeWidth(c, resolution, z, viewScale) : 1 * z
|
|
63
|
+
if (!wi || wi <= 0) continue
|
|
64
|
+
ctx.lineWidth = wi
|
|
65
|
+
|
|
66
|
+
//shape
|
|
67
|
+
const shape = this.shape ? this.shape(c, resolution, z, viewScale) : 'square'
|
|
68
|
+
if (shape === 'none') continue
|
|
69
|
+
|
|
70
|
+
//get offset
|
|
71
|
+
const offset = this.offset(c, resolution, z)
|
|
72
|
+
|
|
73
|
+
if (shape === 'square') {
|
|
74
|
+
//draw square
|
|
75
|
+
const d = resolution * (1 - size / resolution) * 0.5
|
|
76
|
+
ctx.beginPath()
|
|
77
|
+
ctx.rect(c.x + d + offset.dx, c.y + d + offset.dy, size, size)
|
|
78
|
+
ctx.stroke()
|
|
79
|
+
} else if (shape === 'circle') {
|
|
80
|
+
//draw circle
|
|
81
|
+
ctx.beginPath()
|
|
82
|
+
ctx.arc(c.x + r2 + offset.dx, c.y + r2 + offset.dy, size * 0.5, 0, 2 * Math.PI, false)
|
|
83
|
+
ctx.stroke()
|
|
84
|
+
} else if (shape === 'diamond') {
|
|
85
|
+
const s2 = size * 0.5
|
|
86
|
+
ctx.beginPath()
|
|
87
|
+
ctx.moveTo(c.x + r2 - s2, c.y + r2)
|
|
88
|
+
ctx.lineTo(c.x + r2, c.y + r2 + s2)
|
|
89
|
+
ctx.lineTo(c.x + r2 + s2, c.y + r2)
|
|
90
|
+
ctx.lineTo(c.x + r2, c.y + r2 - s2)
|
|
91
|
+
ctx.lineTo(c.x + r2 - s2, c.y + r2)
|
|
92
|
+
ctx.stroke()
|
|
93
|
+
} else if (shape === 'donut') {
|
|
94
|
+
console.error('Not implemented')
|
|
95
|
+
} else if (shape === 'triangle_up') {
|
|
96
|
+
const dr2 = (size - resolution) / 2
|
|
97
|
+
ctx.beginPath()
|
|
98
|
+
ctx.moveTo(c.x - dr2, c.y - dr2)
|
|
99
|
+
ctx.lineTo(c.x + r2, c.y + resolution + dr2)
|
|
100
|
+
ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
|
|
101
|
+
ctx.closePath()
|
|
102
|
+
ctx.stroke()
|
|
103
|
+
} else if (shape === 'triangle_down') {
|
|
104
|
+
const dr2 = (size - resolution) / 2
|
|
105
|
+
ctx.beginPath()
|
|
106
|
+
ctx.moveTo(c.x - dr2, c.y + resolution + dr2)
|
|
107
|
+
ctx.lineTo(c.x + r2, c.y - dr2)
|
|
108
|
+
ctx.lineTo(c.x + resolution + dr2, c.y + resolution + dr2)
|
|
109
|
+
ctx.closePath()
|
|
110
|
+
ctx.stroke()
|
|
111
|
+
} else if (shape === 'triangle_left') {
|
|
112
|
+
const dr2 = (size - resolution) / 2
|
|
113
|
+
ctx.beginPath()
|
|
114
|
+
ctx.moveTo(c.x + resolution + dr2, c.y + resolution + dr2)
|
|
115
|
+
ctx.lineTo(c.x - dr2, c.y + r2)
|
|
116
|
+
ctx.lineTo(c.x + resolution + dr2, c.y - dr2)
|
|
117
|
+
ctx.closePath()
|
|
118
|
+
ctx.stroke()
|
|
119
|
+
} else if (shape === 'triangle_right') {
|
|
120
|
+
const dr2 = (size - resolution) / 2
|
|
121
|
+
ctx.beginPath()
|
|
122
|
+
ctx.moveTo(c.x - dr2, c.y - dr2)
|
|
123
|
+
ctx.lineTo(c.x + resolution + dr2, c.y + r2)
|
|
124
|
+
ctx.lineTo(c.x - dr2, c.y + resolution + dr2)
|
|
125
|
+
ctx.closePath()
|
|
126
|
+
ctx.stroke()
|
|
127
|
+
} else {
|
|
128
|
+
throw new Error('Unexpected shape:' + shape)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
//update legends
|
|
133
|
+
this.updateLegends({ style: this, resolution: resolution, z: z, viewScale: viewScale })
|
|
134
|
+
}
|
|
135
|
+
}
|
package/src/style/TanakaStyle.js
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { SideStyle } from './SideStyle.js'
|
|
5
|
-
import { SquareColorCategoryWebGLStyle } from './SquareColorCategoryWebGLStyle.js'
|
|
6
|
-
import { classifier as clFun, colorClassifier as cclFun } from '../utils/scale.js'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @see https://manifold.net/doc/mfd9/example__tanaka_contours.htm
|
|
10
|
-
*
|
|
11
|
-
* @module style
|
|
12
|
-
* @author Julien Gaffuri
|
|
13
|
-
*/
|
|
14
|
-
export class TanakaStyle {
|
|
15
|
-
/**
|
|
16
|
-
* @param {function(import('../core/Dataset.js').Cell):number} value Function that returns the value of a cell
|
|
17
|
-
* @param {Array.<number>} breaks The break values
|
|
18
|
-
* @param {Array.<string>} colors The colors, one more than the break values
|
|
19
|
-
* @param {object} opts
|
|
20
|
-
* @returns {Array.<import("../core/Style").Style>}
|
|
21
|
-
*/
|
|
22
|
-
static get(value, breaks, colors, opts = {}) {
|
|
23
|
-
//shadow colors
|
|
24
|
-
opts.colorDark = opts.colorDark || '#111'
|
|
25
|
-
opts.colorBright = opts.colorBright || '#ddd'
|
|
26
|
-
|
|
27
|
-
/** @type { function(number, number):number } */
|
|
28
|
-
opts.width =
|
|
29
|
-
opts.width ||
|
|
30
|
-
((sideValue, resolution, z) => {
|
|
31
|
-
const minWG = 1 * z
|
|
32
|
-
const maxWG = 4 * z
|
|
33
|
-
const step = (maxWG - minWG) / 3
|
|
34
|
-
return Math.min(minWG + (sideValue - 1) * step, maxWG)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
//make classifier
|
|
38
|
-
const classifier = clFun(breaks)
|
|
39
|
-
//make colors table
|
|
40
|
-
const colorsDict = {}
|
|
41
|
-
for (let i = 0; i < colors.length; i++) colorsDict[i + ''] = colors[i]
|
|
42
|
-
|
|
43
|
-
const cellStyle = new SquareColorCategoryWebGLStyle({
|
|
44
|
-
code: (cell) => classifier(value(cell)),
|
|
45
|
-
color: colorsDict,
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
const getSideValue = (side) => {
|
|
49
|
-
const cl1 = side.c1 ? classifier(value(side.c1)) : -1
|
|
50
|
-
const cl2 = side.c2 ? classifier(value(side.c2)) : -1
|
|
51
|
-
return cl1 - cl2
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** The side style, for the shadow effect */
|
|
55
|
-
const sideStyle = new SideStyle({
|
|
56
|
-
//white or black, depending on orientation and value
|
|
57
|
-
color: (side) => {
|
|
58
|
-
const v = getSideValue(side)
|
|
59
|
-
if (v === 0) return
|
|
60
|
-
if (side.or === 'v') return v < 0 ? opts.colorBright : opts.colorDark
|
|
61
|
-
return v < 0 ? opts.colorDark : opts.colorBright
|
|
62
|
-
},
|
|
63
|
-
//width depends on the value, that is the number of classes of difference
|
|
64
|
-
width: (side, resolution, z) => opts.width(Math.abs(getSideValue(side)), resolution, z),
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
return [cellStyle, sideStyle]
|
|
68
|
-
}
|
|
69
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { SideStyle } from './SideStyle.js'
|
|
5
|
+
import { SquareColorCategoryWebGLStyle } from './SquareColorCategoryWebGLStyle.js'
|
|
6
|
+
import { classifier as clFun, colorClassifier as cclFun } from '../utils/scale.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @see https://manifold.net/doc/mfd9/example__tanaka_contours.htm
|
|
10
|
+
*
|
|
11
|
+
* @module style
|
|
12
|
+
* @author Julien Gaffuri
|
|
13
|
+
*/
|
|
14
|
+
export class TanakaStyle {
|
|
15
|
+
/**
|
|
16
|
+
* @param {function(import('../core/Dataset.js').Cell):number} value Function that returns the value of a cell
|
|
17
|
+
* @param {Array.<number>} breaks The break values
|
|
18
|
+
* @param {Array.<string>} colors The colors, one more than the break values
|
|
19
|
+
* @param {object} opts
|
|
20
|
+
* @returns {Array.<import("../core/Style").Style>}
|
|
21
|
+
*/
|
|
22
|
+
static get(value, breaks, colors, opts = {}) {
|
|
23
|
+
//shadow colors
|
|
24
|
+
opts.colorDark = opts.colorDark || '#111'
|
|
25
|
+
opts.colorBright = opts.colorBright || '#ddd'
|
|
26
|
+
|
|
27
|
+
/** @type { function(number, number):number } */
|
|
28
|
+
opts.width =
|
|
29
|
+
opts.width ||
|
|
30
|
+
((sideValue, resolution, z) => {
|
|
31
|
+
const minWG = 1 * z
|
|
32
|
+
const maxWG = 4 * z
|
|
33
|
+
const step = (maxWG - minWG) / 3
|
|
34
|
+
return Math.min(minWG + (sideValue - 1) * step, maxWG)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
//make classifier
|
|
38
|
+
const classifier = clFun(breaks)
|
|
39
|
+
//make colors table
|
|
40
|
+
const colorsDict = {}
|
|
41
|
+
for (let i = 0; i < colors.length; i++) colorsDict[i + ''] = colors[i]
|
|
42
|
+
|
|
43
|
+
const cellStyle = new SquareColorCategoryWebGLStyle({
|
|
44
|
+
code: (cell) => classifier(value(cell)),
|
|
45
|
+
color: colorsDict,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const getSideValue = (side) => {
|
|
49
|
+
const cl1 = side.c1 ? classifier(value(side.c1)) : -1
|
|
50
|
+
const cl2 = side.c2 ? classifier(value(side.c2)) : -1
|
|
51
|
+
return cl1 - cl2
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The side style, for the shadow effect */
|
|
55
|
+
const sideStyle = new SideStyle({
|
|
56
|
+
//white or black, depending on orientation and value
|
|
57
|
+
color: (side) => {
|
|
58
|
+
const v = getSideValue(side)
|
|
59
|
+
if (v === 0) return
|
|
60
|
+
if (side.or === 'v') return v < 0 ? opts.colorBright : opts.colorDark
|
|
61
|
+
return v < 0 ? opts.colorDark : opts.colorBright
|
|
62
|
+
},
|
|
63
|
+
//width depends on the value, that is the number of classes of difference
|
|
64
|
+
width: (side, resolution, z) => opts.width(Math.abs(getSideValue(side)), resolution, z),
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return [cellStyle, sideStyle]
|
|
68
|
+
}
|
|
69
|
+
}
|