-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathevaluate.ts
57 lines (49 loc) · 1.73 KB
/
evaluate.ts
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
import globals from './globals'
import interval from './samplers/interval'
import builtIn from './samplers/builtIn'
import { Chart } from './index'
import { FunctionPlotDatum, FunctionPlotScale } from './types'
type SamplerTypeFn = typeof interval | typeof builtIn
/**
* Computes the endpoints x_lo, x_hi of the range in d.range from which the sampler will take samples.
*/
function computeEndpoints(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]
}
/**
* 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 evaluate(chart: Chart, d: FunctionPlotDatum) {
const range = computeEndpoints(chart.meta.xScale, d)
let samplerFn: SamplerTypeFn
if (d.sampler === 'builtIn') {
samplerFn = builtIn
} else if (d.sampler === 'interval') {
samplerFn = interval
} else {
throw new Error(`Invalid sampler function ${d.sampler}`)
}
const nSamples = d.nSamples || Math.min(globals.MAX_ITERATIONS, globals.DEFAULT_ITERATIONS || chart.meta.width * 2)
const data = samplerFn({
d,
range,
xScale: chart.meta.xScale,
yScale: chart.meta.yScale,
xAxis: chart.options.xAxis,
yAxis: chart.options.yAxis,
nSamples
})
// 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 default evaluate