-
Notifications
You must be signed in to change notification settings - Fork 118
/
layer-labels.js
69 lines (57 loc) · 1.92 KB
/
layer-labels.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
import {default as _} from 'underscore';
import {LayerLabelsModel} from './layer-labels-model';
import {LayerLabelsRules} from './layer-labels-rules';
import {FormatterRegistry} from '../../formatter-registry';
export class LayerLabels {
constructor(model, isHorizontal, labelGuide, {width, height, container}) {
this.container = container;
var guide = _.defaults(
(labelGuide || {}),
{
fontSize: 10,
fontColor: '#000',
position: [],
tickFormat: null,
tickFormatNullAlias: ''
});
var formatter = FormatterRegistry.get(guide.tickFormat, guide.tickFormatNullAlias);
var seed = LayerLabelsModel.seed(
model,
{
fontSize: guide.fontSize,
fontColor: guide.fontColor,
flip: isHorizontal,
formatter
});
var args = {maxWidth: width, maxHeight: height};
this.textModel = guide
.position
.concat('keep-in-box')
.map(LayerLabelsRules.getRule)
.reduce((prev, rule) => LayerLabelsModel.compose(prev, rule(prev, args)), seed);
}
draw(fibers) {
var m = this.textModel;
var fullData = fibers.reduce((m, f) => m.concat(f), []).filter(m.label);
var update = function () {
this.style('fill', m.color)
.style('font-size', m.h)
.attr('text-anchor', 'middle')
.attr('x', m.x)
.attr('y', m.y)
.text(m.label);
};
var text = this
.container
.selectAll('.t-label')
.data(fullData);
text.exit()
.remove();
text.call(update);
text.enter()
.append('text')
.attr('class', 't-label')
.call(update);
return text;
}
}