Newer
Older
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
/**
* Create a simple horizontal legend
*
* @param svg
* @param legendMarkers
* @param startingXPosition
* @param markerYPosition
* @param textYPosition
* @param markerGroupOffset
* @param markerTextOffset
*/
function drawSimpleLegend(svg,
legendMarkers,
startingXPosition=0,
markerYPosition=-4,
textYPosition=0,
markerGroupOffset=40,
markerTextOffset=10) {
let legend = svg
.append('g')
.attr('class', 'legend');
let svgBBox = svg.node().getBBox();
let previousMarkerGroup;
for (let legendMarker of legendMarkers) {
let xPosition;
let previousMarkerGroupBox;
let detachedMarker;
if (previousMarkerGroup == null) {
xPosition = startingXPosition;
}
else {
previousMarkerGroupBox = previousMarkerGroup.node().getBBox();
xPosition = previousMarkerGroupBox.x + previousMarkerGroupBox.width + markerGroupOffset;
}
let markerType = legendMarker.type;
let legendGroup = legend.append('g');
if (markerType.name === 'lineMarker') {
detachedMarker = markerType({
x: xPosition,
y: markerYPosition,
length: 20,
domId: legendMarker.domId,
domClass: legendMarker.domClass
});
}
if (markerType.name === 'circleMarker') {
detachedMarker = markerType({
r: legendMarker.r,
x: xPosition,
y: markerYPosition,
domId: legendMarker.domId,
domClass: legendMarker.domClass
});
}
if (detachedMarker) {
legendGroup.node().appendChild(detachedMarker.node());
let detachedMarkerBBox = detachedMarker.node().getBBox();
legendGroup.append('text')
.attr('x', detachedMarkerBBox.x + detachedMarkerBBox.width + markerTextOffset)
.attr('y', textYPosition)
.text(legendMarker.text);
}
previousMarkerGroup = legendGroup;
}
// center the legend group in the svg
let legendBBox = legend.node().getBBox();
let legendXPosition = (svgBBox.width - legendBBox.width) / 2;
legend.attr('transform', `translate(${legendXPosition}, ${svgBBox.height-15})`);
}
module.exports = {drawSimpleLegend};