-
Notifications
You must be signed in to change notification settings - Fork 525
/
axis.js
73 lines (62 loc) · 2.1 KB
/
axis.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
cubism_contextPrototype.axis = function() {
var context = this,
scale = context.scale,
axis_ = d3.svg.axis().scale(scale);
var formatDefault = context.step() < 6e4 ? cubism_axisFormatSeconds
: context.step() < 864e5 ? cubism_axisFormatMinutes
: cubism_axisFormatDays;
var format = formatDefault;
function axis(selection) {
var id = ++cubism_id,
tick;
var g = selection.append("svg")
.datum({id: id})
.attr("width", context.size())
.attr("height", Math.max(28, -axis.tickSize()))
.append("g")
.attr("transform", "translate(0," + (axis_.orient() === "top" ? 27 : 4) + ")")
.call(axis_);
context.on("change.axis-" + id, function() {
g.call(axis_);
if (!tick) tick = d3.select(g.node().appendChild(g.selectAll("text").node().cloneNode(true)))
.style("display", "none")
.text(null);
});
context.on("focus.axis-" + id, function(i) {
if (tick) {
if (i == null) {
tick.style("display", "none");
g.selectAll("text").style("fill-opacity", null);
} else {
tick.style("display", null).attr("x", i).text(format(scale.invert(i)));
var dx = tick.node().getComputedTextLength() + 6;
g.selectAll("text").style("fill-opacity", function(d) { return Math.abs(scale(d) - i) < dx ? 0 : 1; });
}
}
});
}
axis.remove = function(selection) {
selection.selectAll("svg")
.each(remove)
.remove();
function remove(d) {
context.on("change.axis-" + d.id, null);
context.on("focus.axis-" + d.id, null);
}
};
axis.focusFormat = function(_) {
if (!arguments.length) return format == formatDefault ? null : _;
format = _ == null ? formatDefault : _;
return axis;
};
return d3.rebind(axis, axis_,
"orient",
"ticks",
"tickSubdivide",
"tickSize",
"tickPadding",
"tickFormat");
};
var cubism_axisFormatSeconds = d3.time.format("%I:%M:%S %p"),
cubism_axisFormatMinutes = d3.time.format("%I:%M %p"),
cubism_axisFormatDays = d3.time.format("%B %d");