source: node_modules/d3-contour/README.md@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 15.4 KB
Line 
1# d3-contour
2
3This library computes contour polygons by applying [marching squares](https://en.wikipedia.org/wiki/Marching_squares) to a rectangular array of numeric values. For example, here is Maungawhau’s topology (the classic `volcano` dataset and `terrain.colors` from R):
4
5[<img alt="Volcano Contours" src="./img/volcano.gif" width="420" height="295">](https://observablehq.com/@d3/volcano-contours)
6
7For each [threshold value](#contours_thresholds), the [contour generator](#_contours) constructs a GeoJSON MultiPolygon geometry object representing the area where the input values are greater than or equal to the threshold value. The geometry is in planar coordinates, where ⟨<i>i</i> + 0.5, <i>j</i> + 0.5⟩ corresponds to element <i>i</i> + <i>jn</i> in the input values array. Here is an example that loads a GeoTIFF of surface temperatures, and another that blurs a noisy monochrome PNG to produce smooth contours of cloud fraction:
8
9[<img alt="GeoTiff Contours" src="./img/temperature.png" width="420" height="219">](https://observablehq.com/@d3/geotiff-contours)
10[<img alt="Cloud Contours" src="./img/clouds.png" width="420" height="219">](https://observablehq.com/@d3/cloud-contours)
11
12Since the contour polygons are GeoJSON, you can transform and display them using standard tools; see [d3.geoPath](https://github.com/d3/d3-geo/blob/main/README.md#geoPath), [d3.geoProject](https://github.com/d3/d3-geo-projection/blob/main/README.md#geoProject) and [d3.geoStitch](https://github.com/d3/d3-geo-projection/blob/main/README.md#geoStitch), for example. Here the above contours of surface temperature are displayed in the Natural Earth projection:
13
14[<img alt="GeoTiff Contours II" src="./img/reprojection.png" width="420" height="219">](https://observablehq.com/@d3/geotiff-contours-ii)
15
16Contour plots can also visualize continuous functions by sampling. Here is the Goldstein–Price function (a test function for global optimization) and a trippy animation of *sin*(*x* + *y*)*sin*(*x* - *y*):
17
18[<img alt="Contours" src="./img/goldstein-price.png" width="420" height="219">](https://observablehq.com/@d3/contours)
19[<img alt="Animated Contours" src="./img/sin-cos.png" width="420" height="219">](https://observablehq.com/@d3/animated-contours)
20
21Contours can also show the [estimated density](#density-estimation) of point clouds, which is especially useful to avoid overplotting in large datasets. This library implements fast two-dimensional kernel density estimation; see [d3.contourDensity](#contourDensity). Here is a scatterplot showing the relationship between the idle duration and eruption duration for Old Faithful:
22
23[<img alt="Density Contours" src="./img/faithful.png" width="420" height="219">](https://observablehq.com/@d3/density-contours)
24
25And here is a density contour plot showing the relationship between the weight and price of 53,940 diamonds:
26
27[<img alt="Density Contours" src="./img/diamonds.png" width="420" height="420">](https://observablehq.com/@d3/density-contours)
28
29## Installing
30
31If you use npm, `npm install d3-contour`. You can also download the [latest release on GitHub](https://github.com/d3/d3-contour/releases/latest). For vanilla HTML in modern browsers, import d3-contour from jsDelivr:
32
33```html
34<script type="module">
35
36import {contours} from "https://cdn.jsdelivr.net/npm/d3-contour@4/+esm";
37
38const c = contours(values);
39
40</script>
41```
42
43For legacy environments, you can load d3-contour’s UMD bundle; a `d3` global is exported:
44
45```html
46<script src="https://cdn.jsdelivr.net/npm/d3-array@3"></script>
47<script src="https://cdn.jsdelivr.net/npm/d3-contour@4"></script>
48<script>
49
50// Populate a grid of n×m values where -2 ≤ x ≤ 2 and -2 ≤ y ≤ 1.
51const n = 256, m = 256, values = new Array(n * m);
52for (let j = 0.5, k = 0; j < m; ++j) {
53 for (let i = 0.5; i < n; ++i, ++k) {
54 values[k] = goldsteinPrice(i / n * 4 - 2, 1 - j / m * 3);
55 }
56}
57
58// Compute the contour polygons at log-spaced intervals; returns an array of MultiPolygon.
59const contours = d3.contours()
60 .size([n, m])
61 .thresholds(Array.from({ length: 19 }, (_, i) => Math.pow(2, i + 2)))
62 (values);
63
64// See https://en.wikipedia.org/wiki/Test_functions_for_optimization
65function goldsteinPrice(x, y) {
66 return (1 + Math.pow(x + y + 1, 2) * (19 - 14 * x + 3 * x * x - 14 * y + 6 * x * x + 3 * y * y))
67 * (30 + Math.pow(2 * x - 3 * y, 2) * (18 - 32 * x + 12 * x * x + 48 * y - 36 * x * y + 27 * y * y));
68}
69
70</script>
71```
72
73## API Reference
74
75<a name="contours" href="#contours">#</a> d3.<b>contours</b>() · [Source](./src/contours.js), [Examples](https://observablehq.com/collection/@d3/d3-contour)
76
77Constructs a new contour generator with the default settings.
78
79<a name="_contours" href="#_contours">#</a> <i>contours</i>(<i>values</i>) · [Source](./src/contours.js)
80
81Computes the contours for the given array of *values*, returning an array of [GeoJSON](http://geojson.org/geojson-spec.html) [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) [geometry objects](http://geojson.org/geojson-spec.html#geometry-objects). Each geometry object represents the area where the input <i>values</i> are greater than or equal to the corresponding [threshold value](#contours_thresholds); the threshold value for each geometry object is exposed as <i>geometry</i>.value.
82
83The input *values* must be an array of length <i>n</i>×<i>m</i> where [<i>n</i>, <i>m</i>] is the contour generator’s [size](#contours_size); furthermore, each <i>values</i>[<i>i</i> + <i>jn</i>] must represent the value at the position ⟨<i>i</i>, <i>j</i>⟩. For example, to construct a 256×256 grid for the [Goldstein–Price function](https://en.wikipedia.org/wiki/Test_functions_for_optimization) where -2 ≤ <i>x</i> ≤ 2 and -2 ≤ <i>y</i> ≤ 1:
84
85```js
86var n = 256, m = 256, values = new Array(n * m);
87for (var j = 0.5, k = 0; j < m; ++j) {
88 for (var i = 0.5; i < n; ++i, ++k) {
89 values[k] = goldsteinPrice(i / n * 4 - 2, 1 - j / m * 3);
90 }
91}
92
93function goldsteinPrice(x, y) {
94 return (1 + Math.pow(x + y + 1, 2) * (19 - 14 * x + 3 * x * x - 14 * y + 6 * x * x + 3 * y * y))
95 * (30 + Math.pow(2 * x - 3 * y, 2) * (18 - 32 * x + 12 * x * x + 48 * y - 36 * x * y + 27 * y * y));
96}
97```
98
99The returned geometry objects are typically passed to [d3.geoPath](https://github.com/d3/d3-geo/blob/main/README.md#geoPath) to display, using null or [d3.geoIdentity](https://github.com/d3/d3-geo/blob/main/README.md#geoIdentity) as the associated projection.
100
101<a name="contours_contour" href="#contours_contour">#</a> <i>contours</i>.<b>contour</b>(<i>values</i>, <i>threshold</i>) · [Source](./src/contours.js), [Examples](https://observablehq.com/@d3/animated-contours)
102
103Computes a single contour, returning a [GeoJSON](http://geojson.org/geojson-spec.html) [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) [geometry object](http://geojson.org/geojson-spec.html#geometry-objects) representing the area where the input <i>values</i> are greater than or equal to the given [*threshold* value](#contours_thresholds); the threshold value for each geometry object is exposed as <i>geometry</i>.value.
104
105The input *values* must be an array of length <i>n</i>×<i>m</i> where [<i>n</i>, <i>m</i>] is the contour generator’s [size](#contours_size); furthermore, each <i>values</i>[<i>i</i> + <i>jn</i>] must represent the value at the position ⟨<i>i</i>, <i>j</i>⟩. See [*contours*](#_contours) for an example.
106
107<a name="contours_size" href="#contours_size">#</a> <i>contours</i>.<b>size</b>([<i>size</i>]) · [Source](./src/contours.js), [Examples](https://observablehq.com/@d3/animated-contours)
108
109If *size* is specified, sets the expected size of the input *values* grid to the [contour generator](#_contour) and returns the contour generator. The *size* is specified as an array \[<i>n</i>, <i>m</i>\] where <i>n</i> is the number of columns in the grid and <i>m</i> is the number of rows; *n* and *m* must be positive integers. If *size* is not specified, returns the current size which defaults to [1, 1].
110
111<a name="contours_smooth" href="#contours_smooth">#</a> <i>contours</i>.<b>smooth</b>([<i>smooth</i>]) · [Source](./src/contours.js), [Examples](https://observablehq.com/@d3/contours-smooth)
112
113If *smooth* is specified, sets whether or not the generated contour polygons are smoothed using linear interpolation. If *smooth* is not specified, returns the current smoothing flag, which defaults to true.
114
115<a name="contours_thresholds" href="#contours_thresholds">#</a> <i>contours</i>.<b>thresholds</b>([<i>thresholds</i>]) · [Source](./src/contours.js), [Examples](https://observablehq.com/@d3/volcano-contours)
116
117If *thresholds* is specified, sets the threshold generator to the specified function or array and returns this contour generator. If *thresholds* is not specified, returns the current threshold generator, which by default implements [Sturges’ formula](https://github.com/d3/d3-array/blob/main/README.md#thresholdSturges).
118
119Thresholds are defined as an array of values [*x0*, *x1*, …]. The first [generated contour](#_contour) corresponds to the area where the input values are greater than or equal to *x0*; the second contour corresponds to the area where the input values are greater than or equal to *x1*, and so on. Thus, there is exactly one generated MultiPolygon geometry object for each specified threshold value; the threshold value is exposed as <i>geometry</i>.value.
120
121If a *count* is specified instead of an array of *thresholds*, then the input values’ [extent](https://github.com/d3/d3-array/blob/main/README.md#extent) will be uniformly divided into approximately *count* bins; see [d3.ticks](https://github.com/d3/d3-array/blob/main/README.md#ticks).
122
123### Density Estimation
124
125<a name="contourDensity" href="#contourDensity">#</a> d3.<b>contourDensity</b>() · [Source](./src/density.js), [Examples](https://observablehq.com/@d3/density-contours)
126
127Constructs a new density estimator with the default settings.
128
129<a name="_density" href="#_density">#</a> <i>density</i>(<i>data</i>) · [Source](./src/density.js)
130
131Estimates the density contours for the given array of *data*, returning an array of [GeoJSON](http://geojson.org/geojson-spec.html) [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) [geometry objects](http://geojson.org/geojson-spec.html#geometry-objects). Each geometry object represents the area where the estimated number of points per square pixel is greater than or equal to the corresponding [threshold value](#density_thresholds); the threshold value for each geometry object is exposed as <i>geometry</i>.value. The returned geometry objects are typically passed to [d3.geoPath](https://github.com/d3/d3-geo/blob/main/README.md#geoPath) to display, using null or [d3.geoIdentity](https://github.com/d3/d3-geo/blob/main/README.md#geoIdentity) as the associated projection. See also [d3.contours](#contours).
132
133The *x*- and *y*-coordinate for each data point are computed using [*density*.x](#density_x) and [*density*.y](#density_y). In addition, [*density*.weight](#density_weight) indicates the relative contribution of each data point (default 1). The generated contours are only accurate within the estimator’s [defined size](#density_size).
134
135<a name="density_x" href="#density_x">#</a> <i>density</i>.<b>x</b>([<i>x</i>]) · [Source](./src/density.js), [Examples](https://observablehq.com/@d3/density-contours)
136
137If *x* is specified, sets the *x*-coordinate accessor. If *x* is not specified, returns the current *x*-coordinate accessor, which defaults to:
138
139```js
140function x(d) {
141 return d[0];
142}
143```
144
145<a name="density_y" href="#density_y">#</a> <i>density</i>.<b>y</b>([<i>y</i>]) · [Source](./src/density.js), [Examples](https://observablehq.com/@d3/density-contours)
146
147If *y* is specified, sets the *y*-coordinate accessor. If *y* is not specified, returns the current *y*-coordinate accessor, which defaults to:
148
149```js
150function y(d) {
151 return d[1];
152}
153```
154
155<a name="density_weight" href="#density_weight">#</a> <i>density</i>.<b>weight</b>([<i>weight</i>]) · [Source](./src/density.js)<!-- , [Examples](TBD) -->
156
157If *weight* is specified, sets the accessor for point weights. If *weight* is not specified, returns the current point weight accessor, which defaults to:
158
159```js
160function weight() {
161 return 1;
162}
163```
164
165<a name="density_size" href="#density_size">#</a> <i>density</i>.<b>size</b>([<i>size</i>]) · [Source](./src/density.js), [Examples](https://observablehq.com/@d3/density-contours)
166
167If *size* is specified, sets the size of the density estimator to the specified bounds and returns the estimator. The *size* is specified as an array \[<i>width</i>, <i>height</i>\], where <i>width</i> is the maximum *x*-value and <i>height</i> is the maximum *y*-value. If *size* is not specified, returns the current size which defaults to [960, 500]. The [estimated density contours](#_density) are only accurate within the defined size.
168
169<a name="density_cellSize" href="#density_cellSize">#</a> <i>density</i>.<b>cellSize</b>([<i>cellSize</i>]) · [Source](./src/density.js)<!-- , [Examples](TBD) -->
170
171If *cellSize* is specified, sets the size of individual cells in the underlying bin grid to the specified positive integer and returns the estimator. If *cellSize* is not specified, returns the current cell size, which defaults to 4. The cell size is rounded down to the nearest power of two. Smaller cells produce more detailed contour polygons, but are more expensive to compute.
172
173<a name="density_thresholds" href="#density_thresholds">#</a> <i>density</i>.<b>thresholds</b>([<i>thresholds</i>]) · [Source](./src/density.js), [Examples](https://observablehq.com/@d3/density-contours)
174
175If *thresholds* is specified, sets the threshold generator to the specified function or array and returns this contour generator. If *thresholds* is not specified, returns the current threshold generator, which by default generates about twenty nicely-rounded density thresholds.
176
177Thresholds are defined as an array of values [*x0*, *x1*, …]. The first [generated density contour](#_density) corresponds to the area where the estimated density is greater than or equal to *x0*; the second contour corresponds to the area where the estimated density is greater than or equal to *x1*, and so on. Thus, there is exactly one generated MultiPolygon geometry object for each specified threshold value; the threshold value is exposed as <i>geometry</i>.value. The first value *x0* should typically be greater than zero.
178
179If a *count* is specified instead of an array of *thresholds*, then approximately *count* uniformly-spaced nicely-rounded thresholds will be generated; see [d3.ticks](https://github.com/d3/d3-array/blob/main/README.md#ticks).
180
181<a name="density_bandwidth" href="#density_bandwidth">#</a> <i>density</i>.<b>bandwidth</b>([<i>bandwidth</i>]) · [Source](./src/density.js), [Examples](https://observablehq.com/@d3/density-contours)
182
183If *bandwidth* is specified, sets the bandwidth (the standard deviation) of the Gaussian kernel and returns the estimate. If *bandwidth* is not specified, returns the current bandwidth, which defaults to 20.4939…. The specified *bandwidth* is currently rounded to the nearest supported value by this implementation, and must be nonnegative.
184
185<a name="density_contours" href="#density_contours">#</a> <i>density</i>.<b>contours</b>(<i>data</i>) · [Source](./src/density.js), [Examples](https://observablehq.com/@d3/density-contours-data)
186
187Return a *contour*(*value*) function that can be used to compute an arbitrary contour on the given data without needing to recompute the underlying grid. The returned *contour* function also exposes a *contour*.max value which represents the maximum density of the grid.
Note: See TracBrowser for help on using the repository browser.