Repository navigation
Expand file tree
/
Copy pathevaluate.ts
More file actions
50 lines (44 loc) · 1.4 KB
/
Copy pathevaluate.ts
File metadata and controls
50 lines (44 loc) · 1.4 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
import globals from './globals'
import { interval, builtIn } from './samplers'
import { Chart } from './index'
import { FunctionPlotDatum } from './types'
const evalTypeFn = {
interval,
builtIn
}
/**
* Computes the endpoints x_lo, x_hi of the range
* from which the sampler will take samples
*
* @param {Object} scale
* @param {Object} d An item from `data`
* @returns {Array}
*/
function computeEndpoints (scale: any, d: any): [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 {Array}
*/
function evaluate (chart: Chart, d: FunctionPlotDatum) {
const range = computeEndpoints(chart.meta.xScale, d)
const evalFn = evalTypeFn[d.sampler]
const nSamples = d.nSamples || Math.min(
globals.MAX_ITERATIONS,
globals.DEFAULT_ITERATIONS || (chart.meta.width * 2)
)
const data = evalFn(chart, d, range, 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