1
- import { bin as binner , cross , group } from "d3" ;
1
+ import { bin as binner , cross , group , sum } from "d3" ;
2
2
import { firstof } from "../defined.js" ;
3
- import { valueof , first , second , range , identity , lazyChannel , maybeLazyChannel , maybeTransform , maybeColor , maybeValue , mid , take } from "../mark.js" ;
3
+ import { valueof , first , second , range , identity , lazyChannel , maybeLazyChannel , maybeTransform , maybeColor , maybeValue , mid , take , labelof } from "../mark.js" ;
4
4
import { offset } from "../style.js" ;
5
5
6
6
// Group on y, z, fill, or stroke, if any, then bin on x.
@@ -43,12 +43,12 @@ export function bin({x, y, out = "fill", inset, insetTop, insetRight, insetBotto
43
43
return { x1, x2, y1, y2, ...transform , inset, insetTop, insetRight, insetBottom, insetLeft, [ out ] : l } ;
44
44
}
45
45
46
- function bin1 ( x , key , { [ key ] : k , z, fill, stroke, domain, thresholds, normalize, cumulative, ...options } = { } ) {
46
+ function bin1 ( x , key , { [ key ] : k , z, fill, stroke, weight , domain, thresholds, normalize, cumulative, ...options } = { } ) {
47
47
const m = normalize === true || normalize === "z" ? 100 : + normalize ;
48
48
const bin = binof ( identity , { value : x , domain, thresholds} ) ;
49
49
const [ X1 , setX1 ] = lazyChannel ( x ) ;
50
50
const [ X2 , setX2 ] = lazyChannel ( x ) ;
51
- const [ L , setL ] = lazyChannel ( `Frequency${ m === 100 ? " (%)" : "" } ` ) ;
51
+ const [ L , setL ] = lazyChannel ( `${ labelof ( weight , " Frequency" ) } ${ m === 100 ? " (%)" : "" } ` ) ;
52
52
const [ vfill ] = maybeColor ( fill ) ;
53
53
const [ vstroke ] = maybeColor ( stroke ) ;
54
54
const [ BK , setBK ] = maybeLazyChannel ( k ) ;
@@ -68,6 +68,7 @@ function bin1(x, key, {[key]: k, z, fill, stroke, domain, thresholds, normalize,
68
68
const Z = valueof ( data , z ) ;
69
69
const F = valueof ( data , vfill ) ;
70
70
const S = valueof ( data , vstroke ) ;
71
+ const W = valueof ( data , weight ) ;
71
72
const binFacets = [ ] ;
72
73
const binData = [ ] ;
73
74
const X1 = setX1 ( [ ] ) ;
@@ -78,19 +79,19 @@ function bin1(x, key, {[key]: k, z, fill, stroke, domain, thresholds, normalize,
78
79
const BZ = Z && setBZ ( [ ] ) ;
79
80
const BF = F && setBF ( [ ] ) ;
80
81
const BS = S && setBS ( [ ] ) ;
81
- let n = data . length ;
82
+ let n = W ? sum ( W ) : data . length ;
82
83
let i = 0 ;
83
84
if ( cumulative < 0 ) B . reverse ( ) ;
84
85
for ( const facet of facets ) {
85
86
const binFacet = [ ] ;
86
87
for ( const I of G ? group ( facet , i => G [ i ] ) . values ( ) : [ facet ] ) {
87
- if ( normalize === "z" ) n = I . length ;
88
+ if ( normalize === "z" ) n = W ? sum ( I , i => W [ i ] ) : I . length ;
88
89
const set = new Set ( I ) ;
89
90
let f ;
90
91
for ( const b of B ) {
91
92
const s = b . filter ( i => set . has ( i ) ) ;
92
93
f = cumulative && f !== undefined ? f . concat ( s ) : s ;
93
- const l = f . length ;
94
+ const l = W ? sum ( f , i => W [ i ] ) : f . length ;
94
95
if ( l > 0 ) {
95
96
binFacet . push ( i ++ ) ;
96
97
binData . push ( take ( data , f ) ) ;
@@ -119,7 +120,7 @@ function bin1(x, key, {[key]: k, z, fill, stroke, domain, thresholds, normalize,
119
120
// representing a field name, a function, an array), or the value and some
120
121
// additional per-dimension binning options as an objects of the form {value,
121
122
// domain?, thresholds? }.
122
- function bin2 ( x , y , { domain, thresholds, normalize, z, fill, stroke, ...options } = { } ) {
123
+ function bin2 ( x , y , { weight , domain, thresholds, normalize, z, fill, stroke, ...options } = { } ) {
123
124
const m = normalize === true || normalize === "z" ? 100 : + normalize ;
124
125
const binX = binof ( first , { domain, thresholds, ...maybeValue ( x ) } ) ;
125
126
const binY = binof ( second , { domain, thresholds, ...maybeValue ( y ) } ) ;
@@ -128,7 +129,7 @@ function bin2(x, y, {domain, thresholds, normalize, z, fill, stroke, ...options}
128
129
const [ X2 , setX2 ] = lazyChannel ( x ) ;
129
130
const [ Y1 , setY1 ] = lazyChannel ( y ) ;
130
131
const [ Y2 , setY2 ] = lazyChannel ( y ) ;
131
- const [ L , setL ] = lazyChannel ( `Frequency${ m === 100 ? " (%)" : "" } ` ) ;
132
+ const [ L , setL ] = lazyChannel ( `${ labelof ( weight , " Frequency" ) } ${ m === 100 ? " (%)" : "" } ` ) ;
132
133
const [ vfill ] = maybeColor ( fill ) ;
133
134
const [ vstroke ] = maybeColor ( stroke ) ;
134
135
const [ BZ , setBZ ] = maybeLazyChannel ( z ) ;
@@ -145,6 +146,7 @@ function bin2(x, y, {domain, thresholds, normalize, z, fill, stroke, ...options}
145
146
const Z = valueof ( data , z ) ;
146
147
const F = valueof ( data , vfill ) ;
147
148
const S = valueof ( data , vstroke ) ;
149
+ const W = valueof ( data , weight ) ;
148
150
const binFacets = [ ] ;
149
151
const binData = [ ] ;
150
152
const X1 = setX1 ( [ ] ) ;
@@ -156,16 +158,16 @@ function bin2(x, y, {domain, thresholds, normalize, z, fill, stroke, ...options}
156
158
const BZ = Z && setBZ ( [ ] ) ;
157
159
const BF = F && setBF ( [ ] ) ;
158
160
const BS = S && setBS ( [ ] ) ;
159
- let n = data . length ;
161
+ let n = W ? sum ( W ) : data . length ;
160
162
let i = 0 ;
161
163
for ( const facet of facets ) {
162
164
const binFacet = [ ] ;
163
165
for ( const I of G ? group ( facet , i => G [ i ] ) . values ( ) : [ facet ] ) {
164
- if ( normalize === "z" ) n = I . length ;
166
+ if ( normalize === "z" ) n = W ? sum ( I , i => W [ i ] ) : I . length ;
165
167
const set = new Set ( I ) ;
166
168
for ( const b of B ) {
167
169
const f = b . filter ( i => set . has ( i ) ) ;
168
- const l = f . length ;
170
+ const l = W ? sum ( f , i => W [ i ] ) : f . length ;
169
171
if ( l > 0 ) {
170
172
binFacet . push ( i ++ ) ;
171
173
binData . push ( take ( data , f ) ) ;
0 commit comments