forked from ColinEberhardt/d3fc-webgl-hathi-explorer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bespokePointSeries.js
77 lines (64 loc) · 2.2 KB
/
bespokePointSeries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { scaleIdentity } from 'd3';
import {
rebind,
webglSeriesPoint,
webglScaleMapper
} from 'd3fc';
import webglConstantAttribute from '@d3fc/d3fc-webgl/src/buffer/constantAttribute'
import circlePointShader from '@d3fc/d3fc-webgl/src/shaders/point/circle/baseShader'
export default () => {
const sizeAttribute = webglConstantAttribute();
const definedAttribute = webglConstantAttribute();
const draw = webglSeriesPoint()
.sizeAttribute(sizeAttribute)
.definedAttribute(definedAttribute);
let xScale = scaleIdentity();
let yScale = scaleIdentity();
let size = 1;
let decorate = (programBuilder, data, index) => { };
const streamingPointSeries = (data) => {
sizeAttribute.value([Math.pow(size * (window.devicePixelRatio ?? 1), 2)]);
definedAttribute.value([true]);
// the following assumes there is no d3 scale required
const xWebglScale = webglScaleMapper(xScale).webglScale;
const yWebglScale = webglScaleMapper(yScale).webglScale;
draw.xScale(xWebglScale)
.yScale(yWebglScale)
.type(circlePointShader())
.decorate(programBuilder => {
decorate(programBuilder, data, 0);
});
draw(data.length);
};
streamingPointSeries.size = (...args) => {
if (!args.length) {
return size;
}
size = args[0];
return streamingPointSeries;
};
streamingPointSeries.xScale = (...args) => {
if (!args.length) {
return xScale;
}
xScale = args[0];
return streamingPointSeries;
};
streamingPointSeries.yScale = (...args) => {
if (!args.length) {
return yScale;
}
yScale = args[0];
return streamingPointSeries;
};
streamingPointSeries.decorate = (...args) => {
if (!args.length) {
return decorate;
}
decorate = args[0];
return streamingPointSeries;
};
// this is where the attributes are exposed to the consumer
rebind(streamingPointSeries, draw, 'context', 'pixelRatio', 'type', 'mainValueAttribute', 'crossValueAttribute');
return streamingPointSeries;
};