@@ -3,8 +3,8 @@ import clamp from 'clamp'
33import utils from '../utils'
44import { builtIn as evaluate } from '../helpers/eval'
55
6- import { Chart } from '../chart '
7- import { FunctionPlotDatum } from '. ./types'
6+ import { FunctionPlotDatum , FunctionPlotScale } from '../types '
7+ import { SamplerParams , SamplerFn } from './types'
88
99function checkAsymptote (
1010 d0 : number [ ] ,
@@ -45,17 +45,15 @@ function checkAsymptote(
4545/**
4646 * Splits the evaluated data into arrays, each array is separated by any asymptote found
4747 * through the process of detecting slope/sign brusque changes
48- * @param chart
49- * @param d
50- * @param data
48+ *
5149 * @returns {Array[] }
5250 */
53- function split ( chart : Chart , d : FunctionPlotDatum , data : number [ ] [ ] ) {
51+ function split ( d : FunctionPlotDatum , data : number [ ] [ ] , yScale : FunctionPlotScale ) : Array < any > {
5452 let i , oldSign
5553 let deltaX
5654 let st = [ ]
5755 const sets = [ ]
58- const domain = chart . meta . yScale . domain ( )
56+ const domain = yScale . domain ( )
5957 const yMin = domain [ 0 ]
6058 const yMax = domain [ 1 ]
6159
@@ -105,77 +103,78 @@ function split(chart: Chart, d: FunctionPlotDatum, data: number[][]) {
105103 return sets
106104}
107105
108- function linear ( chart : Chart , d : FunctionPlotDatum , range : [ number , number ] , n : number ) {
109- const allX = utils . space ( chart . options . xAxis . type , range , n )
110- const yDomain = chart . meta . yScale . domain ( )
106+ function linear ( samplerParams : SamplerParams ) : Array < any > {
107+ const allX = utils . space ( samplerParams . xAxis , samplerParams . range , samplerParams . nSamples )
108+ const yDomain = samplerParams . yScale . domain ( )
111109 const yDomainMargin = yDomain [ 1 ] - yDomain [ 0 ]
112110 const yMin = yDomain [ 0 ] - yDomainMargin * 1e5
113111 const yMax = yDomain [ 1 ] + yDomainMargin * 1e5
114112 let data = [ ]
115113 for ( let i = 0 ; i < allX . length ; i += 1 ) {
116114 const x = allX [ i ]
117- const y = evaluate ( d , 'fn' , { x } )
115+ const y = evaluate ( samplerParams . d , 'fn' , { x } )
118116 if ( utils . isValidNumber ( x ) && utils . isValidNumber ( y ) ) {
119117 data . push ( [ x , clamp ( y , yMin , yMax ) ] )
120118 }
121119 }
122- data = split ( chart , d , data )
120+ data = split ( samplerParams . d , data , samplerParams . yScale )
123121 return data
124122}
125123
126- function parametric ( chart : Chart , d : FunctionPlotDatum , range : [ number , number ] , nSamples : number ) {
124+ function parametric ( samplerParams : SamplerParams ) : Array < any > {
127125 // range is mapped to canvas coordinates from the input
128126 // for parametric plots the range will tell the start/end points of the `t` param
129- const parametricRange = d . range || [ 0 , 2 * Math . PI ]
130- const tCoords = utils . space ( chart . options . xAxis . type , parametricRange , nSamples )
127+ const parametricRange = samplerParams . d . range || [ 0 , 2 * Math . PI ]
128+ const tCoords = utils . space ( samplerParams . xAxis , parametricRange , samplerParams . nSamples )
131129 const samples = [ ]
132130 for ( let i = 0 ; i < tCoords . length ; i += 1 ) {
133131 const t = tCoords [ i ]
134- const x = evaluate ( d , 'x' , { t } )
135- const y = evaluate ( d , 'y' , { t } )
132+ const x = evaluate ( samplerParams . d , 'x' , { t } )
133+ const y = evaluate ( samplerParams . d , 'y' , { t } )
136134 samples . push ( [ x , y ] )
137135 }
138136 return [ samples ]
139137}
140138
141- function polar ( chart : Chart , d : FunctionPlotDatum , range : [ number , number ] , nSamples : number ) {
139+ function polar ( samplerParams : SamplerParams ) : Array < any > {
142140 // range is mapped to canvas coordinates from the input
143141 // for polar plots the range will tell the start/end points of the `theta` param
144- const polarRange = d . range || [ - Math . PI , Math . PI ]
145- const thetaSamples = utils . space ( chart . options . xAxis . type , polarRange , nSamples )
142+ const polarRange = samplerParams . d . range || [ - Math . PI , Math . PI ]
143+ const thetaSamples = utils . space ( samplerParams . xAxis , polarRange , samplerParams . nSamples )
146144 const samples = [ ]
147145 for ( let i = 0 ; i < thetaSamples . length ; i += 1 ) {
148146 const theta = thetaSamples [ i ]
149- const r = evaluate ( d , 'r' , { theta } )
147+ const r = evaluate ( samplerParams . d , 'r' , { theta } )
150148 const x = r * Math . cos ( theta )
151149 const y = r * Math . sin ( theta )
152150 samples . push ( [ x , y ] )
153151 }
154152 return [ samples ]
155153}
156154
157- function points ( chart : Chart , d : FunctionPlotDatum , range : [ number , number ] , nSamples : number ) {
158- return [ d . points ]
155+ function points ( samplerParams : SamplerParams ) : Array < any > {
156+ return [ samplerParams . d . points ]
159157}
160158
161- function vector ( chart : Chart , d : FunctionPlotDatum , range : [ number , number ] , nSamples : number ) {
159+ function vector ( sampleParams : SamplerParams ) : Array < any > {
160+ const d = sampleParams . d
162161 d . offset = d . offset || [ 0 , 0 ]
163162 return [ [ d . offset , [ d . vector [ 0 ] + d . offset [ 0 ] , d . vector [ 1 ] + d . offset [ 1 ] ] ] ]
164163}
165164
166- const sampler = function ( chart : Chart , d : FunctionPlotDatum , range : [ number , number ] , nSamples : number ) {
165+ const sampler : SamplerFn = function sampler ( samplerParams : SamplerParams ) : Array < any > {
167166 const fnTypes = {
168167 parametric,
169168 polar,
170169 points,
171170 vector,
172171 linear
173172 }
174- if ( ! ( d . fnType in fnTypes ) ) {
175- throw Error ( d . fnType + ' is not supported in the `builtIn` sampler' )
173+ if ( ! ( samplerParams . d . fnType in fnTypes ) ) {
174+ throw Error ( samplerParams . d . fnType + ' is not supported in the `builtIn` sampler' )
176175 }
177176 // @ts -ignore
178- return fnTypes [ d . fnType ] . apply ( null , arguments )
177+ return fnTypes [ samplerParams . d . fnType ] . apply ( null , arguments )
179178}
180179
181180export default sampler
0 commit comments