FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pie-lib/packages/plot/src/utils.js at develop · pie-framework/pie-lib · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
pie-framework
/
pie-lib
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
4
Code
Issues
4
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pie-lib
/
packages
/
plot
/
src
/
utils.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
167 lines (136 loc) · 4.18 KB
Breadcrumbs
pie-lib
/
packages
/
plot
/
src
/
utils.js
Copy path
File metadata and controls
167 lines (136 loc) · 4.18 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
import
invariant
from
'invariant'
;
import
{
head
,
isEqual
,
range
,
tail
}
from
'lodash-es'
;
import
Point
from
'@mapbox/point-geometry'
;
export
const
xy
=
(
x
,
y
)
=>
(
{
x
,
y
}
)
;
export
const
buildSizeArray
=
(
size
,
padding
)
=>
{
padding
=
Number
.
isFinite
(
padding
)
?
Math
.
max
(
padding
,
0
)
:
0
;
invariant
(
padding
<
size
,
'padding must be less than size'
)
;
const
half
=
Math
.
round
(
padding
*
0.5
)
;
return
[
half
,
size
-
half
]
;
}
;
export
const
tickCount
=
(
min
,
max
,
step
)
=>
{
invariant
(
min
<
max
,
'min must be less than max'
)
;
const
size
=
Math
.
abs
(
min
-
max
)
;
return
Math
.
round
(
size
/
step
)
;
}
;
export
function
getInterval
(
domain
,
ticks
)
{
const
{
min
,
max
}
=
domain
;
const
{
major
,
minor
}
=
ticks
;
if
(
min
>=
max
)
{
throw
new
Error
(
`min is > max:
${
min
}
>
${
max
}
`
)
;
}
const
distance
=
max
-
min
;
const
minorTicks
=
minor
>
0
?
minor
+
1
:
1
;
const
normalizedMajor
=
major
-
1
;
if
(
isNaN
(
normalizedMajor
)
)
{
throw
new
Error
(
'Tick Frequency must be 2 or higher'
)
;
}
if
(
normalizedMajor
<=
0
)
{
throw
new
Error
(
'Tick Frequency must be 2 or higher'
)
;
}
const
divider
=
normalizedMajor
*
minorTicks
;
const
raw
=
distance
/
divider
;
return
parseFloat
(
Number
(
raw
)
.
toFixed
(
4
)
)
;
}
const
mkRange
=
(
min
,
max
,
interval
)
=>
{
const
raw
=
range
(
min
,
max
,
interval
)
;
/* Fix the last step due to rounding errors */
raw
.
splice
(
raw
.
length
,
1
,
max
)
;
return
raw
;
}
;
export
function
snapTo
(
min
,
max
,
interval
,
value
)
{
if
(
value
>=
max
)
{
return
max
;
}
if
(
value
<=
min
)
{
return
min
;
}
let
rng
=
mkRange
(
min
,
max
,
interval
)
;
rng
=
rng
.
filter
(
(
v
)
=>
{
return
Math
.
abs
(
value
-
v
)
<=
interval
;
}
)
;
return
(
rng
.
length
&&
rng
.
reduce
(
(
prev
,
curr
)
=>
{
const
currentDistance
=
Math
.
abs
(
curr
-
value
)
;
const
previousDistance
=
Math
.
abs
(
prev
-
value
)
;
return
currentDistance
<=
previousDistance
?
curr
:
prev
;
}
)
)
;
}
export
function
buildTickModel
(
domain
,
ticks
,
interval
,
scaleFn
)
{
const
rng
=
mkRange
(
domain
.
min
,
domain
.
max
,
interval
)
;
return
rng
.
map
(
(
r
,
index
)
=>
{
const
isMajor
=
index
%
(
ticks
.
minor
+
1
)
===
0
;
return
{
value
:
r
,
major
:
isMajor
,
x
:
scaleFn
(
r
)
,
}
;
}
)
;
}
export
const
polygonToArea
=
(
points
)
=>
{
const
h
=
head
(
points
)
;
const
area
=
{
left
:
h
.
x
,
top
:
h
.
y
,
bottom
:
h
.
y
,
right
:
h
.
x
,
}
;
return
tail
(
points
)
.
reduce
(
(
a
,
p
)
=>
{
a
.
left
=
Math
.
min
(
a
.
left
,
p
.
x
)
;
a
.
top
=
Math
.
max
(
a
.
top
,
p
.
y
)
;
a
.
bottom
=
Math
.
min
(
a
.
bottom
,
p
.
y
)
;
a
.
right
=
Math
.
max
(
a
.
right
,
p
.
x
)
;
return
a
;
}
,
area
)
;
}
;
export
const
lineToArea
=
(
from
,
to
)
=>
{
const
left
=
Math
.
min
(
from
.
x
,
to
.
x
)
;
const
top
=
Math
.
max
(
from
.
y
,
to
.
y
)
;
const
bottom
=
Math
.
min
(
from
.
y
,
to
.
y
)
;
const
right
=
Math
.
max
(
from
.
x
,
to
.
x
)
;
return
{
left
,
top
,
bottom
,
right
}
;
}
;
export
const
bounds
=
(
area
,
domain
,
range
)
=>
{
return
{
left
:
domain
.
min
-
area
.
left
,
right
:
Math
.
abs
(
area
.
right
-
domain
.
max
)
,
top
:
Math
.
abs
(
area
.
top
-
range
.
max
)
,
bottom
:
range
.
min
-
area
.
bottom
,
}
;
}
;
export
const
point
=
(
o
)
=>
new
Point
(
o
.
x
,
o
.
y
)
;
export
const
getDelta
=
(
from
,
to
)
=>
{
return
point
(
to
)
.
sub
(
point
(
from
)
)
;
}
;
export
const
bandKey
=
(
d
,
index
)
=>
`
${
index
}
-
${
d
.
label
||
'-'
}
`
;
export
const
isDomainRangeEqual
=
(
graphProps
,
nextGraphProps
)
=>
{
return
isEqual
(
graphProps
.
domain
,
nextGraphProps
.
domain
)
&&
isEqual
(
graphProps
.
range
,
nextGraphProps
.
range
)
;
}
;
// findLongestWord is also used in graphing
export
const
findLongestWord
=
(
label
)
=>
{
let
longestWord
=
(
label
||
''
)
.
replace
(
/
<
[
^
>
]
+
>
/
g
,
''
)
.
split
(
' '
)
.
sort
(
(
a
,
b
)
=>
b
.
length
-
a
.
length
)
;
return
longestWord
[
0
]
.
length
;
}
;
// amountToIncreaseWidth is also used in graphing
export
const
amountToIncreaseWidth
=
(
longestWord
)
=>
{
if
(
!
longestWord
)
{
return
0
;
}
return
longestWord
*
20
;
}
;
export
const
extractTextFromHTML
=
(
htmlString
)
=>
{
const
parser
=
new
DOMParser
(
)
;
const
doc
=
parser
?.
parseFromString
(
htmlString
,
'text/html'
)
;
return
doc
?.
body
?.
textContent
||
''
;
}
;
export
const
isEmptyObject
=
(
obj
)
=>
{
return
obj
&&
Object
.
keys
(
obj
)
.
length
===
0
&&
obj
.
constructor
===
Object
;
}
;
export
const
isEmptyString
=
(
str
)
=>
{
return
typeof
str
===
'string'
&&
str
.
trim
(
)
===
''
;
}
;
Back
|
FazBrowse Home
|
New Git URL