Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
plot/src/facet.js at plot-link-defaults · Salebarn/plot · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
Salebarn
/
plot
Public
forked from
observablehq/plot
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
plot-link-defaults
Breadcrumbs
plot
/
src
/
facet.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
196 lines (185 loc) · 7.22 KB
plot-link-defaults
Breadcrumbs
plot
/
src
/
facet.js
Copy path
Top
File metadata and controls
Code
Blame
196 lines (185 loc) · 7.22 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import
{
cross
,
groups
,
InternMap
}
from
"d3-array"
;
import
{
create
}
from
"d3-selection"
;
import
{
Mark
,
first
,
second
}
from
"./mark.js"
;
export
function
facets
(
data
,
{
x
,
y
,
...
options
}
,
marks
)
{
return
x
===
undefined
&&
y
===
undefined
?
marks
// if no facets are specified, ignore!
:
[
new
Facet
(
data
,
{
x
,
y
,
...
options
}
,
marks
)
]
;
}
class
Facet
extends
Mark
{
constructor
(
data
,
{
x
,
y
,
transform
}
=
{
}
,
marks
=
[
]
)
{
super
(
data
,
[
{
name
:
"fx"
,
value
:
x
,
scale
:
"fx"
,
type
:
"band"
,
optional
:
true
}
,
{
name
:
"fy"
,
value
:
y
,
scale
:
"fy"
,
type
:
"band"
,
optional
:
true
}
]
,
transform
)
;
this
.
marks
=
marks
;
// The following fields are set by initialize:
this
.
marksChannels
=
undefined
;
// array of mark channels
this
.
marksIndex
=
undefined
;
// array of mark indexes (for non-faceted marks)
this
.
marksIndexByFacet
=
undefined
;
// map from facet key to array of mark indexes
}
initialize
(
)
{
const
{
index
,
channels
}
=
super
.
initialize
(
)
;
const
facets
=
index
===
undefined
?
[
]
:
facetGroups
(
index
,
channels
)
;
const
facetsKeys
=
Array
.
from
(
facets
,
first
)
;
const
facetsIndex
=
Array
.
from
(
facets
,
second
)
;
const
subchannels
=
[
]
;
const
marksChannels
=
this
.
marksChannels
=
[
]
;
const
marksIndex
=
this
.
marksIndex
=
new
Array
(
this
.
marks
.
length
)
;
const
marksIndexByFacet
=
this
.
marksIndexByFacet
=
facetMap
(
channels
)
;
for
(
const
facetKey
of
facetsKeys
)
{
marksIndexByFacet
.
set
(
facetKey
,
new
Array
(
this
.
marks
.
length
)
)
;
}
for
(
let
i
=
0
;
i
<
this
.
marks
.
length
;
++
i
)
{
const
mark
=
this
.
marks
[
i
]
;
const
markFacets
=
mark
.
data
===
this
.
data
?
facetsIndex
:
undefined
;
const
{
index
,
channels
}
=
mark
.
initialize
(
markFacets
)
;
// If an index is returned by mark.initialize, its structure depends on
// whether or not faceting has been applied: it is a flat index ([0, 1, 2,
// …]) when not faceted, and a nested index ([[0, 1, …], [2, 3, …], …])
// when faceted. Faceting is only applied if the mark data is the same as
// the facet’s data.
if
(
index
!==
undefined
)
{
if
(
markFacets
)
{
for
(
let
j
=
0
;
j
<
facetsKeys
.
length
;
++
j
)
{
marksIndexByFacet
.
get
(
facetsKeys
[
j
]
)
[
i
]
=
index
[
j
]
;
}
marksIndex
[
i
]
=
[
]
;
// implicit empty index for sparse facets
}
else
{
for
(
let
j
=
0
;
j
<
facetsKeys
.
length
;
++
j
)
{
marksIndexByFacet
.
get
(
facetsKeys
[
j
]
)
[
i
]
=
index
;
}
marksIndex
[
i
]
=
index
;
}
}
const
named
=
Object
.
create
(
null
)
;
for
(
const
[
name
,
channel
]
of
channels
)
{
if
(
name
!==
undefined
)
named
[
name
]
=
channel
.
value
;
subchannels
.
push
(
[
undefined
,
channel
]
)
;
}
marksChannels
.
push
(
named
)
;
}
return
{
index
,
channels
:
[
...
channels
,
...
subchannels
]
}
;
}
render
(
index
,
scales
,
channels
,
dimensions
,
axes
)
{
const
{
marks
,
marksChannels
,
marksIndex
,
marksIndexByFacet
}
=
this
;
const
{
fx
,
fy
}
=
scales
;
const
fyMargins
=
fy
&&
{
marginTop
:
0
,
marginBottom
:
0
,
height
:
fy
.
bandwidth
(
)
}
;
const
fxMargins
=
fx
&&
{
marginRight
:
0
,
marginLeft
:
0
,
width
:
fx
.
bandwidth
(
)
}
;
const
subdimensions
=
{
...
dimensions
,
...
fxMargins
,
...
fyMargins
}
;
return
create
(
"svg:g"
)
.
call
(
g
=>
{
if
(
fy
&&
axes
.
y
)
{
const
domain
=
fy
.
domain
(
)
;
const
axis1
=
axes
.
y
,
axis2
=
nolabel
(
axis1
)
;
const
j
=
axis1
.
labelAnchor
===
"bottom"
?
domain
.
length
-
1
:
axis1
.
labelAnchor
===
"center"
?
domain
.
length
>>
1
:
0
;
const
fyDimensions
=
{
...
dimensions
,
...
fyMargins
}
;
g
.
selectAll
(
)
.
data
(
domain
)
.
join
(
"g"
)
.
attr
(
"transform"
,
ky
=>
`translate(0,
${
fy
(
ky
)
}
)`
)
.
append
(
(
_
,
i
)
=>
(
i
===
j
?
axis1
:
axis2
)
.
render
(
null
,
scales
,
null
,
fyDimensions
)
)
;
}
if
(
fx
&&
axes
.
x
)
{
const
domain
=
fx
.
domain
(
)
;
const
axis1
=
axes
.
x
,
axis2
=
nolabel
(
axis1
)
;
const
j
=
axis1
.
labelAnchor
===
"right"
?
domain
.
length
-
1
:
axis1
.
labelAnchor
===
"center"
?
domain
.
length
>>
1
:
0
;
const
{
marginLeft
,
marginRight
}
=
dimensions
;
const
fxDimensions
=
{
...
dimensions
,
...
fxMargins
,
labelMarginLeft
:
marginLeft
,
labelMarginRight
:
marginRight
}
;
g
.
selectAll
(
)
.
data
(
domain
)
.
join
(
"g"
)
.
attr
(
"transform"
,
kx
=>
`translate(
${
fx
(
kx
)
}
,0)`
)
.
append
(
(
_
,
i
)
=>
(
i
===
j
?
axis1
:
axis2
)
.
render
(
null
,
scales
,
null
,
fxDimensions
)
)
;
}
}
)
.
call
(
g
=>
g
.
selectAll
(
)
.
data
(
facetKeys
(
scales
)
)
.
join
(
"g"
)
.
attr
(
"transform"
,
facetTranslate
(
fx
,
fy
)
)
.
each
(
function
(
key
)
{
const
marksFacetIndex
=
marksIndexByFacet
.
get
(
key
)
||
marksIndex
;
for
(
let
i
=
0
;
i
<
marks
.
length
;
++
i
)
{
const
node
=
marks
[
i
]
.
render
(
marksFacetIndex
[
i
]
,
scales
,
marksChannels
[
i
]
,
subdimensions
)
;
if
(
node
!=
null
)
this
.
appendChild
(
node
)
;
}
}
)
)
.
node
(
)
;
}
}
// Derives a copy of the specified axis with the label disabled.
function
nolabel
(
axis
)
{
return
axis
===
undefined
||
axis
.
label
===
undefined
?
axis
// use the existing axis if unlabeled
:
Object
.
assign
(
Object
.
create
(
axis
)
,
{
label
:
undefined
}
)
;
}
// Unlike facetGroups, which returns groups in order of input data, this returns
// keys in order of the associated scale’s domains.
function
facetKeys
(
{
fx
,
fy
}
)
{
return
fx
&&
fy
?
cross
(
fx
.
domain
(
)
,
fy
.
domain
(
)
)
:
fx
?
fx
.
domain
(
)
:
fy
.
domain
(
)
;
}
// Returns an array of [[key1, index1], [key2, index2], …] representing the data
// indexes associated with each facet. For two-dimensional faceting, each key
// is a two-element array; see also facetMap.
function
facetGroups
(
index
,
channels
)
{
return
(
channels
.
length
>
1
?
facetGroup2
:
facetGroup1
)
(
index
,
...
channels
)
;
}
function
facetGroup1
(
index
,
[
,
{
value
:
F
}
]
)
{
return
groups
(
index
,
i
=>
F
[
i
]
)
;
}
function
facetGroup2
(
index
,
[
,
{
value
:
FX
}
]
,
[
,
{
value
:
FY
}
]
)
{
return
groups
(
index
,
i
=>
FX
[
i
]
,
i
=>
FY
[
i
]
)
.
flatMap
(
(
[
x
,
xgroup
]
)
=>
xgroup
.
map
(
(
[
y
,
ygroup
]
)
=>
[
[
x
,
y
]
,
ygroup
]
)
)
;
}
// This must match the key structure returned by facetGroups.
function
facetTranslate
(
fx
,
fy
)
{
return
fx
&&
fy
?
(
[
kx
,
ky
]
)
=>
`translate(
${
fx
(
kx
)
}
,
${
fy
(
ky
)
}
)`
:
fx
?
kx
=>
`translate(
${
fx
(
kx
)
}
,0)`
:
ky
=>
`translate(0,
${
fy
(
ky
)
}
)`
;
}
function
facetMap
(
channels
)
{
return
new
(
channels
.
length
>
1
?
FacetMap2
:
FacetMap
)
;
}
class
FacetMap
{
constructor
(
)
{
this
.
_
=
new
InternMap
(
)
;
}
has
(
key
)
{
return
this
.
_
.
has
(
key
)
;
}
get
(
key
)
{
return
this
.
_
.
get
(
key
)
;
}
set
(
key
,
value
)
{
return
this
.
_
.
set
(
key
,
value
)
,
this
;
}
}
// A Map-like interface that supports paired keys.
class
FacetMap2
extends
FacetMap
{
has
(
[
key1
,
key2
]
)
{
const
map
=
super
.
get
(
key1
)
;
return
map
?
map
.
has
(
key2
)
:
false
;
}
get
(
[
key1
,
key2
]
)
{
const
map
=
super
.
get
(
key1
)
;
return
map
&&
map
.
get
(
key2
)
;
}
set
(
[
key1
,
key2
]
,
value
)
{
const
map
=
super
.
get
(
key1
)
;
if
(
map
)
map
.
set
(
key2
,
value
)
;
else
super
.
set
(
key1
,
new
InternMap
(
[
[
key2
,
value
]
]
)
)
;
return
this
;
}
}
You can’t perform that action at this time.