source: node_modules/d3-scale/src/linear.js@ ba17441

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

Prototype 1.1

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[e4c61dd]1import {ticks, tickIncrement} from "d3-array";
2import continuous, {copy} from "./continuous.js";
3import {initRange} from "./init.js";
4import tickFormat from "./tickFormat.js";
5
6export function linearish(scale) {
7 var domain = scale.domain;
8
9 scale.ticks = function(count) {
10 var d = domain();
11 return ticks(d[0], d[d.length - 1], count == null ? 10 : count);
12 };
13
14 scale.tickFormat = function(count, specifier) {
15 var d = domain();
16 return tickFormat(d[0], d[d.length - 1], count == null ? 10 : count, specifier);
17 };
18
19 scale.nice = function(count) {
20 if (count == null) count = 10;
21
22 var d = domain();
23 var i0 = 0;
24 var i1 = d.length - 1;
25 var start = d[i0];
26 var stop = d[i1];
27 var prestep;
28 var step;
29 var maxIter = 10;
30
31 if (stop < start) {
32 step = start, start = stop, stop = step;
33 step = i0, i0 = i1, i1 = step;
34 }
35
36 while (maxIter-- > 0) {
37 step = tickIncrement(start, stop, count);
38 if (step === prestep) {
39 d[i0] = start
40 d[i1] = stop
41 return domain(d);
42 } else if (step > 0) {
43 start = Math.floor(start / step) * step;
44 stop = Math.ceil(stop / step) * step;
45 } else if (step < 0) {
46 start = Math.ceil(start * step) / step;
47 stop = Math.floor(stop * step) / step;
48 } else {
49 break;
50 }
51 prestep = step;
52 }
53
54 return scale;
55 };
56
57 return scale;
58}
59
60export default function linear() {
61 var scale = continuous();
62
63 scale.copy = function() {
64 return copy(scale, linear());
65 };
66
67 initRange.apply(scale, arguments);
68
69 return linearish(scale);
70}
Note: See TracBrowser for help on using the repository browser.