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
|
(function ($) {
function trans (left, top) {
return 'translate(' + left + ', ' + top + ')';
}
var defaults = {
height: 140,
color: '#1f77b4',
interpolate: 'basis'
};
/*
* data = [
* { val: '2015-01-30', count: 30 },
* ...
* ]
*/
$.fn.timeline = function (data) {
$(this).each(function () {
var options = _.defaults($(this).data(), defaults);
_.extend(options, { width: $(this).width() });
var container = d3.select(this),
svg = container.append('svg')
.attr('width', options.width + 2)
.attr('height', options.height + 2)
.classed('sonar-d3', true),
plot = svg.append('g')
.classed('plot', true),
xScale = d3.time.scale()
.domain(d3.extent(data, function (d) {
return new Date(d.val);
})),
yScale = d3.scale.linear()
.domain([
0, d3.max(data, function (d) {
return d.count;
})
]),
line = d3.svg.line()
.x(function (d) {
return xScale(new Date(d.val));
})
.y(function (d) {
return yScale(d.count);
})
.interpolate(options.interpolate),
minDate = xScale.domain()[0],
minDateTick = svg.append('text')
.classed('subtitle', true)
.text(moment(minDate).format('LL')),
maxDate = xScale.domain()[1],
maxDateTick = svg.append('text')
.classed('subtitle', true)
.text(moment(maxDate).format('LL'))
.style('text-anchor', 'end');
_.extend(options, {
marginLeft: 1,
marginRight: 1,
marginTop: 1,
marginBottom: 1 + maxDateTick.node().getBBox().height
});
_.extend(options, {
availableWidth: options.width - options.marginLeft - options.marginRight,
availableHeight: options.height - options.marginTop - options.marginBottom
});
plot.attr('transform', trans(options.marginLeft, options.marginTop));
xScale.range([0, options.availableWidth]);
yScale.range([0, options.availableHeight]);
minDateTick
.attr('x', options.marginLeft)
.attr('y', options.height);
maxDateTick
.attr('x', options.width - options.marginRight)
.attr('y', options.height);
plot.append('path')
.datum(data)
.attr('d', line)
.attr('class', 'line')
.style('stroke', options.color);
});
};
})(window.jQuery);
|