1- import { bin as binner , cross , group } from "d3" ;
1+ import { bin as binner , cross , group , sum } from "d3" ;
22import { 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" ;
44import { offset } from "../style.js" ;
55
66// 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
4343 return { x1, x2, y1, y2, ...transform , inset, insetTop, insetRight, insetBottom, insetLeft, [ out ] : l } ;
4444}
4545
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 } = { } ) {
4747 const m = normalize === true || normalize === "z" ? 100 : + normalize ;
4848 const bin = binof ( identity , { value : x , domain, thresholds} ) ;
4949 const [ X1 , setX1 ] = lazyChannel ( x ) ;
5050 const [ X2 , setX2 ] = lazyChannel ( x ) ;
51- const [ L , setL ] = lazyChannel ( `Frequency${ m === 100 ? " (%)" : "" } ` ) ;
51+ const [ L , setL ] = lazyChannel ( `${ labelof ( weight , " Frequency" ) } ${ m === 100 ? " (%)" : "" } ` ) ;
5252 const [ vfill ] = maybeColor ( fill ) ;
5353 const [ vstroke ] = maybeColor ( stroke ) ;
5454 const [ BK , setBK ] = maybeLazyChannel ( k ) ;
@@ -68,6 +68,7 @@ function bin1(x, key, {[key]: k, z, fill, stroke, domain, thresholds, normalize,
6868 const Z = valueof ( data , z ) ;
6969 const F = valueof ( data , vfill ) ;
7070 const S = valueof ( data , vstroke ) ;
71+ const W = valueof ( data , weight ) ;
7172 const binFacets = [ ] ;
7273 const binData = [ ] ;
7374 const X1 = setX1 ( [ ] ) ;
@@ -78,19 +79,19 @@ function bin1(x, key, {[key]: k, z, fill, stroke, domain, thresholds, normalize,
7879 const BZ = Z && setBZ ( [ ] ) ;
7980 const BF = F && setBF ( [ ] ) ;
8081 const BS = S && setBS ( [ ] ) ;
81- let n = data . length ;
82+ let n = W ? sum ( W ) : data . length ;
8283 let i = 0 ;
8384 if ( cumulative < 0 ) B . reverse ( ) ;
8485 for ( const facet of facets ) {
8586 const binFacet = [ ] ;
8687 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 ;
8889 const set = new Set ( I ) ;
8990 let f ;
9091 for ( const b of B ) {
9192 const s = b . filter ( i => set . has ( i ) ) ;
9293 f = cumulative && f !== undefined ? f . concat ( s ) : s ;
93- const l = f . length ;
94+ const l = W ? sum ( f , i => W [ i ] ) : f . length ;
9495 if ( l > 0 ) {
9596 binFacet . push ( i ++ ) ;
9697 binData . push ( take ( data , f ) ) ;
@@ -119,7 +120,7 @@ function bin1(x, key, {[key]: k, z, fill, stroke, domain, thresholds, normalize,
119120// representing a field name, a function, an array), or the value and some
120121// additional per-dimension binning options as an objects of the form {value,
121122// 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 } = { } ) {
123124 const m = normalize === true || normalize === "z" ? 100 : + normalize ;
124125 const binX = binof ( first , { domain, thresholds, ...maybeValue ( x ) } ) ;
125126 const binY = binof ( second , { domain, thresholds, ...maybeValue ( y ) } ) ;
@@ -128,7 +129,7 @@ function bin2(x, y, {domain, thresholds, normalize, z, fill, stroke, ...options}
128129 const [ X2 , setX2 ] = lazyChannel ( x ) ;
129130 const [ Y1 , setY1 ] = lazyChannel ( y ) ;
130131 const [ Y2 , setY2 ] = lazyChannel ( y ) ;
131- const [ L , setL ] = lazyChannel ( `Frequency${ m === 100 ? " (%)" : "" } ` ) ;
132+ const [ L , setL ] = lazyChannel ( `${ labelof ( weight , " Frequency" ) } ${ m === 100 ? " (%)" : "" } ` ) ;
132133 const [ vfill ] = maybeColor ( fill ) ;
133134 const [ vstroke ] = maybeColor ( stroke ) ;
134135 const [ BZ , setBZ ] = maybeLazyChannel ( z ) ;
@@ -145,6 +146,7 @@ function bin2(x, y, {domain, thresholds, normalize, z, fill, stroke, ...options}
145146 const Z = valueof ( data , z ) ;
146147 const F = valueof ( data , vfill ) ;
147148 const S = valueof ( data , vstroke ) ;
149+ const W = valueof ( data , weight ) ;
148150 const binFacets = [ ] ;
149151 const binData = [ ] ;
150152 const X1 = setX1 ( [ ] ) ;
@@ -156,16 +158,16 @@ function bin2(x, y, {domain, thresholds, normalize, z, fill, stroke, ...options}
156158 const BZ = Z && setBZ ( [ ] ) ;
157159 const BF = F && setBF ( [ ] ) ;
158160 const BS = S && setBS ( [ ] ) ;
159- let n = data . length ;
161+ let n = W ? sum ( W ) : data . length ;
160162 let i = 0 ;
161163 for ( const facet of facets ) {
162164 const binFacet = [ ] ;
163165 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 ;
165167 const set = new Set ( I ) ;
166168 for ( const b of B ) {
167169 const f = b . filter ( i => set . has ( i ) ) ;
168- const l = f . length ;
170+ const l = W ? sum ( f , i => W [ i ] ) : f . length ;
169171 if ( l > 0 ) {
170172 binFacet . push ( i ++ ) ;
171173 binData . push ( take ( data , f ) ) ;
0 commit comments