Skip to content

Commit

Permalink
Split FunctionPlotDatum types into multiple types
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Nov 2, 2024
1 parent 9a703e4 commit 8aaa230
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/datum-defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FunctionPlotDatum } from './types.js'

export default function datumDefaults(d: FunctionPlotDatum) {
export default function datumDefaults(d: FunctionPlotDatum): FunctionPlotDatum {
// default graphType uses boxes i.e. 2d intervals
if (!('graphType' in d)) {
d.graphType = 'interval'
Expand Down
4 changes: 2 additions & 2 deletions src/graph-types/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { asyncIntervalEvaluate, intervalEvaluate } from '../evaluate-datum.js'
import { infinity, color } from '../utils.mjs'

import { Chart } from '../index.js'
import { Interval, FunctionPlotDatum, FunctionPlotScale } from '../types.js'
import { Interval, FunctionPlotDatum, FunctionPlotScale, LinearFunction } from '../types.js'

function clampRange(minWidthHeight: number, vLo: number, vHi: number, gLo: number, gHi: number) {
// issue 69
Expand Down Expand Up @@ -84,7 +84,7 @@ export default function interval(chart: Chart) {
const index = d.index
const closed = d.closed
let evaluatedData
if (d.fnType === 'linear' && typeof d.fn === 'string' && d.sampler === 'asyncInterval') {
if (d.fnType === 'linear' && typeof (d as LinearFunction).fn === 'string' && d.sampler === 'asyncInterval') {
evaluatedData = await asyncIntervalEvaluate(chart, d)
} else {
evaluatedData = intervalEvaluate(chart, d)
Expand Down
4 changes: 2 additions & 2 deletions src/graph-types/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { hsl as d3Hsl } from 'd3-color'
import { color } from '../utils.mjs'

import { Chart } from '../index.js'
import { FunctionPlotDatum } from '../types.js'
import { TextFunction } from '../types.js'

export default function Text(chart: Chart) {
const xScale = chart.meta.xScale
const yScale = chart.meta.yScale

function text(selection: Selection<any, FunctionPlotDatum, any, any>) {
function text(selection: Selection<any, TextFunction, any, any>) {
selection.each(function (d) {
// Force some parameters to make it look like a vector.
d.sampler = 'builtIn'
Expand Down
12 changes: 6 additions & 6 deletions src/helpers/derivative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import datumDefaults from '../datum-defaults.js'
import { infinity } from '../utils.mjs'

import { Chart } from '../index.js'
import { FunctionPlotDatum } from '../types.js'
import { LinearFunction } from '../types.js'

export default function derivative(chart: Chart) {
const derivativeDatum = datumDefaults({
const derivativeDatum: LinearFunction = datumDefaults({
isHelper: true,
skipTip: true,
skipBoundsCheck: true,
nSamples: 2,
graphType: 'polyline'
})
}) as LinearFunction

function computeLine(d: FunctionPlotDatum) {
function computeLine(d: LinearFunction) {
if (!d.derivative) {
return []
}
Expand All @@ -32,7 +32,7 @@ export default function derivative(chart: Chart) {
return [derivativeDatum]
}

function checkAutoUpdate(d: FunctionPlotDatum) {
function checkAutoUpdate(d: LinearFunction) {
const self = this
if (!d.derivative) {
return
Expand All @@ -53,7 +53,7 @@ export default function derivative(chart: Chart) {
}
}

const derivative = function (selection: Selection<any, FunctionPlotDatum, any, any>) {
const derivative = function (selection: Selection<any, LinearFunction, any, any>) {
selection.each(function (d) {
const el = d3Select(this)
const data = computeLine.call(selection, d)
Expand Down
16 changes: 8 additions & 8 deletions src/helpers/secant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import { polyline } from '../graph-types/index.js'
import { infinity } from '../utils.mjs'

import { Chart } from '../index.js'
import { FunctionPlotDatumScope, FunctionPlotDatum, FunctionPlotDatumSecant } from '../types.js'
import { FunctionPlotDatumScope, LinearFunction, SecantDatum } from '../types.js'

export default function secant(chart: Chart) {
const secantDefaults = datumDefaults({
const secantDefaults: LinearFunction = datumDefaults({
isHelper: true,
skipTip: true,
skipBoundsCheck: true,
nSamples: 2,
graphType: 'polyline'
})
}) as LinearFunction

function computeSlope(scope: FunctionPlotDatumScope) {
scope.m = (scope.y1 - scope.y0) / (scope.x1 - scope.x0)
}

function updateLine(d: FunctionPlotDatum, secant: FunctionPlotDatumSecant) {
function updateLine(d: LinearFunction, secant: SecantDatum) {
if (!('x0' in secant)) {
throw Error('secant must have the property `x0` defined')
}
Expand All @@ -38,12 +38,12 @@ export default function secant(chart: Chart) {
computeSlope(secant.scope)
}

function setFn(d: FunctionPlotDatum, secant: FunctionPlotDatumSecant) {
function setFn(d: LinearFunction, secant: SecantDatum) {
updateLine(d, secant)
secant.fn = 'm * (x - x0) + y0'
}

function setMouseListener(d: FunctionPlotDatum, secantObject: FunctionPlotDatumSecant) {
function setMouseListener(d: LinearFunction, secantObject: SecantDatum) {
const self = this
if (secantObject.updateOnMouseMove && !secantObject.$$mouseListener) {
secantObject.$$mouseListener = function ({ x }: any) {
Expand All @@ -55,7 +55,7 @@ export default function secant(chart: Chart) {
}
}

function computeLines(d: FunctionPlotDatum) {
function computeLines(d: LinearFunction) {
const self = this
const data = []
d.secants = d.secants || []
Expand All @@ -72,7 +72,7 @@ export default function secant(chart: Chart) {
return data
}

function secant(selection: Selection<any, FunctionPlotDatum, any, any>) {
function secant(selection: Selection<any, LinearFunction, any, any>) {
selection.each(function (d) {
const el = d3Select(this)
const data = computeLines.call(selection, d)
Expand Down
9 changes: 5 additions & 4 deletions src/samplers/builtIn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { linspace, sgn, infinity, clamp, space, isValidNumber } from '../utils.mjs'
import { builtIn as evaluate } from './eval.mjs'

import { FunctionPlotDatum, FunctionPlotScale } from '../types.js'
import { FunctionPlotDatum, FunctionPlotScale, PointFunction, VectorFunction } from '../types.js'
import { SamplerParams, SamplerFn } from './types.js'

type Asymptote = {
Expand Down Expand Up @@ -168,11 +168,12 @@ function polar(samplerParams: SamplerParams): SamplerResult {
}

function points(samplerParams: SamplerParams): SamplerResult {
return [samplerParams.d.points]
const d: PointFunction = samplerParams.d as PointFunction
return [d.points]
}

function vector(sampleParams: SamplerParams): SamplerResult {
const d = sampleParams.d
function vector(samplerParms: SamplerParams): SamplerResult {
const d: VectorFunction = samplerParms.d as VectorFunction
d.offset = d.offset || [0, 0]
return [[d.offset, [d.vector[0] + d.offset[0], d.vector[1] + d.offset[1]]]]
}
Expand Down
4 changes: 2 additions & 2 deletions src/samplers/interval_worker_pool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionPlotDatum } from '../types.js'
import { LinearFunction } from '../types.js'

// Webpack is doing a transformation of the statement `new Worker(...)`
// which means that we can't use new MyWorker() because it confuses it.
Expand All @@ -10,7 +10,7 @@ if (typeof window === 'undefined') {
}

interface IntervalTask {
d: FunctionPlotDatum
d: LinearFunction
lo: number
hi: number
n: number
Expand Down
Loading

0 comments on commit 8aaa230

Please sign in to comment.