|
| 1 | +import { select as d3Select } from 'd3-selection' |
| 2 | +import type { Selection } from 'd3-selection' |
| 3 | + |
| 4 | +import { builtIn as builtInEvaluator } from '../samplers/eval.mjs' |
| 5 | +import datumDefaults from '../datum-defaults.js' |
| 6 | +import { Polyline } from '../graph-types/index.js' |
| 7 | +import { infinity } from '../utils.mjs' |
| 8 | +import { Mark } from '../graph-types/mark.js' |
| 9 | + |
| 10 | +import type { FunctionPlotDatum, FunctionPlotDatumScope, LinearFunction, SecantDatum } from '../types.js' |
| 11 | + |
| 12 | +export class Secant extends Mark { |
| 13 | + private secantDefaults: LinearFunction |
| 14 | + |
| 15 | + constructor(options: any) { |
| 16 | + super(options) |
| 17 | + this.secantDefaults = datumDefaults({ |
| 18 | + isHelper: true, |
| 19 | + skipTip: true, |
| 20 | + skipBoundsCheck: true, |
| 21 | + nSamples: 2, |
| 22 | + graphType: 'polyline' |
| 23 | + }) as LinearFunction |
| 24 | + } |
| 25 | + |
| 26 | + private computeSlope(scope: FunctionPlotDatumScope) { |
| 27 | + scope.m = (scope.y1 - scope.y0) / (scope.x1 - scope.x0) |
| 28 | + } |
| 29 | + |
| 30 | + private updateLine(d: FunctionPlotDatum, secant: SecantDatum) { |
| 31 | + if (!('x0' in secant)) { |
| 32 | + throw Error('secant must have the property `x0` defined') |
| 33 | + } |
| 34 | + secant.scope = secant.scope || {} |
| 35 | + |
| 36 | + const x0 = secant.x0 |
| 37 | + const x1 = typeof secant.x1 === 'number' ? secant.x1 : infinity() |
| 38 | + Object.assign(secant.scope, { |
| 39 | + x0, |
| 40 | + x1, |
| 41 | + y0: builtInEvaluator(d, 'fn', { x: x0 }), |
| 42 | + y1: builtInEvaluator(d, 'fn', { x: x1 }) |
| 43 | + }) |
| 44 | + this.computeSlope(secant.scope) |
| 45 | + } |
| 46 | + |
| 47 | + private setFn(d: FunctionPlotDatum, secant: SecantDatum) { |
| 48 | + this.updateLine(d, secant) |
| 49 | + secant.fn = 'm * (x - x0) + y0' |
| 50 | + } |
| 51 | + |
| 52 | + private setMouseListener( |
| 53 | + d: FunctionPlotDatum, |
| 54 | + secantObject: SecantDatum, |
| 55 | + selection: Selection<any, FunctionPlotDatum, any, any> |
| 56 | + ) { |
| 57 | + if (secantObject.updateOnMouseMove && !secantObject.$$mouseListener) { |
| 58 | + secantObject.$$mouseListener = ({ x }: any) => { |
| 59 | + secantObject.x1 = x |
| 60 | + this.updateLine(d, secantObject) |
| 61 | + this.render(selection) |
| 62 | + } |
| 63 | + this.chart.on('tip:update', secantObject.$$mouseListener) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private computeLines(d: FunctionPlotDatum, selection: Selection<any, FunctionPlotDatum, any, any>) { |
| 68 | + const data = [] |
| 69 | + d.secants = d.secants || [] |
| 70 | + for (let i = 0; i < d.secants.length; i += 1) { |
| 71 | + const secant = (d.secants[i] = Object.assign({}, this.secantDefaults, d.secants[i])) |
| 72 | + // necessary to make the secant have the same color as d |
| 73 | + secant.index = d.index |
| 74 | + if (!secant.fn) { |
| 75 | + this.setFn(d, secant) |
| 76 | + this.setMouseListener(d, secant, selection) |
| 77 | + } |
| 78 | + data.push(secant) |
| 79 | + } |
| 80 | + return data |
| 81 | + } |
| 82 | + |
| 83 | + render(selection: Selection<any, FunctionPlotDatum, any, any>) { |
| 84 | + selection.each((d, i, nodes) => { |
| 85 | + const el = d3Select(nodes[i]) |
| 86 | + const data = this.computeLines(d, selection) |
| 87 | + const innerSelection = el.selectAll('g.secant').data(data) |
| 88 | + |
| 89 | + const innerSelectionEnter = innerSelection.enter().append('g').attr('class', 'secant') |
| 90 | + |
| 91 | + // enter + update |
| 92 | + innerSelection.merge(innerSelectionEnter).each((d: any, i, nodes) => { |
| 93 | + const polyline = new Polyline(d) |
| 94 | + polyline.chart = this.chart |
| 95 | + polyline.render(d3Select(nodes[i])) |
| 96 | + }) |
| 97 | + |
| 98 | + // change the opacity of the secants |
| 99 | + innerSelection.merge(innerSelectionEnter).selectAll('path').attr('opacity', 0.5) |
| 100 | + |
| 101 | + // exit |
| 102 | + innerSelection.exit().remove() |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +export function secant(options: any) { |
| 108 | + return new Secant(options) |
| 109 | +} |
0 commit comments