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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
function mg_return_label(d) {
return d.label;
}
function mg_remove_existing_markers(svg) {
svg.selectAll('.mg-markers').remove();
svg.selectAll('.mg-baselines').remove();
}
function mg_in_range(args) {
return function(d) {
return (args.scales.X(d[args.x_accessor]) >= mg_get_plot_left(args)) && (args.scales.X(d[args.x_accessor]) <= mg_get_plot_right(args));
};
}
function mg_x_position(args) {
return function(d) {
return args.scales.X(d[args.x_accessor]);
};
}
function mg_x_position_fixed(args) {
var _mg_x_pos = mg_x_position(args);
return function(d) {
return _mg_x_pos(d).toFixed(2);
};
}
function mg_y_position_fixed(args) {
var _mg_y_pos = args.scales.Y;
return function(d) {
return _mg_y_pos(d.value).toFixed(2);
};
}
function mg_place_annotations(checker, class_name, args, svg, line_fcn, text_fcn) {
var g;
if (checker) {
g = svg.append('g').attr('class', class_name);
line_fcn(g, args);
text_fcn(g, args);
}
}
function mg_place_markers(args, svg) {
mg_place_annotations(args.markers, 'mg-markers', args, svg, mg_place_marker_lines, mg_place_marker_text);
}
function mg_place_baselines(args, svg) {
mg_place_annotations(args.baselines, 'mg-baselines', args, svg, mg_place_baseline_lines, mg_place_baseline_text);
}
function mg_place_marker_lines(gm, args) {
var x_pos_fixed = mg_x_position_fixed(args);
gm.selectAll('.mg-markers')
.data(args.markers.filter(mg_in_range(args)))
.enter()
.append('line')
.attr('x1', x_pos_fixed)
.attr('x2', x_pos_fixed)
.attr('y1', args.top)
.attr('y2', mg_get_plot_bottom(args))
.attr('class', function(d) {
return d.lineclass;
})
.attr('stroke-dasharray', '3,1');
}
function mg_place_marker_text(gm, args) {
gm.selectAll('.mg-markers')
.data(args.markers.filter(mg_in_range(args)))
.enter()
.append('text')
.attr('class', function(d) {
return d.textclass || ''; })
.classed('mg-marker-text', true)
.attr('x', mg_x_position(args))
.attr('y', args.x_axis_position === 'bottom' ? mg_get_top(args) * 0.95 : mg_get_bottom(args) + args.buffer)
.attr('text-anchor', 'middle')
.text(mg_return_label)
.each(function(d) {
if (d.click) {
d3.select(this).style('cursor', 'pointer')
.on('click', d.click);
}
if (d.mouseover) {
d3.select(this).style('cursor', 'pointer')
.on('mouseover', d.mouseover);
}
if (d.mouseout) {
d3.select(this).style('cursor', 'pointer')
.on('mouseout', d.mouseout);
}
});
mg_prevent_horizontal_overlap(gm.selectAll('.mg-marker-text').nodes(), args);
}
function mg_place_baseline_lines(gb, args) {
var y_pos = mg_y_position_fixed(args);
gb.selectAll('.mg-baselines')
.data(args.baselines)
.enter().append('line')
.attr('x1', mg_get_plot_left(args))
.attr('x2', mg_get_plot_right(args))
.attr('y1', y_pos)
.attr('y2', y_pos);
}
function mg_place_baseline_text(gb, args) {
var y_pos = mg_y_position_fixed(args);
gb.selectAll('.mg-baselines')
.data(args.baselines)
.enter().append('text')
.attr('x', mg_get_plot_right(args))
.attr('y', y_pos)
.attr('dy', -3)
.attr('text-anchor', 'end')
.text(mg_return_label);
}
function markers(args) {
'use strict';
var svg = mg_get_svg_child_of(args.target);
mg_remove_existing_markers(svg);
mg_place_markers(args, svg);
mg_place_baselines(args, svg);
return this;
}
MG.markers = markers;
|