gridviz 2.1.7 → 2.1.9
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 +125 -124
- package/dist/gridviz.js +8021 -7991
- package/dist/gridviz.min.js +1 -1
- package/package.json +77 -77
- package/src/App.js +705 -705
- package/src/BackgroundLayer.js +182 -182
- package/src/BackgroundLayerWMS.js +131 -131
- package/src/Dataset.js +76 -76
- package/src/DatasetComponent.js +101 -101
- package/src/GeoCanvas.js +420 -420
- package/src/LabelLayer.js +190 -190
- package/src/Layer.js +103 -103
- package/src/Legend.js +86 -86
- package/src/LineLayer.js +150 -150
- package/src/Style.js +129 -129
- package/src/Tooltip.js +180 -172
- package/src/dataset/COGeo.js +24 -24
- package/src/dataset/CSVGrid.js +104 -104
- package/src/dataset/GeoTIFF.js +127 -127
- package/src/dataset/GridTile.js +41 -41
- package/src/dataset/LGrid.js +54 -54
- package/src/dataset/TiledGrid.js +264 -264
- package/src/index.js +71 -71
- package/src/legend/ColorCategoryLegend.js +124 -124
- package/src/legend/ColorDiscreteLegend.js +103 -103
- package/src/legend/ColorLegend.js +124 -124
- package/src/legend/SegmentOrientationLegend.js +87 -87
- package/src/legend/SegmentWidthLegend.js +97 -97
- package/src/legend/SizeLegend.js +141 -141
- package/src/style/CompositionStyle.js +300 -300
- package/src/style/ContourStyle.js +54 -54
- package/src/style/DotDensityStyle.js +156 -156
- package/src/style/IsoFenceStyle.js +262 -240
- package/src/style/JoyPlotStyle.js +135 -135
- package/src/style/LegoStyle.js +112 -112
- package/src/style/MosaicStyle.js +107 -107
- package/src/style/NinjaStarStyle.js +125 -125
- package/src/style/PillarStyle.js +230 -230
- package/src/style/SegmentStyle.js +142 -142
- package/src/style/ShapeColorSizeStyle.js +126 -126
- package/src/style/SideCatStyle.js +172 -172
- package/src/style/SideStyle.js +216 -216
- package/src/style/SquareColorCatWGLStyle.js +120 -120
- package/src/style/SquareColorWGLStyle.js +146 -146
- package/src/style/StrokeStyle.js +131 -131
- package/src/style/TanakaStyle.js +160 -160
- package/src/style/TextStyle.js +132 -132
- package/src/style/TimeSeriesStyle.js +250 -250
- package/src/utils/Utils.js +35 -35
- package/src/utils/WebGLRectangleColoring.js +106 -106
- package/src/utils/WebGLSquareColoring.js +118 -118
- package/src/utils/WebGLSquareColoringAdvanced.js +230 -230
- package/src/utils/WebGLSquareColoringCatAdvanced.js +133 -133
- package/src/utils/WebGLSquareColoringKS.js +153 -153
- package/src/utils/stretching.js +131 -131
- package/src/utils/webGLUtils.js +71 -71
- package/src/utils/zoomUtils.js +142 -142
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { Style } from '../Style.js'
|
|
5
|
-
import { makeWebGLCanvas } from '../utils/webGLUtils.js'
|
|
6
|
-
import { WebGLSquareColoringAdvanced } from '../utils/WebGLSquareColoringAdvanced.js'
|
|
7
|
-
import { monitor, monitorDuration } from '../utils/Utils.js'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Style based on webGL
|
|
11
|
-
* To show cells as colored squares, with computation of the colors on GPU side (faster than JavaScript side).
|
|
12
|
-
* Alls squares with the same size
|
|
13
|
-
*
|
|
14
|
-
* @author Julien Gaffuri
|
|
15
|
-
*/
|
|
16
|
-
export class SquareColorWGLStyle extends Style {
|
|
17
|
-
/** @param {object} opts */
|
|
18
|
-
constructor(opts) {
|
|
19
|
-
super(opts)
|
|
20
|
-
opts = opts || {}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* The name of the column/attribute of the tabular data where to retrieve the variable for color.
|
|
24
|
-
* @type {string} */
|
|
25
|
-
this.colorCol = opts.colorCol
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* A function returning the t value (within [0,1]) of the cell.
|
|
29
|
-
* @type {function(number,number,import("../Style").Stat):number} */
|
|
30
|
-
this.tFun = opts.tFun || ((v, r, s) => v / s.max)
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Distribution stretching method.
|
|
34
|
-
* The stretching is performed on GPU side (fragment shader).
|
|
35
|
-
* @type {{ fun:string, alpha:number }} */
|
|
36
|
-
this.stretching = opts.stretching
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* The sample of the color ramp.
|
|
40
|
-
* The color is computed on GPU side (fragment shader) based on those values (linear interpolation).
|
|
41
|
-
* @type {Array.<string>} */
|
|
42
|
-
this.colors =
|
|
43
|
-
opts.colors ||
|
|
44
|
-
[
|
|
45
|
-
'rgb(158, 1, 66)',
|
|
46
|
-
'rgb(248, 142, 83)',
|
|
47
|
-
'rgb(251, 248, 176)',
|
|
48
|
-
'rgb(137, 207, 165)',
|
|
49
|
-
'rgb(94, 79, 162)',
|
|
50
|
-
].reverse()
|
|
51
|
-
if (opts.color)
|
|
52
|
-
this.colors = [
|
|
53
|
-
opts.color(0),
|
|
54
|
-
opts.color(0.2),
|
|
55
|
-
opts.color(0.4),
|
|
56
|
-
opts.color(0.6),
|
|
57
|
-
opts.color(0.8),
|
|
58
|
-
opts.color(1),
|
|
59
|
-
]
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Define the opacity of the style, within [0,1].
|
|
63
|
-
* If this opacity is defined, the individual color opacity will be ignored.
|
|
64
|
-
* @type {function(number,number):number} */
|
|
65
|
-
this.opacity = opts.opacity // (r,zf) => ...
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* A function returning the size of the cells, in geographical unit. All cells have the same size.
|
|
69
|
-
* @type {function(number,number):number} */
|
|
70
|
-
this.size = opts.size // (resolution, zf) => ...
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* @param {Array.<import("../Dataset").Cell>} cells
|
|
75
|
-
* @param {number} r
|
|
76
|
-
* @param {import("../GeoCanvas").GeoCanvas} cg
|
|
77
|
-
*/
|
|
78
|
-
draw(cells, r, cg) {
|
|
79
|
-
if (monitor) monitorDuration('*** SquareColorWGLStyle draw')
|
|
80
|
-
|
|
81
|
-
//filter
|
|
82
|
-
if (this.filter) cells = cells.filter(this.filter)
|
|
83
|
-
|
|
84
|
-
//zoom factor
|
|
85
|
-
const zf = cg.getZf()
|
|
86
|
-
|
|
87
|
-
//compute color variable statistics
|
|
88
|
-
const statColor = Style.getStatistics(cells, (c) => c[this.colorCol], true)
|
|
89
|
-
if (monitor) monitorDuration(' color stats computation')
|
|
90
|
-
|
|
91
|
-
if (!statColor) return
|
|
92
|
-
|
|
93
|
-
//create canvas and webgl renderer
|
|
94
|
-
//for opacity control, see: https://webglfundamentals.org/webgl/lessons/webgl-and-alpha.html
|
|
95
|
-
const cvWGL = makeWebGLCanvas(
|
|
96
|
-
cg.w + '',
|
|
97
|
-
cg.h + '',
|
|
98
|
-
this.opacity != undefined ? { premultipliedAlpha: false } : undefined
|
|
99
|
-
)
|
|
100
|
-
if (!cvWGL) {
|
|
101
|
-
console.error('No webGL')
|
|
102
|
-
return
|
|
103
|
-
}
|
|
104
|
-
if (monitor) monitorDuration(' web GL canvas creation')
|
|
105
|
-
|
|
106
|
-
//add vertice and fragment data
|
|
107
|
-
const r2 = r / 2
|
|
108
|
-
const verticesBuffer = []
|
|
109
|
-
const tBuffer = []
|
|
110
|
-
for (let c of cells) {
|
|
111
|
-
const t = this.tFun(c[this.colorCol], r, statColor)
|
|
112
|
-
if (t == null || t == undefined) continue
|
|
113
|
-
verticesBuffer.push(c.x + r2, c.y + r2)
|
|
114
|
-
tBuffer.push(t > 1 ? 1 : t < 0 ? 0 : t)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (monitor) monitorDuration(' webgl drawing data preparation')
|
|
118
|
-
|
|
119
|
-
//compute pixel size
|
|
120
|
-
const sizeGeo = this.size ? this.size(r, zf) : r + 0.2 * zf
|
|
121
|
-
|
|
122
|
-
//compute opacity
|
|
123
|
-
const op = this.opacity ? this.opacity(r, zf) : undefined
|
|
124
|
-
|
|
125
|
-
//
|
|
126
|
-
const wgp = new WebGLSquareColoringAdvanced(cvWGL.gl, this.colors, this.stretching, sizeGeo / zf, op)
|
|
127
|
-
|
|
128
|
-
if (monitor) monitorDuration(' webgl program preparation')
|
|
129
|
-
|
|
130
|
-
//draw
|
|
131
|
-
wgp.draw(verticesBuffer, tBuffer, cg.getWebGLTransform())
|
|
132
|
-
|
|
133
|
-
if (monitor) monitorDuration(' webgl drawing')
|
|
134
|
-
|
|
135
|
-
//draw in canvas geo
|
|
136
|
-
cg.initCanvasTransform()
|
|
137
|
-
cg.ctx.drawImage(cvWGL.canvas, 0, 0)
|
|
138
|
-
|
|
139
|
-
if (monitor) monitorDuration(' canvas drawing')
|
|
140
|
-
|
|
141
|
-
//update legends
|
|
142
|
-
this.updateLegends({ style: this, r: r, zf: zf, sColor: statColor })
|
|
143
|
-
|
|
144
|
-
if (monitor) monitorDuration('*** SquareColorWGLStyle end draw')
|
|
145
|
-
}
|
|
146
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { Style } from '../Style.js'
|
|
5
|
+
import { makeWebGLCanvas } from '../utils/webGLUtils.js'
|
|
6
|
+
import { WebGLSquareColoringAdvanced } from '../utils/WebGLSquareColoringAdvanced.js'
|
|
7
|
+
import { monitor, monitorDuration } from '../utils/Utils.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Style based on webGL
|
|
11
|
+
* To show cells as colored squares, with computation of the colors on GPU side (faster than JavaScript side).
|
|
12
|
+
* Alls squares with the same size
|
|
13
|
+
*
|
|
14
|
+
* @author Julien Gaffuri
|
|
15
|
+
*/
|
|
16
|
+
export class SquareColorWGLStyle extends Style {
|
|
17
|
+
/** @param {object} opts */
|
|
18
|
+
constructor(opts) {
|
|
19
|
+
super(opts)
|
|
20
|
+
opts = opts || {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The name of the column/attribute of the tabular data where to retrieve the variable for color.
|
|
24
|
+
* @type {string} */
|
|
25
|
+
this.colorCol = opts.colorCol
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A function returning the t value (within [0,1]) of the cell.
|
|
29
|
+
* @type {function(number,number,import("../Style").Stat):number} */
|
|
30
|
+
this.tFun = opts.tFun || ((v, r, s) => v / s.max)
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Distribution stretching method.
|
|
34
|
+
* The stretching is performed on GPU side (fragment shader).
|
|
35
|
+
* @type {{ fun:string, alpha:number }} */
|
|
36
|
+
this.stretching = opts.stretching
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The sample of the color ramp.
|
|
40
|
+
* The color is computed on GPU side (fragment shader) based on those values (linear interpolation).
|
|
41
|
+
* @type {Array.<string>} */
|
|
42
|
+
this.colors =
|
|
43
|
+
opts.colors ||
|
|
44
|
+
[
|
|
45
|
+
'rgb(158, 1, 66)',
|
|
46
|
+
'rgb(248, 142, 83)',
|
|
47
|
+
'rgb(251, 248, 176)',
|
|
48
|
+
'rgb(137, 207, 165)',
|
|
49
|
+
'rgb(94, 79, 162)',
|
|
50
|
+
].reverse()
|
|
51
|
+
if (opts.color)
|
|
52
|
+
this.colors = [
|
|
53
|
+
opts.color(0),
|
|
54
|
+
opts.color(0.2),
|
|
55
|
+
opts.color(0.4),
|
|
56
|
+
opts.color(0.6),
|
|
57
|
+
opts.color(0.8),
|
|
58
|
+
opts.color(1),
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Define the opacity of the style, within [0,1].
|
|
63
|
+
* If this opacity is defined, the individual color opacity will be ignored.
|
|
64
|
+
* @type {function(number,number):number} */
|
|
65
|
+
this.opacity = opts.opacity // (r,zf) => ...
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A function returning the size of the cells, in geographical unit. All cells have the same size.
|
|
69
|
+
* @type {function(number,number):number} */
|
|
70
|
+
this.size = opts.size // (resolution, zf) => ...
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @param {Array.<import("../Dataset").Cell>} cells
|
|
75
|
+
* @param {number} r
|
|
76
|
+
* @param {import("../GeoCanvas").GeoCanvas} cg
|
|
77
|
+
*/
|
|
78
|
+
draw(cells, r, cg) {
|
|
79
|
+
if (monitor) monitorDuration('*** SquareColorWGLStyle draw')
|
|
80
|
+
|
|
81
|
+
//filter
|
|
82
|
+
if (this.filter) cells = cells.filter(this.filter)
|
|
83
|
+
|
|
84
|
+
//zoom factor
|
|
85
|
+
const zf = cg.getZf()
|
|
86
|
+
|
|
87
|
+
//compute color variable statistics
|
|
88
|
+
const statColor = Style.getStatistics(cells, (c) => c[this.colorCol], true)
|
|
89
|
+
if (monitor) monitorDuration(' color stats computation')
|
|
90
|
+
|
|
91
|
+
if (!statColor) return
|
|
92
|
+
|
|
93
|
+
//create canvas and webgl renderer
|
|
94
|
+
//for opacity control, see: https://webglfundamentals.org/webgl/lessons/webgl-and-alpha.html
|
|
95
|
+
const cvWGL = makeWebGLCanvas(
|
|
96
|
+
cg.w + '',
|
|
97
|
+
cg.h + '',
|
|
98
|
+
this.opacity != undefined ? { premultipliedAlpha: false } : undefined
|
|
99
|
+
)
|
|
100
|
+
if (!cvWGL) {
|
|
101
|
+
console.error('No webGL')
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
if (monitor) monitorDuration(' web GL canvas creation')
|
|
105
|
+
|
|
106
|
+
//add vertice and fragment data
|
|
107
|
+
const r2 = r / 2
|
|
108
|
+
const verticesBuffer = []
|
|
109
|
+
const tBuffer = []
|
|
110
|
+
for (let c of cells) {
|
|
111
|
+
const t = this.tFun(c[this.colorCol], r, statColor)
|
|
112
|
+
if (t == null || t == undefined) continue
|
|
113
|
+
verticesBuffer.push(c.x + r2, c.y + r2)
|
|
114
|
+
tBuffer.push(t > 1 ? 1 : t < 0 ? 0 : t)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (monitor) monitorDuration(' webgl drawing data preparation')
|
|
118
|
+
|
|
119
|
+
//compute pixel size
|
|
120
|
+
const sizeGeo = this.size ? this.size(r, zf) : r + 0.2 * zf
|
|
121
|
+
|
|
122
|
+
//compute opacity
|
|
123
|
+
const op = this.opacity ? this.opacity(r, zf) : undefined
|
|
124
|
+
|
|
125
|
+
//
|
|
126
|
+
const wgp = new WebGLSquareColoringAdvanced(cvWGL.gl, this.colors, this.stretching, sizeGeo / zf, op)
|
|
127
|
+
|
|
128
|
+
if (monitor) monitorDuration(' webgl program preparation')
|
|
129
|
+
|
|
130
|
+
//draw
|
|
131
|
+
wgp.draw(verticesBuffer, tBuffer, cg.getWebGLTransform())
|
|
132
|
+
|
|
133
|
+
if (monitor) monitorDuration(' webgl drawing')
|
|
134
|
+
|
|
135
|
+
//draw in canvas geo
|
|
136
|
+
cg.initCanvasTransform()
|
|
137
|
+
cg.ctx.drawImage(cvWGL.canvas, 0, 0)
|
|
138
|
+
|
|
139
|
+
if (monitor) monitorDuration(' canvas drawing')
|
|
140
|
+
|
|
141
|
+
//update legends
|
|
142
|
+
this.updateLegends({ style: this, r: r, zf: zf, sColor: statColor })
|
|
143
|
+
|
|
144
|
+
if (monitor) monitorDuration('*** SquareColorWGLStyle end draw')
|
|
145
|
+
}
|
|
146
|
+
}
|
package/src/style/StrokeStyle.js
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
//@ts-check
|
|
2
|
-
'use strict'
|
|
3
|
-
|
|
4
|
-
import { Style } from '../Style.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
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
|
-
/** The name of the column/attribute of the tabular data where to retrieve the variable for color.
|
|
17
|
-
* @type {string} */
|
|
18
|
-
this.strokeColorCol = opts.strokeColorCol
|
|
19
|
-
|
|
20
|
-
/** A function returning the color of the stroke.
|
|
21
|
-
* @type {function(number,number,import("../Style").Stat|undefined):string} */
|
|
22
|
-
this.strokeColor = opts.strokeColor || (() => '#666')
|
|
23
|
-
|
|
24
|
-
/** The name of the column/attribute of the tabular data where to retrieve the variable for size.
|
|
25
|
-
* @type {string} */
|
|
26
|
-
this.sizeCol = opts.sizeCol
|
|
27
|
-
|
|
28
|
-
/** A function returning the size of a cell in geographical unit.
|
|
29
|
-
* @type {function(number,number,import("../Style").Stat|undefined,number):number} */
|
|
30
|
-
this.size = opts.size
|
|
31
|
-
|
|
32
|
-
/** The stroke line width, in pixels.
|
|
33
|
-
* @type {string} */
|
|
34
|
-
this.strokeWidthCol = opts.strokeWidthCol
|
|
35
|
-
|
|
36
|
-
/** The stroke line width in geographical unit.
|
|
37
|
-
* @type {function(number,number,import("../Style").Stat|undefined,number):number} */
|
|
38
|
-
this.strokeWidth = opts.strokeWidth // (v,r,s,z)=>...
|
|
39
|
-
|
|
40
|
-
/** A function returning the shape of a cell.
|
|
41
|
-
* @type {function(import("../Dataset").Cell):import("../Style").Shape} */
|
|
42
|
-
this.shape = opts.shape || (() => 'square')
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Draw cells as squares, with various colors and size.
|
|
47
|
-
*
|
|
48
|
-
* @param {Array.<import("../Dataset").Cell>} cells
|
|
49
|
-
* @param {number} r
|
|
50
|
-
* @param {import("../GeoCanvas").GeoCanvas} cg
|
|
51
|
-
*/
|
|
52
|
-
draw(cells, r, cg) {
|
|
53
|
-
//filter
|
|
54
|
-
if (this.filter) cells = cells.filter(this.filter)
|
|
55
|
-
|
|
56
|
-
//zoom factor
|
|
57
|
-
const zf = cg.getZf()
|
|
58
|
-
|
|
59
|
-
let statColor
|
|
60
|
-
if (this.strokeColorCol) statColor = Style.getStatistics(cells, (c) => c[this.strokeColorCol], true)
|
|
61
|
-
|
|
62
|
-
let statSize
|
|
63
|
-
if (this.sizeCol) statSize = Style.getStatistics(cells, (c) => c[this.sizeCol], true)
|
|
64
|
-
|
|
65
|
-
let statWidth
|
|
66
|
-
if (this.strokeWidthCol) statWidth = Style.getStatistics(cells, (c) => c[this.strokeWidthCol], true)
|
|
67
|
-
|
|
68
|
-
//draw with HTML canvas
|
|
69
|
-
//in geo coordinates
|
|
70
|
-
cg.setCanvasTransform()
|
|
71
|
-
|
|
72
|
-
const r2 = r * 0.5
|
|
73
|
-
for (let cell of cells) {
|
|
74
|
-
//color
|
|
75
|
-
const col = this.strokeColor
|
|
76
|
-
? this.strokeColor(cell[this.strokeColorCol], r, statColor)
|
|
77
|
-
: undefined
|
|
78
|
-
if (!col || col === 'none') continue
|
|
79
|
-
cg.ctx.strokeStyle = col
|
|
80
|
-
|
|
81
|
-
//size
|
|
82
|
-
/** @type {function(number,number,import("../Style").Stat|undefined,number):number} */
|
|
83
|
-
let s_ = this.size || (() => r)
|
|
84
|
-
//size - in geo unit
|
|
85
|
-
const sG = s_(cell[this.sizeCol], r, statSize, zf)
|
|
86
|
-
|
|
87
|
-
//width
|
|
88
|
-
const wi = this.strokeWidth
|
|
89
|
-
? this.strokeWidth(cell[this.strokeWidthCol], r, statWidth, zf)
|
|
90
|
-
: 1 * zf
|
|
91
|
-
if (!wi || wi <= 0) continue
|
|
92
|
-
cg.ctx.lineWidth = wi
|
|
93
|
-
|
|
94
|
-
//shape
|
|
95
|
-
const shape = this.shape ? this.shape(cell) : 'square'
|
|
96
|
-
if (shape === 'none') continue
|
|
97
|
-
|
|
98
|
-
//get offset
|
|
99
|
-
const offset = this.offset(cell, r, zf)
|
|
100
|
-
|
|
101
|
-
if (shape === 'square') {
|
|
102
|
-
//draw square
|
|
103
|
-
const d = r * (1 - sG / r) * 0.5
|
|
104
|
-
cg.ctx.beginPath()
|
|
105
|
-
cg.ctx.rect(cell.x + d + offset.dx, cell.y + d + offset.dy, sG, sG)
|
|
106
|
-
cg.ctx.stroke()
|
|
107
|
-
} else if (shape === 'circle') {
|
|
108
|
-
//draw circle
|
|
109
|
-
cg.ctx.beginPath()
|
|
110
|
-
cg.ctx.arc(cell.x + r2 + offset.dx, cell.y + r2 + offset.dy, sG * 0.5, 0, 2 * Math.PI, false)
|
|
111
|
-
cg.ctx.stroke()
|
|
112
|
-
} else if (shape === 'diamond') {
|
|
113
|
-
const s2 = sG * 0.5
|
|
114
|
-
cg.ctx.beginPath()
|
|
115
|
-
cg.ctx.moveTo(cell.x + r2 - s2, cell.y + r2)
|
|
116
|
-
cg.ctx.lineTo(cell.x + r2, cell.y + r2 + s2)
|
|
117
|
-
cg.ctx.lineTo(cell.x + r2 + s2, cell.y + r2)
|
|
118
|
-
cg.ctx.lineTo(cell.x + r2, cell.y + r2 - s2)
|
|
119
|
-
cg.ctx.lineTo(cell.x + r2 - s2, cell.y + r2)
|
|
120
|
-
cg.ctx.stroke()
|
|
121
|
-
} else if (shape === 'donut') {
|
|
122
|
-
console.error('Not implemented')
|
|
123
|
-
} else {
|
|
124
|
-
throw new Error('Unexpected shape:' + shape)
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
//update legends
|
|
129
|
-
//this.updateLegends({ style: this, r: resolution, zf: zf, sSize: statSize, sColor: statColor });
|
|
130
|
-
}
|
|
131
|
-
}
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { Style } from '../Style.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
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
|
+
/** The name of the column/attribute of the tabular data where to retrieve the variable for color.
|
|
17
|
+
* @type {string} */
|
|
18
|
+
this.strokeColorCol = opts.strokeColorCol
|
|
19
|
+
|
|
20
|
+
/** A function returning the color of the stroke.
|
|
21
|
+
* @type {function(number,number,import("../Style").Stat|undefined):string} */
|
|
22
|
+
this.strokeColor = opts.strokeColor || (() => '#666')
|
|
23
|
+
|
|
24
|
+
/** The name of the column/attribute of the tabular data where to retrieve the variable for size.
|
|
25
|
+
* @type {string} */
|
|
26
|
+
this.sizeCol = opts.sizeCol
|
|
27
|
+
|
|
28
|
+
/** A function returning the size of a cell in geographical unit.
|
|
29
|
+
* @type {function(number,number,import("../Style").Stat|undefined,number):number} */
|
|
30
|
+
this.size = opts.size
|
|
31
|
+
|
|
32
|
+
/** The stroke line width, in pixels.
|
|
33
|
+
* @type {string} */
|
|
34
|
+
this.strokeWidthCol = opts.strokeWidthCol
|
|
35
|
+
|
|
36
|
+
/** The stroke line width in geographical unit.
|
|
37
|
+
* @type {function(number,number,import("../Style").Stat|undefined,number):number} */
|
|
38
|
+
this.strokeWidth = opts.strokeWidth // (v,r,s,z)=>...
|
|
39
|
+
|
|
40
|
+
/** A function returning the shape of a cell.
|
|
41
|
+
* @type {function(import("../Dataset").Cell):import("../Style").Shape} */
|
|
42
|
+
this.shape = opts.shape || (() => 'square')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Draw cells as squares, with various colors and size.
|
|
47
|
+
*
|
|
48
|
+
* @param {Array.<import("../Dataset").Cell>} cells
|
|
49
|
+
* @param {number} r
|
|
50
|
+
* @param {import("../GeoCanvas").GeoCanvas} cg
|
|
51
|
+
*/
|
|
52
|
+
draw(cells, r, cg) {
|
|
53
|
+
//filter
|
|
54
|
+
if (this.filter) cells = cells.filter(this.filter)
|
|
55
|
+
|
|
56
|
+
//zoom factor
|
|
57
|
+
const zf = cg.getZf()
|
|
58
|
+
|
|
59
|
+
let statColor
|
|
60
|
+
if (this.strokeColorCol) statColor = Style.getStatistics(cells, (c) => c[this.strokeColorCol], true)
|
|
61
|
+
|
|
62
|
+
let statSize
|
|
63
|
+
if (this.sizeCol) statSize = Style.getStatistics(cells, (c) => c[this.sizeCol], true)
|
|
64
|
+
|
|
65
|
+
let statWidth
|
|
66
|
+
if (this.strokeWidthCol) statWidth = Style.getStatistics(cells, (c) => c[this.strokeWidthCol], true)
|
|
67
|
+
|
|
68
|
+
//draw with HTML canvas
|
|
69
|
+
//in geo coordinates
|
|
70
|
+
cg.setCanvasTransform()
|
|
71
|
+
|
|
72
|
+
const r2 = r * 0.5
|
|
73
|
+
for (let cell of cells) {
|
|
74
|
+
//color
|
|
75
|
+
const col = this.strokeColor
|
|
76
|
+
? this.strokeColor(cell[this.strokeColorCol], r, statColor)
|
|
77
|
+
: undefined
|
|
78
|
+
if (!col || col === 'none') continue
|
|
79
|
+
cg.ctx.strokeStyle = col
|
|
80
|
+
|
|
81
|
+
//size
|
|
82
|
+
/** @type {function(number,number,import("../Style").Stat|undefined,number):number} */
|
|
83
|
+
let s_ = this.size || (() => r)
|
|
84
|
+
//size - in geo unit
|
|
85
|
+
const sG = s_(cell[this.sizeCol], r, statSize, zf)
|
|
86
|
+
|
|
87
|
+
//width
|
|
88
|
+
const wi = this.strokeWidth
|
|
89
|
+
? this.strokeWidth(cell[this.strokeWidthCol], r, statWidth, zf)
|
|
90
|
+
: 1 * zf
|
|
91
|
+
if (!wi || wi <= 0) continue
|
|
92
|
+
cg.ctx.lineWidth = wi
|
|
93
|
+
|
|
94
|
+
//shape
|
|
95
|
+
const shape = this.shape ? this.shape(cell) : 'square'
|
|
96
|
+
if (shape === 'none') continue
|
|
97
|
+
|
|
98
|
+
//get offset
|
|
99
|
+
const offset = this.offset(cell, r, zf)
|
|
100
|
+
|
|
101
|
+
if (shape === 'square') {
|
|
102
|
+
//draw square
|
|
103
|
+
const d = r * (1 - sG / r) * 0.5
|
|
104
|
+
cg.ctx.beginPath()
|
|
105
|
+
cg.ctx.rect(cell.x + d + offset.dx, cell.y + d + offset.dy, sG, sG)
|
|
106
|
+
cg.ctx.stroke()
|
|
107
|
+
} else if (shape === 'circle') {
|
|
108
|
+
//draw circle
|
|
109
|
+
cg.ctx.beginPath()
|
|
110
|
+
cg.ctx.arc(cell.x + r2 + offset.dx, cell.y + r2 + offset.dy, sG * 0.5, 0, 2 * Math.PI, false)
|
|
111
|
+
cg.ctx.stroke()
|
|
112
|
+
} else if (shape === 'diamond') {
|
|
113
|
+
const s2 = sG * 0.5
|
|
114
|
+
cg.ctx.beginPath()
|
|
115
|
+
cg.ctx.moveTo(cell.x + r2 - s2, cell.y + r2)
|
|
116
|
+
cg.ctx.lineTo(cell.x + r2, cell.y + r2 + s2)
|
|
117
|
+
cg.ctx.lineTo(cell.x + r2 + s2, cell.y + r2)
|
|
118
|
+
cg.ctx.lineTo(cell.x + r2, cell.y + r2 - s2)
|
|
119
|
+
cg.ctx.lineTo(cell.x + r2 - s2, cell.y + r2)
|
|
120
|
+
cg.ctx.stroke()
|
|
121
|
+
} else if (shape === 'donut') {
|
|
122
|
+
console.error('Not implemented')
|
|
123
|
+
} else {
|
|
124
|
+
throw new Error('Unexpected shape:' + shape)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
//update legends
|
|
129
|
+
//this.updateLegends({ style: this, r: resolution, zf: zf, sSize: statSize, sColor: statColor });
|
|
130
|
+
}
|
|
131
|
+
}
|