@@ -2,17 +2,23 @@ import {geoCentroid as GeoCentroid, geoPath} from "d3";
2
2
import { identity , valueof } from "../options.js" ;
3
3
import { initializer } from "./basic.js" ;
4
4
5
- export function centroid ( { geometry = identity , ...options } = { } ) {
5
+ export function centroid ( { geometry, ...options } = { } ) {
6
6
// Suppress defaults for x and y since they will be computed by the initializer.
7
- return initializer ( { ...options , x : null , y : null } , ( data , facets , channels , scales , dimensions , { projection} ) => {
8
- const G = valueof ( data , geometry ) ;
9
- const n = G . length ;
10
- const X = new Float64Array ( n ) ;
11
- const Y = new Float64Array ( n ) ;
12
- const path = geoPath ( projection ) ;
13
- for ( let i = 0 ; i < n ; ++ i ) [ X [ i ] , Y [ i ] ] = path . centroid ( G [ i ] ) ;
14
- return { data, facets, channels : { x : { value : X , source : null } , y : { value : Y , source : null } } } ;
15
- } ) ;
7
+ return initializer ( { ...options , x : null , y : null } , ( data , facets , channels , scales , dimensions , { projection} ) => ( {
8
+ data,
9
+ facets,
10
+ channels : { ...channels , ...centroidChannels ( data , geometry , projection ) }
11
+ } ) ) ;
12
+ }
13
+
14
+ export function centroidChannels ( data , geometry = identity , projection ) {
15
+ const G = valueof ( data , geometry ) ;
16
+ const n = G . length ;
17
+ const X = new Float64Array ( n ) ;
18
+ const Y = new Float64Array ( n ) ;
19
+ const path = geoPath ( projection ) ;
20
+ for ( let i = 0 ; i < n ; ++ i ) [ X [ i ] , Y [ i ] ] = path . centroid ( G [ i ] ) ;
21
+ return { x : { value : X , source : null } , y : { value : Y , source : null } } ;
16
22
}
17
23
18
24
export function geoCentroid ( { geometry = identity , ...options } = { } ) {
0 commit comments