source: node_modules/d3-axis/src/axis.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 5.3 KB
Line 
1import identity from "./identity.js";
2
3var top = 1,
4 right = 2,
5 bottom = 3,
6 left = 4,
7 epsilon = 1e-6;
8
9function translateX(x) {
10 return "translate(" + x + ",0)";
11}
12
13function translateY(y) {
14 return "translate(0," + y + ")";
15}
16
17function number(scale) {
18 return d => +scale(d);
19}
20
21function center(scale, offset) {
22 offset = Math.max(0, scale.bandwidth() - offset * 2) / 2;
23 if (scale.round()) offset = Math.round(offset);
24 return d => +scale(d) + offset;
25}
26
27function entering() {
28 return !this.__axis;
29}
30
31function axis(orient, scale) {
32 var tickArguments = [],
33 tickValues = null,
34 tickFormat = null,
35 tickSizeInner = 6,
36 tickSizeOuter = 6,
37 tickPadding = 3,
38 offset = typeof window !== "undefined" && window.devicePixelRatio > 1 ? 0 : 0.5,
39 k = orient === top || orient === left ? -1 : 1,
40 x = orient === left || orient === right ? "x" : "y",
41 transform = orient === top || orient === bottom ? translateX : translateY;
42
43 function axis(context) {
44 var values = tickValues == null ? (scale.ticks ? scale.ticks.apply(scale, tickArguments) : scale.domain()) : tickValues,
45 format = tickFormat == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments) : identity) : tickFormat,
46 spacing = Math.max(tickSizeInner, 0) + tickPadding,
47 range = scale.range(),
48 range0 = +range[0] + offset,
49 range1 = +range[range.length - 1] + offset,
50 position = (scale.bandwidth ? center : number)(scale.copy(), offset),
51 selection = context.selection ? context.selection() : context,
52 path = selection.selectAll(".domain").data([null]),
53 tick = selection.selectAll(".tick").data(values, scale).order(),
54 tickExit = tick.exit(),
55 tickEnter = tick.enter().append("g").attr("class", "tick"),
56 line = tick.select("line"),
57 text = tick.select("text");
58
59 path = path.merge(path.enter().insert("path", ".tick")
60 .attr("class", "domain")
61 .attr("stroke", "currentColor"));
62
63 tick = tick.merge(tickEnter);
64
65 line = line.merge(tickEnter.append("line")
66 .attr("stroke", "currentColor")
67 .attr(x + "2", k * tickSizeInner));
68
69 text = text.merge(tickEnter.append("text")
70 .attr("fill", "currentColor")
71 .attr(x, k * spacing)
72 .attr("dy", orient === top ? "0em" : orient === bottom ? "0.71em" : "0.32em"));
73
74 if (context !== selection) {
75 path = path.transition(context);
76 tick = tick.transition(context);
77 line = line.transition(context);
78 text = text.transition(context);
79
80 tickExit = tickExit.transition(context)
81 .attr("opacity", epsilon)
82 .attr("transform", function(d) { return isFinite(d = position(d)) ? transform(d + offset) : this.getAttribute("transform"); });
83
84 tickEnter
85 .attr("opacity", epsilon)
86 .attr("transform", function(d) { var p = this.parentNode.__axis; return transform((p && isFinite(p = p(d)) ? p : position(d)) + offset); });
87 }
88
89 tickExit.remove();
90
91 path
92 .attr("d", orient === left || orient === right
93 ? (tickSizeOuter ? "M" + k * tickSizeOuter + "," + range0 + "H" + offset + "V" + range1 + "H" + k * tickSizeOuter : "M" + offset + "," + range0 + "V" + range1)
94 : (tickSizeOuter ? "M" + range0 + "," + k * tickSizeOuter + "V" + offset + "H" + range1 + "V" + k * tickSizeOuter : "M" + range0 + "," + offset + "H" + range1));
95
96 tick
97 .attr("opacity", 1)
98 .attr("transform", function(d) { return transform(position(d) + offset); });
99
100 line
101 .attr(x + "2", k * tickSizeInner);
102
103 text
104 .attr(x, k * spacing)
105 .text(format);
106
107 selection.filter(entering)
108 .attr("fill", "none")
109 .attr("font-size", 10)
110 .attr("font-family", "sans-serif")
111 .attr("text-anchor", orient === right ? "start" : orient === left ? "end" : "middle");
112
113 selection
114 .each(function() { this.__axis = position; });
115 }
116
117 axis.scale = function(_) {
118 return arguments.length ? (scale = _, axis) : scale;
119 };
120
121 axis.ticks = function() {
122 return tickArguments = Array.from(arguments), axis;
123 };
124
125 axis.tickArguments = function(_) {
126 return arguments.length ? (tickArguments = _ == null ? [] : Array.from(_), axis) : tickArguments.slice();
127 };
128
129 axis.tickValues = function(_) {
130 return arguments.length ? (tickValues = _ == null ? null : Array.from(_), axis) : tickValues && tickValues.slice();
131 };
132
133 axis.tickFormat = function(_) {
134 return arguments.length ? (tickFormat = _, axis) : tickFormat;
135 };
136
137 axis.tickSize = function(_) {
138 return arguments.length ? (tickSizeInner = tickSizeOuter = +_, axis) : tickSizeInner;
139 };
140
141 axis.tickSizeInner = function(_) {
142 return arguments.length ? (tickSizeInner = +_, axis) : tickSizeInner;
143 };
144
145 axis.tickSizeOuter = function(_) {
146 return arguments.length ? (tickSizeOuter = +_, axis) : tickSizeOuter;
147 };
148
149 axis.tickPadding = function(_) {
150 return arguments.length ? (tickPadding = +_, axis) : tickPadding;
151 };
152
153 axis.offset = function(_) {
154 return arguments.length ? (offset = +_, axis) : offset;
155 };
156
157 return axis;
158}
159
160export function axisTop(scale) {
161 return axis(top, scale);
162}
163
164export function axisRight(scale) {
165 return axis(right, scale);
166}
167
168export function axisBottom(scale) {
169 return axis(bottom, scale);
170}
171
172export function axisLeft(scale) {
173 return axis(left, scale);
174}
Note: See TracBrowser for help on using the repository browser.