Skip to content

Commit ed0b75f

Browse files
committed
Export function to register new graph types
1 parent 9fe2e4f commit ed0b75f

File tree

10 files changed

+950
-881
lines changed

10 files changed

+950
-881
lines changed

src/chart.ts

Lines changed: 768 additions & 0 deletions
Large diffs are not rendered by default.

src/datum-defaults.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FunctionPlotDatum } from './types'
22

3-
4-
export default function datumDefaults (d: FunctionPlotDatum) {
3+
export default function datumDefaults(d: FunctionPlotDatum) {
54
// default graphType uses boxes i.e. 2d intervals
65
if (!('graphType' in d)) {
76
d.graphType = 'interval'
@@ -10,9 +9,7 @@ export default function datumDefaults (d: FunctionPlotDatum) {
109
// if the graphType is not `interval` then the sampler is `builtIn`
1110
// because the interval sampler returns a box instead of a point
1211
if (!('sampler' in d)) {
13-
d.sampler = d.graphType !== 'interval'
14-
? 'builtIn'
15-
: 'interval'
12+
d.sampler = d.graphType !== 'interval' ? 'builtIn' : 'interval'
1613
}
1714

1815
// TODO: handle default fnType

src/globals.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
import { hsl as d3Hsl } from 'd3-color'
1+
import { hsl as d3Hsl, HSLColor } from 'd3-color'
22

3-
// var d3 = window.d3
4-
const Globals = {
3+
import { GraphTypeBuilder } from './graph-types/types'
4+
5+
type TGlobals = {
6+
COLORS: Array<HSLColor>
7+
DEFAULT_WIDTH: number
8+
DEFAULT_HEIGHT: number
9+
DEFAULT_ITERATIONS: number
10+
MAX_ITERATIONS: number
11+
TIP_X_EPS: number
12+
13+
/**
14+
* graphTypes are the graph types registered in functionPlot,
15+
* to register a new graphType use `registerGraphType`
16+
*/
17+
graphTypes: { [key: string]: GraphTypeBuilder }
18+
}
19+
20+
const Globals: TGlobals = {
521
COLORS: [
622
'steelblue',
723
'red',
@@ -17,13 +33,20 @@ const Globals = {
1733
}),
1834
DEFAULT_WIDTH: 550,
1935
DEFAULT_HEIGHT: 350,
36+
DEFAULT_ITERATIONS: null,
2037
TIP_X_EPS: 1,
21-
DEFAULT_ITERATIONS: Infinity,
22-
MAX_ITERATIONS: 0
38+
MAX_ITERATIONS: 0,
39+
graphTypes: {}
2340
}
2441

25-
Globals.DEFAULT_ITERATIONS = null
2642
Globals.MAX_ITERATIONS = Globals.DEFAULT_WIDTH * 10
2743

28-
// module.exports.Globals = Globals
44+
function registerGraphType(graphType: string, graphTypeBulder: GraphTypeBuilder) {
45+
if (Object.hasOwn(Globals.graphTypes, graphType)) {
46+
throw new Error(`registerGraphType: graphType ${graphType} is already registered.`)
47+
}
48+
Globals.graphTypes[graphType] = graphTypeBulder
49+
}
50+
51+
export { registerGraphType }
2952
export default Globals

src/graph-types/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Selection } from 'd3-selection'
2+
import { FunctionPlotDatum } from '../types'
3+
import { Chart } from '../chart'
4+
5+
/**
6+
* GraphTypeBuilder is the returned Plotter instance, this function is called
7+
* every time there's an update in the canvas, it corresponds to the `my` function
8+
* described in detail in https://bost.ocks.org/mike/chart/
9+
*
10+
* @param {Chart} Is a reference to the Chart instance.
11+
*/
12+
type GraphTypePlotter = (selection: Selection<any, FunctionPlotDatum, any, any>) => any
13+
14+
/**
15+
* GraphTypeBuilder is a graph type builder, functionPlot uses the standard d3 reusable char pattern,
16+
* it corresponds to the `chart` function descripbed in detail in https://bost.ocks.org/mike/chart/
17+
*
18+
* @param {Chart} Is a reference to the Chart instance.
19+
*/
20+
type GraphTypeBuilder = (chart: Chart) => GraphTypePlotter
21+
22+
export { GraphTypePlotter, GraphTypeBuilder }

0 commit comments

Comments
 (0)