Repository navigation
Expand file tree
/
Copy pathevaluate-datum.ts
More file actions
78 lines (71 loc) · 2.63 KB
/
Copy pathevaluate-datum.ts
File metadata and controls
78 lines (71 loc) · 2.63 KB
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
import globals from './globals.mjs'
import { syncSamplerInterval, asyncSamplerInterval } from './samplers/interval.js'
import builtIn from './samplers/builtIn.js'
import { Chart } from './chart.js'
import type { FunctionPlotDatum, FunctionPlotScale } from './types.js'
/**
* Computes the endpoints x_lo, x_hi of the range in d.range from which the sampler will take samples.
*/
function getRange(scale: FunctionPlotScale, d: FunctionPlotDatum): [number, number] {
const range = d.range || [-Infinity, Infinity]
const start = Math.max(scale.domain()[0], range[0])
const end = Math.min(scale.domain()[1], range[1])
return [start, end]
}
function getSamples(nSamples: number, chartWidth: number) {
return nSamples || Math.min(globals.MAX_ITERATIONS, globals.DEFAULT_ITERATIONS || chartWidth * 2)
}
/**
* Decides which sampler function to call based on the options
* of `data`
*
* @param {Object} chart Chart instance which is orchestrating this sampling operation
* @param {Object} d a.k.a a single item from `data`
* @returns [number, number]
*/
function builtInEvaluate(chart: Chart, d: FunctionPlotDatum) {
const data = builtIn({
d,
range: getRange(chart.meta.xScale, d),
xScale: chart.meta.xScale,
yScale: chart.meta.yScale,
xAxis: chart.options.x,
yAxis: chart.options.y,
nSamples: getSamples(d.nSamples, chart.meta.width)
})
// NOTE: it's impossible to listen for the first eval event
// as the event is already fired when a listener is attached
chart.emit('eval', data, d.index, d.isHelper)
return data
}
function intervalEvaluate(chart: Chart, d: FunctionPlotDatum) {
const data = syncSamplerInterval({
d,
range: getRange(chart.meta.xScale, d),
xScale: chart.meta.xScale,
yScale: chart.meta.yScale,
xAxis: chart.options.x,
yAxis: chart.options.y,
nSamples: getSamples(d.nSamples, chart.meta.width)
})
// NOTE: it's impossible to listen for the first eval event
// as the event is already fired when a listener is attached
chart.emit('eval', data, d.index, d.isHelper)
return data
}
async function asyncIntervalEvaluate(chart: Chart, d: FunctionPlotDatum) {
const data = asyncSamplerInterval({
d,
range: getRange(chart.meta.xScale, d),
xScale: chart.meta.xScale,
yScale: chart.meta.yScale,
xAxis: chart.options.x,
yAxis: chart.options.y,
nSamples: getSamples(d.nSamples, chart.meta.width)
})
// NOTE: it's impossible to listen for the first eval event
// as the event is already fired when a listener is attached
chart.emit('eval', data, d.index, d.isHelper)
return data
}
export { builtInEvaluate, intervalEvaluate, asyncIntervalEvaluate }