gridviz 2.0.28 → 2.0.31
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/dist/gridviz.js +34368 -0
- package/dist/gridviz.min.js +1 -1
- package/package.json +10 -6
- package/src/App.js +654 -0
- package/src/BackgroundLayer.js +182 -0
- package/src/BackgroundLayerWMS.js +131 -0
- package/src/Dataset.js +76 -0
- package/src/DatasetComponent.js +103 -0
- package/src/GeoCanvas.js +419 -0
- package/src/LabelLayer.js +190 -0
- package/src/Layer.js +92 -0
- package/src/Legend.js +86 -0
- package/src/LineLayer.js +150 -0
- package/src/Style.js +114 -0
- package/src/Tooltip.js +150 -0
- package/src/dataset/COGeo.js +24 -0
- package/src/dataset/CSVGrid.js +104 -0
- package/src/dataset/GeoTIFF.js +127 -0
- package/src/dataset/GridTile.js +41 -0
- package/src/dataset/ParquetGrid.js +122 -0
- package/src/dataset/TiledGrid.js +296 -0
- package/src/index.js +177 -0
- package/src/legend/ColorCategoryLegend.js +124 -0
- package/src/legend/ColorDiscreteLegend.js +105 -0
- package/src/legend/ColorLegend.js +125 -0
- package/src/legend/SegmentOrientationLegend.js +87 -0
- package/src/legend/SegmentWidthLegend.js +97 -0
- package/src/legend/SizeLegend.js +141 -0
- package/src/style/CompositionStyle.js +300 -0
- package/src/style/ContourStyle.js +54 -0
- package/src/style/DotDensityStyle.js +156 -0
- package/src/style/JoyPlotStyle.js +135 -0
- package/src/style/KernelSmoothingOldStyle.js +221 -0
- package/src/style/KernelSmoothingStyle.js +115 -0
- package/src/style/KernelSmoothingWGLStyle.js +130 -0
- package/src/style/LegoStyle.js +112 -0
- package/src/style/MosaicStyle.js +107 -0
- package/src/style/NinjaStarStyle.js +124 -0
- package/src/style/PillarStyle.js +230 -0
- package/src/style/SegmentStyle.js +142 -0
- package/src/style/ShapeColorSizeStyle.js +126 -0
- package/src/style/SideCatStyle.js +172 -0
- package/src/style/SideStyle.js +218 -0
- package/src/style/SquareColorCatWGLStyle.js +120 -0
- package/src/style/SquareColorWGLStyle.js +146 -0
- package/src/style/StrokeStyle.js +131 -0
- package/src/style/TanakaStyle.js +160 -0
- package/src/style/TextStyle.js +132 -0
- package/src/utils/Utils.js +35 -0
- package/src/utils/WebGLRectangleColoring.js +106 -0
- package/src/utils/WebGLSquareColoring.js +118 -0
- package/src/utils/WebGLSquareColoringAdvanced.js +230 -0
- package/src/utils/WebGLSquareColoringCatAdvanced.js +133 -0
- package/src/utils/WebGLSquareColoringKS.js +153 -0
- package/src/utils/stretching.js +131 -0
- package/src/utils/webGLUtils.js +71 -0
- package/src/utils/zoomUtils.js +142 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { Style } from '../Style.js'
|
|
5
|
+
import { makeWebGLCanvas } from '../utils/webGLUtils.js'
|
|
6
|
+
import { WebGLSquareColoringCatAdvanced } from '../utils/WebGLSquareColoringCatAdvanced.js'
|
|
7
|
+
import { monitor, monitorDuration } from '../utils/Utils.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Style based on webGL
|
|
11
|
+
* To show cells as colored squares, from categories.
|
|
12
|
+
* Alls squares with the same size
|
|
13
|
+
*
|
|
14
|
+
* @author Julien Gaffuri
|
|
15
|
+
*/
|
|
16
|
+
export class SquareColorCatWGLStyle 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 category of the cell, for coloring.
|
|
24
|
+
* @type {string} */
|
|
25
|
+
this.colorCol = opts.colorCol
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The dictionary (string -> color) which give the color of each category.
|
|
29
|
+
* @type {object} */
|
|
30
|
+
opts.color = opts.color || undefined
|
|
31
|
+
|
|
32
|
+
/** @type { Array.<string> } @private */
|
|
33
|
+
const keys = Object.keys(opts.color)
|
|
34
|
+
|
|
35
|
+
/** @type { object } @private */
|
|
36
|
+
this.catToI = {}
|
|
37
|
+
for (let i = 0; i < keys.length; i++) this.catToI[keys[i]] = i + ''
|
|
38
|
+
|
|
39
|
+
/** @type { Array.<string> } @private */
|
|
40
|
+
this.colors = []
|
|
41
|
+
for (let i = 0; i < keys.length; i++) {
|
|
42
|
+
this.colors.push(opts.color['' + keys[i]])
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A function returning the size of the cells, in geographical unit. All cells have the same size.
|
|
47
|
+
* @type {function(number,number):number} */
|
|
48
|
+
this.size = opts.size // (resolution, zf) => ...
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @private
|
|
52
|
+
* @type { WebGLSquareColoringCatAdvanced } */
|
|
53
|
+
this.wgp = new WebGLSquareColoringCatAdvanced(this.colors)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {Array.<import("../Dataset").Cell>} cells
|
|
58
|
+
* @param {number} r
|
|
59
|
+
* @param {import("../GeoCanvas").GeoCanvas} cg
|
|
60
|
+
*/
|
|
61
|
+
draw(cells, r, cg) {
|
|
62
|
+
if (monitor) monitorDuration('*** SquareColorCatWGLStyle draw')
|
|
63
|
+
|
|
64
|
+
//filter
|
|
65
|
+
if (this.filter) cells = cells.filter(this.filter)
|
|
66
|
+
|
|
67
|
+
//zoom factor
|
|
68
|
+
const zf = cg.getZf()
|
|
69
|
+
|
|
70
|
+
//add vertice and fragment data
|
|
71
|
+
const r2 = r / 2
|
|
72
|
+
let c,
|
|
73
|
+
nb = cells.length
|
|
74
|
+
const verticesBuffer = []
|
|
75
|
+
const iBuffer = []
|
|
76
|
+
for (let i = 0; i < nb; i++) {
|
|
77
|
+
c = cells[i]
|
|
78
|
+
const cat = c[this.colorCol]
|
|
79
|
+
if (cat == undefined) {
|
|
80
|
+
console.log('Unexpected category: ' + cat)
|
|
81
|
+
continue
|
|
82
|
+
}
|
|
83
|
+
/** @type {number} */
|
|
84
|
+
const i_ = this.catToI[cat]
|
|
85
|
+
if (isNaN(+i_)) {
|
|
86
|
+
console.log('Unexpected category index: ' + cat + ' ' + i_)
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
verticesBuffer.push(c.x + r2, c.y + r2)
|
|
90
|
+
iBuffer.push(+i_)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (monitor) monitorDuration(' webgl program inputs preparation')
|
|
94
|
+
|
|
95
|
+
//create canvas and webgl renderer
|
|
96
|
+
const cvWGL = makeWebGLCanvas(cg.w + '', cg.h + '')
|
|
97
|
+
if (!cvWGL) {
|
|
98
|
+
console.error('No webGL')
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
if (monitor) monitorDuration(' web GL canvas creation')
|
|
102
|
+
|
|
103
|
+
//draw
|
|
104
|
+
const sizeGeo = this.size ? this.size(r, zf) : r + 0.2 * zf
|
|
105
|
+
this.wgp.draw(cvWGL.gl, verticesBuffer, iBuffer, cg.getWebGLTransform(), sizeGeo / zf)
|
|
106
|
+
|
|
107
|
+
if (monitor) monitorDuration(' webgl drawing')
|
|
108
|
+
|
|
109
|
+
//draw in canvas geo
|
|
110
|
+
cg.initCanvasTransform()
|
|
111
|
+
cg.ctx.drawImage(cvWGL.canvas, 0, 0)
|
|
112
|
+
|
|
113
|
+
if (monitor) monitorDuration(' canvas drawing')
|
|
114
|
+
|
|
115
|
+
//update legends
|
|
116
|
+
this.updateLegends({ style: this, r: r, zf: zf })
|
|
117
|
+
|
|
118
|
+
if (monitor) monitorDuration('*** SquareColorCatWGLStyle end draw')
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { SquareColorWGLStyle } from './SquareColorWGLStyle.js'
|
|
5
|
+
import { SideStyle } from './SideStyle.js'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @see https://manifold.net/doc/mfd9/example__tanaka_contours.htm
|
|
10
|
+
*
|
|
11
|
+
* @author Julien Gaffuri
|
|
12
|
+
*/
|
|
13
|
+
export class TanakaStyle {
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} col
|
|
16
|
+
* @param {object} opts
|
|
17
|
+
* @returns {Array.<import("../Style").Style>}
|
|
18
|
+
*/
|
|
19
|
+
static get(col, opts) {
|
|
20
|
+
opts = opts || {}
|
|
21
|
+
|
|
22
|
+
//get colors from d3 ramps, if 'nb' is specified
|
|
23
|
+
if (opts.nb != undefined) {
|
|
24
|
+
if (opts.nb < 2) {
|
|
25
|
+
console.error('unexpected number of colors in tanaka (<2): ' + opts.nb)
|
|
26
|
+
opts.nb = 2
|
|
27
|
+
}
|
|
28
|
+
if (!opts.color) {
|
|
29
|
+
console.error('color function not defined in tanaka')
|
|
30
|
+
opts.color = () => 'gray'
|
|
31
|
+
}
|
|
32
|
+
opts.colors = []
|
|
33
|
+
for (let i = 0; i < opts.nb; i++) opts.colors.push(opts.color(i / (opts.nb - 1)))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The colors.
|
|
38
|
+
* @type {Array.<string>} */
|
|
39
|
+
opts.colors = opts.colors || ['#a9bb9e', '#c9dcaa', '#fde89f', '#f9a579', '#eb444b']
|
|
40
|
+
const nb = opts.colors.length
|
|
41
|
+
|
|
42
|
+
/** A function to compute 't' from the value v
|
|
43
|
+
* @type {function(number,number,import("../Style").Stat):number} */
|
|
44
|
+
opts.tFun = opts.tFun || ((v, r, s) => (v - s.min) / (s.max - s.min))
|
|
45
|
+
|
|
46
|
+
//shadow colors
|
|
47
|
+
opts.colDark = opts.colDark || '#111'
|
|
48
|
+
opts.colBright = opts.colBright || '#ddd'
|
|
49
|
+
|
|
50
|
+
//width of the segment (share of the resolution)
|
|
51
|
+
opts.widthFactor = opts.widthFactor || 0.08
|
|
52
|
+
|
|
53
|
+
//shading
|
|
54
|
+
opts.newShading = opts.newShading
|
|
55
|
+
opts.newShadingWidthPix = opts.newShadingWidthPix || 2
|
|
56
|
+
//transparency value, within [0,1]
|
|
57
|
+
opts.newShadingTr =
|
|
58
|
+
opts.newShadingTr ||
|
|
59
|
+
((sideValue, sideStat) =>
|
|
60
|
+
Math.abs(sideValue) / Math.max(Math.abs(sideStat.min), Math.abs(sideStat.max)))
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {number} t A cell t value, within [0,1].
|
|
64
|
+
* @returns the class number for the value
|
|
65
|
+
*/
|
|
66
|
+
const getClass = (t) => {
|
|
67
|
+
if (isNaN(t) || t == undefined) {
|
|
68
|
+
console.error('Unexpected t value 1: ' + t)
|
|
69
|
+
return -9
|
|
70
|
+
}
|
|
71
|
+
for (let i = 0; i < nb; i++) if (t <= (i + 1) / nb) return i
|
|
72
|
+
console.error('Unexpected t value 2: ' + t)
|
|
73
|
+
return -9
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const colStyle = new SquareColorWGLStyle({
|
|
77
|
+
colorCol: col,
|
|
78
|
+
colors: opts.colors,
|
|
79
|
+
tFun: (v, r, s) => {
|
|
80
|
+
const t = opts.tFun(v, r, s)
|
|
81
|
+
const c = getClass(t)
|
|
82
|
+
return c / (nb - 1)
|
|
83
|
+
},
|
|
84
|
+
//stretching: { fun: "expRev", alpha: -7 },
|
|
85
|
+
size: (r, zf) => r + 0.5 * zf, //that is to ensure no gap between same class cells is visible
|
|
86
|
+
filter: opts.filter,
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
/*
|
|
90
|
+
if no web gl:
|
|
91
|
+
const colStyle = new ShapeColorSizeStyle({
|
|
92
|
+
colorCol: col,
|
|
93
|
+
//the color corresponding to the class
|
|
94
|
+
color: (v, r, s, zf) => {
|
|
95
|
+
if (v == 0 && opts.tFun && isNaN(opts.tFun(v, r, s)))
|
|
96
|
+
return undefined
|
|
97
|
+
return opts.colors[getClass(opts.tFun ? opts.tFun(v, r, s) : v)]
|
|
98
|
+
},
|
|
99
|
+
shape: () => "square",
|
|
100
|
+
size: (v, r, s, zf) => r + 0.5 * zf, //that is to ensure no gap between same class cells is visible
|
|
101
|
+
})
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
/** The side style, for the shadow effect */
|
|
105
|
+
const sideStyle = new SideStyle({
|
|
106
|
+
valueCol: col,
|
|
107
|
+
value: (v1, v2, r, s, zf) => {
|
|
108
|
+
//compute the number of classes of difference
|
|
109
|
+
if (v1 === undefined && v2 === undefined) return 0
|
|
110
|
+
else if (v2 === undefined) {
|
|
111
|
+
const t = opts.tFun(v1, r, s)
|
|
112
|
+
if (t == undefined || isNaN(t)) throw new Error('Unexpected value: ' + v1 + ' - ' + t)
|
|
113
|
+
const c = getClass(t)
|
|
114
|
+
return c + 1
|
|
115
|
+
} else if (v1 === undefined) {
|
|
116
|
+
const t = opts.tFun(v2, r, s)
|
|
117
|
+
if (t == undefined || isNaN(t)) throw new Error('Unexpected value: ' + v2 + ' - ' + t)
|
|
118
|
+
const c = getClass(t)
|
|
119
|
+
return -c - 1
|
|
120
|
+
}
|
|
121
|
+
const t1 = opts.tFun(v1, r, s)
|
|
122
|
+
if (t1 == undefined || isNaN(t1)) throw new Error('Unexpected value: ' + v1 + ' - ' + t1)
|
|
123
|
+
const t2 = opts.tFun(v2, r, s)
|
|
124
|
+
if (t2 == undefined || isNaN(t2)) throw new Error('Unexpected value: ' + v2 + ' - ' + t2)
|
|
125
|
+
const c1 = getClass(t1)
|
|
126
|
+
const c2 = getClass(t2)
|
|
127
|
+
return -c2 + c1
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
color: opts.newShading
|
|
131
|
+
? //black with transparency depending on difference
|
|
132
|
+
(side, r, s, z) => {
|
|
133
|
+
const tr = opts.newShadingTr(side.value, s)
|
|
134
|
+
return (side.value > 0 && side.or === 'h') || (side.value < 0 && side.or === 'v')
|
|
135
|
+
? 'rgba(255,255,100,' + tr + ')'
|
|
136
|
+
: 'rgba(0,0,0,' + tr + ')'
|
|
137
|
+
}
|
|
138
|
+
: //white or black, depending on orientation and value
|
|
139
|
+
(side, r, s, z) => {
|
|
140
|
+
if (side.value === 0) return
|
|
141
|
+
//return "gray"
|
|
142
|
+
if (side.or === 'v') return side.value < 0 ? opts.colBright : opts.colDark
|
|
143
|
+
return side.value < 0 ? opts.colDark : opts.colBright
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
width: opts.newShading
|
|
147
|
+
? //fill size
|
|
148
|
+
(side, r, s, z) => {
|
|
149
|
+
return opts.newShadingWidthPix * z
|
|
150
|
+
}
|
|
151
|
+
: //width depends on the value, that is the number of classes of difference
|
|
152
|
+
(side, r, s, z) =>
|
|
153
|
+
opts.widthFactor * r * Math.abs(side.value) * (side.or === 'v' ? 0.5 : 1),
|
|
154
|
+
|
|
155
|
+
filter: opts.filter,
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
return [colStyle, sideStyle]
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//@ts-check
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
import { Style } from '../Style.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @author Julien Gaffuri
|
|
9
|
+
*/
|
|
10
|
+
export class TextStyle 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 text.
|
|
17
|
+
* @type {string} */
|
|
18
|
+
this.textCol = opts.textCol
|
|
19
|
+
|
|
20
|
+
/** A function returning the text of a cell.
|
|
21
|
+
* @type {function(number,number,import("../Style").Stat|undefined):string} */
|
|
22
|
+
this.text = opts.text || ((v, r, s, z) => 'X')
|
|
23
|
+
|
|
24
|
+
/** The name of the column/attribute of the tabular data where to retrieve the variable for color.
|
|
25
|
+
* @type {string} */
|
|
26
|
+
this.colorCol = opts.colorCol
|
|
27
|
+
|
|
28
|
+
/** A function returning the color of the cell.
|
|
29
|
+
* @type {function(number,number,import("../Style").Stat|undefined):string} */
|
|
30
|
+
this.color = opts.color || (() => '#EA6BAC')
|
|
31
|
+
|
|
32
|
+
/** The name of the column/attribute of the tabular data where to retrieve the variable for font size.
|
|
33
|
+
* @type {string} */
|
|
34
|
+
this.fontSizeCol = opts.fontSizeCol
|
|
35
|
+
|
|
36
|
+
/** A function returning the font size of a cell in geo unit.
|
|
37
|
+
* @type {function(number,number,import("../Style").Stat|undefined,number):number} */
|
|
38
|
+
this.fontSize = opts.fontSize || ((v, r, s, z) => r * 0.8)
|
|
39
|
+
|
|
40
|
+
/** The text font family.
|
|
41
|
+
* @type {string} */
|
|
42
|
+
this.fontFamily = opts.fontFamily || 'Arial'
|
|
43
|
+
|
|
44
|
+
/** The text font weight.
|
|
45
|
+
* @type {string} */
|
|
46
|
+
this.fontWeight = opts.fontWeight || 'bold'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Draw cells as text.
|
|
51
|
+
*
|
|
52
|
+
* @param {Array.<import("../Dataset").Cell>} cells
|
|
53
|
+
* @param {number} r
|
|
54
|
+
* @param {import("../GeoCanvas").GeoCanvas} cg
|
|
55
|
+
*/
|
|
56
|
+
draw(cells, r, cg) {
|
|
57
|
+
//filter
|
|
58
|
+
if (this.filter) cells = cells.filter(this.filter)
|
|
59
|
+
|
|
60
|
+
//zoom factor
|
|
61
|
+
const zf = cg.getZf()
|
|
62
|
+
|
|
63
|
+
let statText
|
|
64
|
+
if (this.textCol) {
|
|
65
|
+
//compute text variable statistics
|
|
66
|
+
statText = Style.getStatistics(cells, (c) => c[this.textCol], true)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
let statColor
|
|
70
|
+
if (this.colorCol) {
|
|
71
|
+
//compute color variable statistics
|
|
72
|
+
statColor = Style.getStatistics(cells, (c) => c[this.colorCol], true)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let statFontSize
|
|
76
|
+
if (this.fontSizeCol) {
|
|
77
|
+
//if size is used, sort cells by size so that the biggest are drawn first
|
|
78
|
+
cells.sort((c1, c2) => c2[this.fontSizeCol] - c1[this.fontSizeCol])
|
|
79
|
+
//and compute size variable statistics
|
|
80
|
+
statFontSize = Style.getStatistics(cells, (c) => c[this.fontSizeCol], true)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
//draw with HTML canvas
|
|
84
|
+
//in screen coordinates
|
|
85
|
+
cg.initCanvasTransform()
|
|
86
|
+
|
|
87
|
+
for (let cell of cells) {
|
|
88
|
+
//get cell text
|
|
89
|
+
const text = this.text ? this.text(cell[this.textCol], r, statText) : undefined
|
|
90
|
+
if (text == undefined || text == null || text + '' === '') continue
|
|
91
|
+
|
|
92
|
+
//color
|
|
93
|
+
const col = this.color ? this.color(cell[this.colorCol], r, statColor) : undefined
|
|
94
|
+
if (!col) continue
|
|
95
|
+
cg.ctx.fillStyle = col
|
|
96
|
+
|
|
97
|
+
//font size
|
|
98
|
+
//size - in pixel unit
|
|
99
|
+
const fontSizePix = this.fontSize(cell[this.fontSizeCol], r, statFontSize, zf) / zf
|
|
100
|
+
|
|
101
|
+
//set font
|
|
102
|
+
const fontFamily = this.fontFamily || 'Arial'
|
|
103
|
+
const fontWeight = this.fontWeight || 'bold'
|
|
104
|
+
cg.ctx.font = fontWeight + ' ' + fontSizePix + 'px ' + fontFamily
|
|
105
|
+
|
|
106
|
+
//get offset
|
|
107
|
+
const offset = this.offset(cell, r, zf)
|
|
108
|
+
|
|
109
|
+
//text position
|
|
110
|
+
cg.ctx.textAlign = 'center'
|
|
111
|
+
const tx = cg.geoToPixX(cell.x + r * 0.5 + offset.dx)
|
|
112
|
+
const ty = cg.geoToPixY(cell.y + r * 0.5 + offset.dy) + fontSizePix * 0.3 //it should be 0.5 but 0.3 seems to work better
|
|
113
|
+
|
|
114
|
+
//draw the text
|
|
115
|
+
cg.ctx.fillText(text, tx, ty)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
//update legends
|
|
119
|
+
this.updateLegends({ style: this, r: r, zf: zf, sColor: statColor })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Build a function [0,1]->string for characters legend
|
|
124
|
+
*
|
|
125
|
+
* @param {Array.<string>} chars
|
|
126
|
+
* @returns {function(number):string}
|
|
127
|
+
*/
|
|
128
|
+
static getCharLegendFun(chars) {
|
|
129
|
+
const nb = chars.length
|
|
130
|
+
return (t) => (t == 0 ? '' : t == 1 ? chars[nb - 1] : chars[Math.floor(t * nb)])
|
|
131
|
+
}
|
|
132
|
+
}
|