Skip to content

Commit 6466a78

Browse files
committed
Move types around
1 parent 2cb29d5 commit 6466a78

File tree

5 files changed

+89
-22
lines changed

5 files changed

+89
-22
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
site/js/vendor
22
site/js/plugins.js
3+
site/*.html
34
.github

site/playground-rerender.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!-- This page is used for tests purposes, think twice before making changes in <body> -->
2+
<!doctype html>
3+
<html>
4+
<head lang="en">
5+
<meta charset="UTF-8" />
6+
<title>Function Plot</title>
7+
<link rel="stylesheet" type="text/css" rel="noopener" target="_blank" href="./css/reset.css" />
8+
<style>
9+
body,
10+
text {
11+
font-family: Arial, sans-serif;
12+
color: black;
13+
font-size: 14px;
14+
}
15+
#playground-wrapper {
16+
display: flex;
17+
align-items: center;
18+
justify-content: center;
19+
}
20+
</style>
21+
</head>
22+
<body>
23+
<script src="./function-plot.js"></script>
24+
25+
Does nothing:
26+
<button id=upd>update existing fn</button>
27+
<br>
28+
Doesn't remove old polyline plot:
29+
<button id=redefine>redefine data</button>
30+
31+
<div id=plot></div>
32+
<script>
33+
34+
let obj = {
35+
target: plot,
36+
grid: true,
37+
data: [
38+
{ fn: 'x' }
39+
]
40+
}
41+
42+
functionPlot(obj)
43+
44+
upd.addEventListener('click', evt => {
45+
obj.data[0] = { fn: 'x', nSamples: 10, graphType: 'scatter' }
46+
47+
// obj.data[0].nSamples = 10
48+
// obj.data[0].graphType = 'scatter'
49+
console.log(obj)
50+
functionPlot(obj)
51+
})
52+
53+
redefine.addEventListener('click', evt => {
54+
obj.data = [{
55+
fn: 'x', nSamples: 10, graphType: 'scatter'
56+
}]
57+
console.log(obj)
58+
functionPlot(obj)
59+
})
60+
61+
</script>
62+
</body>
63+
</html>

src/chart.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ export class Chart extends EventEmitter.EventEmitter {
472472
}
473473

474474
/**
475-
* Draws each of the datums stored in data.options only
475+
* buildContent draws each of the datums stored in data.options only.
476476
* To do a full redraw call `instance.plot()`
477477
*/
478478
private buildContent() {
@@ -546,7 +546,7 @@ export class Chart extends EventEmitter.EventEmitter {
546546
// - for each datum determine the sampler to use
547547
const graphs = content
548548
.merge(contentEnter)
549-
.selectAll(':scope > g.graph')
549+
.selectAll('g.graph')
550550
.data(
551551
(d: FunctionPlotOptions) => {
552552
return d.data
@@ -685,8 +685,11 @@ export class Chart extends EventEmitter.EventEmitter {
685685
this.options.y.domain = [this.meta.yScale.domain()[0], this.meta.yScale.domain()[1]]
686686
}
687687

688-
// renderContent is a perf optimization to only render the content
689-
// without rendering the canvas beneath it.
688+
/**
689+
* renderContent is a perf optimization to only render the content
690+
* without rendering the canvas beneath it, when the user interacts with the canvas
691+
* doing a pan or zoom, it'll call this function instead of #plot (which does a full plot).
692+
*/
690693
private renderContent() {
691694
const instance = this
692695
instance.emit('before:renderContent')
@@ -816,7 +819,7 @@ export class Chart extends EventEmitter.EventEmitter {
816819
}
817820

818821
/**
819-
* Removes a linked graph.
822+
* Remove link removes a linked graph.
820823
*/
821824
removeLink(instance: Chart) {
822825
const idx = this.linkedGraphs.indexOf(instance)
@@ -826,7 +829,7 @@ export class Chart extends EventEmitter.EventEmitter {
826829
}
827830

828831
/**
829-
* Destroys this instance of functionPlot,
832+
* destroy destroys the current functionPlot instance.
830833
* if you added this to other instances through `addLink` make
831834
* sure you remove the links from the other instances to this
832835
* instance using `removeLink`.

src/samplers/builtIn.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ import { linspace, sgn, infinity, clamp, space, isValidNumber } from '../utils.m
22
import { builtIn as evaluate } from './eval.mjs'
33

44
import { FunctionPlotDatum, FunctionPlotScale, PointFunction, VectorFunction } from '../types.js'
5-
import { SamplerParams, SamplerFn } from './types.js'
5+
import { SamplerParams, SamplerFn, BuiltInSamplerResult, BuiltInSamplerResultGroup } from './types.js'
66

77
type Asymptote = {
88
asymptote: boolean
99
d0: [number, number]
1010
d1: [number, number]
1111
}
1212

13-
type SamplerResultSingle = [number, number]
14-
type SamplerResultGroup = Array<SamplerResultSingle>
15-
type SamplerResult = Array<SamplerResultGroup>
16-
1713
function checkAsymptote(
1814
d0: [number, number],
1915
d1: [number, number],
@@ -50,18 +46,18 @@ function checkAsymptote(
5046
* Splits the evaluated data into arrays, each array is separated by any asymptote found
5147
* through the process of detecting slope/sign brusque changes
5248
*/
53-
function split(d: FunctionPlotDatum, data: SamplerResultGroup, yScale: FunctionPlotScale): SamplerResult {
49+
function split(d: FunctionPlotDatum, data: BuiltInSamplerResultGroup, yScale: FunctionPlotScale): BuiltInSamplerResult {
5450
if (data.length === 0) {
5551
// This case is possible when the function didn't render any valid points
5652
// e.g. when evaluating sqrt(x) with all negative values.
5753
return []
5854
}
5955

60-
const samplerResult: SamplerResult = []
56+
const samplerResult: BuiltInSamplerResult = []
6157
const yMin = yScale.domain()[0] - infinity()
6258
const yMax = yScale.domain()[1] + infinity()
6359

64-
let samplerGroup: SamplerResultGroup = [data[0]]
60+
let samplerGroup: BuiltInSamplerResultGroup = [data[0]]
6561

6662
let i = 1
6763
let deltaX = infinity()
@@ -117,7 +113,7 @@ function split(d: FunctionPlotDatum, data: SamplerResultGroup, yScale: FunctionP
117113
return samplerResult
118114
}
119115

120-
function linear(samplerParams: SamplerParams): SamplerResult {
116+
function linear(samplerParams: SamplerParams): BuiltInSamplerResult {
121117
const allX = space(samplerParams.xAxis, samplerParams.range, samplerParams.nSamples)
122118
const yDomain = samplerParams.yScale.domain()
123119
// const yDomainMargin = yDomain[1] - yDomain[0]
@@ -136,12 +132,12 @@ function linear(samplerParams: SamplerParams): SamplerResult {
136132
return splitData
137133
}
138134

139-
function parametric(samplerParams: SamplerParams): SamplerResult {
135+
function parametric(samplerParams: SamplerParams): BuiltInSamplerResult {
140136
// range is mapped to canvas coordinates from the input
141137
// for parametric plots the range will tell the start/end points of the `t` param
142138
const parametricRange = samplerParams.d.range || [0, 2 * Math.PI]
143139
const tCoords = space(samplerParams.xAxis, parametricRange, samplerParams.nSamples)
144-
const samples: SamplerResultGroup = []
140+
const samples: BuiltInSamplerResultGroup = []
145141
for (let i = 0; i < tCoords.length; i += 1) {
146142
const t = tCoords[i]
147143
const x = evaluate(samplerParams.d, 'x', { t })
@@ -151,12 +147,12 @@ function parametric(samplerParams: SamplerParams): SamplerResult {
151147
return [samples]
152148
}
153149

154-
function polar(samplerParams: SamplerParams): SamplerResult {
150+
function polar(samplerParams: SamplerParams): BuiltInSamplerResult {
155151
// range is mapped to canvas coordinates from the input
156152
// for polar plots the range will tell the start/end points of the `theta` param
157153
const polarRange = samplerParams.d.range || [-Math.PI, Math.PI]
158154
const thetaSamples = space(samplerParams.xAxis, polarRange, samplerParams.nSamples)
159-
const samples: SamplerResultGroup = []
155+
const samples: BuiltInSamplerResultGroup = []
160156
for (let i = 0; i < thetaSamples.length; i += 1) {
161157
const theta = thetaSamples[i]
162158
const r = evaluate(samplerParams.d, 'r', { theta })
@@ -167,18 +163,18 @@ function polar(samplerParams: SamplerParams): SamplerResult {
167163
return [samples]
168164
}
169165

170-
function points(samplerParams: SamplerParams): SamplerResult {
166+
function points(samplerParams: SamplerParams): BuiltInSamplerResult {
171167
const d: PointFunction = samplerParams.d as PointFunction
172168
return [d.points]
173169
}
174170

175-
function vector(samplerParms: SamplerParams): SamplerResult {
171+
function vector(samplerParms: SamplerParams): BuiltInSamplerResult {
176172
const d: VectorFunction = samplerParms.d as VectorFunction
177173
d.offset = d.offset || [0, 0]
178174
return [[d.offset, [d.vector[0] + d.offset[0], d.vector[1] + d.offset[1]]]]
179175
}
180176

181-
const sampler: SamplerFn = function sampler(samplerParams: SamplerParams): SamplerResult {
177+
const sampler: SamplerFn = function sampler(samplerParams: SamplerParams): BuiltInSamplerResult {
182178
switch (samplerParams.d.fnType) {
183179
case 'linear':
184180
return linear(samplerParams)

src/samplers/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@ export type IntervalSamplerResultSingle = [TInterval, TInterval] | null
1818
export type IntervalSamplerResultGroup = Array<IntervalSamplerResultSingle>
1919
export type IntervalSamplerResult = Array<IntervalSamplerResultGroup>
2020

21+
export type BuiltInSamplerResultSingle = [number, number]
22+
export type BuiltInSamplerResultGroup = Array<BuiltInSamplerResultSingle>
23+
export type BuiltInSamplerResult = Array<BuiltInSamplerResultGroup>
24+
2125
export type SamplerFn = (samplerParams: SamplerParams) => Array<any>
2226
export type AsyncSamplerFn = (samplerParams: SamplerParams) => Promise<Array<any>>

0 commit comments

Comments
 (0)