gridviz 3.0.31 → 3.0.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gridviz",
3
- "version": "3.0.31",
3
+ "version": "3.0.32",
4
4
  "description": "Visualization tool for gridded statistics",
5
5
  "keywords": [
6
6
  "statistics",
package/src/core/Map.js CHANGED
@@ -76,6 +76,7 @@ export class Map {
76
76
  this.legend = opts.legendContainer
77
77
  ? select(opts.legendContainer) // Wrap the provided HTML node in a D3 selection
78
78
  : null
79
+ this._legendProvidedByUser = !!opts.legendContainer
79
80
  if (!this.legend) this.initialiseLegend()
80
81
 
81
82
  //tooltip
@@ -536,7 +537,10 @@ export class Map {
536
537
  this.geoCanvas.canvas.remove()
537
538
 
538
539
  // remove legend
539
- this.legend?.remove()
540
+ // only remove legend if gridviz created it
541
+ if (!this._legendProvidedByUser) {
542
+ this.legend?.remove()
543
+ }
540
544
 
541
545
  // remove tooltip
542
546
  this.tooltip.tooltip?.remove()
@@ -124,12 +124,17 @@ export class SideStyle extends Style {
124
124
  * @param {boolean} center Set to true so that the side coordinate are those of its center point rather than its left/bottom point (the side x,y coordinates are those of the left point for horizontal sides, and of the bottom point for vertical sides)
125
125
  * @returns { Array.<Side> }
126
126
  */
127
- static buildSides(cells, resolution, withHorizontal = true, withVertical = true, center = true) {
127
+ static buildSides(cells, resolution, withHorizontal = true, withVertical = true, center = true, eps = 0.01) {
128
128
  /** @type { Array.<Side> } */
129
129
  const sides = []
130
130
 
131
+ // half resolution
131
132
  const r2 = center ? resolution / 2 : 0
132
133
 
134
+ // epsilon
135
+ eps = resolution * eps
136
+ const abs = Math.abs
137
+
133
138
  //make horizontal sides
134
139
  //sort cells by x and y
135
140
  cells.sort((c1, c2) => (c2.x == c1.x ? c1.y - c2.y : c1.x - c2.x))
@@ -137,7 +142,7 @@ export class SideStyle extends Style {
137
142
  for (let i = 1; i < cells.length; i++) {
138
143
  let c2 = cells[i]
139
144
 
140
- if (c1.y + resolution == c2.y && c1.x == c2.x)
145
+ if (abs(c1.y + resolution - c2.y) < eps && abs(c1.x - c2.x) < eps)
141
146
  //cells in same column and touch along horizontal side
142
147
  //make shared side
143
148
  sides.push({
@@ -176,7 +181,7 @@ export class SideStyle extends Style {
176
181
  for (let i = 1; i < cells.length; i++) {
177
182
  let c2 = cells[i]
178
183
 
179
- if (c1.x + resolution == c2.x && c1.y == c2.y)
184
+ if (abs(c1.x + resolution - c2.x) < eps && abs(c1.y - c2.y) < eps)
180
185
  //cells in same row and touch along vertical side
181
186
  //make shared side
182
187
  sides.push({
@@ -10,6 +10,8 @@ import { SideStyle } from './SideStyle.js'
10
10
  * @typedef {function(Array.<Side>,number, number):*} SideViewScale */
11
11
 
12
12
  /**
13
+ * @see https://manifold.net/doc/mfd9/example__tanaka_contours.htm
14
+ *
13
15
  * @module style
14
16
  * @author Julien Gaffuri
15
17
  */
@@ -25,7 +27,7 @@ export class SideTanakaStyle extends Style {
25
27
 
26
28
  /** A function returning the width of a cell side, in geo unit
27
29
  * @type {function(Side, number, number, number, object):number} */
28
- this.width = opts.width || ((side, sideValue, resolution, z, sidesScale) => Math.abs(sideValue) * Math.min(2 * z, resolution / 3))
30
+ this.width = opts.width || ((side, sideValue, resolution, z, sidesScale) => Math.abs(sideValue) * z) //Math.min(2 * z, resolution / 3))
29
31
 
30
32
  /** A function returning the length of a cell side, in geo unit
31
33
  * @type {function(Side, number, number, object):number} */
@@ -151,6 +151,28 @@ export const viewScaleColorQuantile = (opts) => {
151
151
  }
152
152
  }
153
153
 
154
+
155
+ /**
156
+ * Generic function for view scale quantile classifier
157
+ *
158
+ * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, classNumber?:number }} opts
159
+ * @returns {function(Array.<import("../core/Dataset").Cell>):ColorScale}
160
+ */
161
+ export const viewScaleQuantileClassifer = (opts) => {
162
+ const valueFunction = opts.valueFunction // the function that return the cell value from a cell
163
+ const classNumber = opts.classNumber || 12 // the class number
164
+
165
+ const scale = scaleQuantile().range(Array.from( { length: classNumber }, (_, i) => i ))
166
+ return (cells) => {
167
+ scale.domain(cells.map(valueFunction))
168
+ scale.breaks = scale.quantiles()
169
+ return scale
170
+ }
171
+ }
172
+
173
+
174
+
175
+
154
176
  /**
155
177
  * Combine view scale functions
156
178
  *
@@ -1,66 +0,0 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
-
5
- /**
6
- * @see https://manifold.net/doc/mfd9/example__tanaka_contours.htm
7
- *
8
- * @module style
9
- * @author Julien Gaffuri
10
- */
11
- export class TanakaStyle {
12
- /**
13
- * @param {function(import('../core/Dataset.js').Cell):number} value Function that returns the value of a cell
14
- * @param {Array.<number>} breaks The break values
15
- * @param {Array.<string>} colors The colors, one more than the break values
16
- * @param {object} opts
17
- * @returns {Array.<import("../core/Style.js").Style>}
18
- */
19
- static get(value, breaks, colors, opts = {}) {
20
- //shadow colors
21
- opts.colorDark = opts.colorDark || '#111'
22
- opts.colorBright = opts.colorBright || '#ddd'
23
-
24
- /** @type { function(number, number):number } */
25
- opts.width =
26
- opts.width ||
27
- ((sideValue, resolution, z) => {
28
- const minWG = 1 * z
29
- const maxWG = 4 * z
30
- const step = (maxWG - minWG) / 3
31
- return Math.min(minWG + (sideValue - 1) * step, maxWG)
32
- })
33
-
34
- //make classifier
35
- const classifier = clFun(breaks)
36
- //make colors table
37
- const colorsDict = {}
38
- for (let i = 0; i < colors.length; i++) colorsDict[i + ''] = colors[i]
39
-
40
- const cellStyle = new SquareColorCategoryWebGLStyle({
41
- code: (cell) => classifier(value(cell)),
42
- color: colorsDict,
43
- })
44
-
45
- const getSideValue = (side) => {
46
- const cl1 = side.c1 ? classifier(value(side.c1)) : -1
47
- const cl2 = side.c2 ? classifier(value(side.c2)) : -1
48
- return cl1 - cl2
49
- }
50
-
51
- /** The side style, for the shadow effect */
52
- const sideStyle = new SideStyle({
53
- //white or black, depending on orientation and value
54
- color: (side) => {
55
- const v = getSideValue(side)
56
- if (v === 0) return
57
- if (side.or === 'v') return v < 0 ? opts.colorBright : opts.colorDark
58
- return v < 0 ? opts.colorDark : opts.colorBright
59
- },
60
- //width depends on the value, that is the number of classes of difference
61
- width: (side, resolution, z) => opts.width(Math.abs(getSideValue(side)), resolution, z),
62
- })
63
-
64
- return [cellStyle, sideStyle]
65
- }
66
- }