gridviz 3.0.0 → 3.0.1

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.0",
3
+ "version": "3.0.1",
4
4
  "description": "Visualization tool for gridded statistics",
5
5
  "keywords": [
6
6
  "statistics",
@@ -20,7 +20,6 @@ export class Dataset {
20
20
  * @abstract
21
21
  */
22
22
  constructor(map, url, resolution, opts = {}) {
23
- opts = opts || {}
24
23
 
25
24
  /**
26
25
  * The map.
package/src/core/Map.js CHANGED
@@ -135,6 +135,20 @@ export class Map {
135
135
  //.style("visibility", "hidden")
136
136
  }
137
137
 
138
+ /**
139
+ * Set/get layer stack.
140
+ *
141
+ * @param {undefined|import("./Layer.js").Layer|import("./Layer.js").Layer[]} layers
142
+ * @returns { this | import("./Layer.js").Layer[] }
143
+ */
144
+ layers_(layers) {
145
+ if (arguments.length === 0) return this.layers
146
+ if (arguments.length === 1)
147
+ if (Array.isArray(layers)) this.layers = layers
148
+ else this.layers = [layers]
149
+ else this.layers = arguments
150
+ return this
151
+ }
138
152
 
139
153
  /** @returns {this} */
140
154
  redraw() {
@@ -1,8 +1,6 @@
1
1
  //@ts-check
2
2
  'use strict'
3
3
 
4
- /** @typedef {{ dims: object, crs: string, tileSizeCell: number, originPoint: {x:number,y:number}, resolutionGeo: number, tilingBounds:import("../core/GeoCanvas.js").Envelope }} GridInfo */
5
-
6
4
  import { Dataset } from '../core/Dataset.js'
7
5
 
8
6
  /**
@@ -11,6 +9,7 @@ import { Dataset } from '../core/Dataset.js'
11
9
  * @author Joseph Davies, Julien Gaffuri
12
10
  */
13
11
  export class JSGrid extends Dataset {
12
+
14
13
  /**
15
14
  * @param {number} resolution The dataset resolution in geographical unit.
16
15
  * @param {Array.<Object>} cells The cells.
@@ -20,7 +19,7 @@ export class JSGrid extends Dataset {
20
19
 
21
20
  /**
22
21
  * @private
23
- * @type {Array.<import("../core/MultiResolutionDataset.js").Cell>} */
22
+ * @type {Array.<import('../core/Dataset.js').Cell>} */
24
23
  this.cells = cells || []
25
24
  }
26
25
 
package/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  // the application
5
5
  export { Map } from './core/Map.js'
6
- //export { GeoCanvas } from './GeoCanvas.js'
6
+ export { GeoCanvas } from './core/GeoCanvas.js'
7
7
  export { Style } from './core/Style.js'
8
8
  export { Layer } from './core/Layer.js'
9
9
  export { MultiResolutionDataset } from './core/MultiResolutionDataset.js'
@@ -20,6 +20,7 @@ export class GridLayer extends Layer {
20
20
 
21
21
  /** @type {import("../core/Dataset").Dataset|import("../core/MultiResolutionDataset").MultiResolutionDataset} */
22
22
  this.dataset = dataset
23
+
23
24
  /** @type {Array.<import("../core/Style").Style>} */
24
25
  this.styles = styles
25
26
 
@@ -121,6 +122,25 @@ export class GridLayer extends Layer {
121
122
  return this.dataset.getDataset(z, this.minPixelsPerCell);
122
123
  }
123
124
 
125
+
126
+
127
+ /**
128
+ * Set/get style stack.
129
+ *
130
+ * @param {undefined|import("../core/Style").Style|Array.<import("../core/Style").Style>} styles
131
+ * @returns { this | Array.<import("../core/Style").Style> }
132
+ */
133
+ styles_(styles) {
134
+ if (arguments.length === 0) return this.styles
135
+ if (arguments.length === 1)
136
+ if (Array.isArray(styles)) this.styles = styles
137
+ else this.styles = [styles]
138
+ else this.styles = arguments
139
+ return this
140
+ }
141
+
142
+
143
+
124
144
  /**
125
145
  * The default function returning cell information as HTML.
126
146
  * This is typically used for tooltip information.
@@ -15,9 +15,9 @@ import { scaleQuantile } from "d3-scale"
15
15
 
16
16
 
17
17
  /**
18
- * Generic function for view scale - continuous
18
+ * Generic function for view scale - continuous or discrete
19
19
  *
20
- * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, minValue?:number, minSizePix?:number, maxSizeFactor?:number, range?:[number, number], domain?:[number, number], stretching?:function(number):number }} opts
20
+ * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, minValue?:number, minSizePix?:number, maxSizeFactor?:number, range?:[number, number], domain?:[number, number], stretching?:function(number):number, classNumber?:number}} opts
21
21
  * @returns {function(Array.<import("../core/Dataset").Cell>):Scale}
22
22
  */
23
23
  export const viewScale = (opts) => {
@@ -28,21 +28,27 @@ export const viewScale = (opts) => {
28
28
  const stretching = opts.stretching
29
29
  const range_ = opts.range
30
30
  const domain_ = opts.domain
31
+ const classNumber = opts.classNumber
31
32
  return (cells, r, z) => {
32
33
  const domain = domain_ || [minValue, max(cells, valueFunction)]
33
34
  const range = range_ || [minSizePix * z, r * maxSizeFactor]
35
+ const domainSize = domain[1] - domain[0], domainMin = domain[0]
36
+ const rangeSize = range[1] - range[0], rangeMin = range[0]
34
37
  return t => {
35
38
  //scale to [0,1]
36
- t = (t - domain[0]) / (domain[1] - domain[0])
39
+ t = (t - domainMin) / domainSize
37
40
  //stretch
38
41
  if (stretching) t = stretching(t)
42
+ //classify
43
+ if (classNumber) t = t == 1 ? 1 : Math.floor(t * (classNumber)) / (classNumber - 1)
39
44
  //scale to range
40
- return range[0] + t * (range[1] - range[0])
45
+ return rangeMin + t * rangeSize
41
46
  }
42
47
  }
43
48
  }
44
49
 
45
50
 
51
+
46
52
  /**
47
53
  * Generic function for view scale - quantile
48
54
  *
@@ -71,24 +77,29 @@ export const viewScaleQuantile = (opts) => {
71
77
 
72
78
 
73
79
  /**
74
- * Generic function for color view scale - continuous
80
+ * Generic function for color view scale - continuous or discrete
75
81
  *
76
- * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, colorScale?:function(number):string, stretching?:function(number):number }} opts
82
+ * @param {{ valueFunction:function(import("../core/Dataset").Cell):number, colorScale?:function(number):string, colors?:Array.<string>, stretching?:function(number):number }} opts
77
83
  * @returns {function(Array.<import("../core/Dataset").Cell>):ColorScale}
78
84
  */
79
85
  export const viewScaleColor = (opts) => {
80
86
  const valueFunction = opts.valueFunction
81
- const colorScale = opts.colorScale || (() => "purple")
82
87
  const stretching = opts.stretching
88
+ let colorScale = opts.colorScale || (() => "purple")
89
+
90
+ //case of discrete colors
91
+ if (opts.colors)
92
+ colorScale = t => opts.colors[t == 1 ? opts.colors.length - 1 : Math.floor(t * opts.colors.length)]
93
+
83
94
  return (cells) => {
84
95
  if (cells.length == 0 || !cells) return
85
96
  /** @type {[undefined, undefined] | [number, number]} */
86
97
  const domain = extent(cells, valueFunction)
87
98
  if (domain[0] == undefined) return
88
- const amplitude = domain[1] - domain[0]
99
+ const domainSize = domain[1] - domain[0]
89
100
  const scale = t => {
90
101
  //scale to [0,1]
91
- t = (t - domain[0]) / amplitude
102
+ t = (t - domain[0]) / domainSize
92
103
  //stretch
93
104
  if (stretching) t = stretching(t)
94
105
  return colorScale(t)
@@ -96,13 +107,17 @@ export const viewScaleColor = (opts) => {
96
107
  //function that return the domain value from the [0,1] range.
97
108
  scale.invert = t => {
98
109
  if (stretching) t = stretching.invert(t)
99
- return domain[0] + t * amplitude
110
+ return domain[0] + t * domainSize
100
111
  }
101
112
 
102
113
  return scale;
103
114
  }
104
115
  }
105
116
 
117
+
118
+
119
+
120
+
106
121
  /**
107
122
  * Generic function for color view scale - quantile
108
123
  *