source: node_modules/d3-scale/src/time.js@ e4c61dd

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

Prototype 1.1

  • Property mode set to 100644
File size: 2.3 KB
Line 
1import {timeYear, timeMonth, timeWeek, timeDay, timeHour, timeMinute, timeSecond, timeTicks, timeTickInterval} from "d3-time";
2import {timeFormat} from "d3-time-format";
3import continuous, {copy} from "./continuous.js";
4import {initRange} from "./init.js";
5import nice from "./nice.js";
6
7function date(t) {
8 return new Date(t);
9}
10
11function number(t) {
12 return t instanceof Date ? +t : +new Date(+t);
13}
14
15export function calendar(ticks, tickInterval, year, month, week, day, hour, minute, second, format) {
16 var scale = continuous(),
17 invert = scale.invert,
18 domain = scale.domain;
19
20 var formatMillisecond = format(".%L"),
21 formatSecond = format(":%S"),
22 formatMinute = format("%I:%M"),
23 formatHour = format("%I %p"),
24 formatDay = format("%a %d"),
25 formatWeek = format("%b %d"),
26 formatMonth = format("%B"),
27 formatYear = format("%Y");
28
29 function tickFormat(date) {
30 return (second(date) < date ? formatMillisecond
31 : minute(date) < date ? formatSecond
32 : hour(date) < date ? formatMinute
33 : day(date) < date ? formatHour
34 : month(date) < date ? (week(date) < date ? formatDay : formatWeek)
35 : year(date) < date ? formatMonth
36 : formatYear)(date);
37 }
38
39 scale.invert = function(y) {
40 return new Date(invert(y));
41 };
42
43 scale.domain = function(_) {
44 return arguments.length ? domain(Array.from(_, number)) : domain().map(date);
45 };
46
47 scale.ticks = function(interval) {
48 var d = domain();
49 return ticks(d[0], d[d.length - 1], interval == null ? 10 : interval);
50 };
51
52 scale.tickFormat = function(count, specifier) {
53 return specifier == null ? tickFormat : format(specifier);
54 };
55
56 scale.nice = function(interval) {
57 var d = domain();
58 if (!interval || typeof interval.range !== "function") interval = tickInterval(d[0], d[d.length - 1], interval == null ? 10 : interval);
59 return interval ? domain(nice(d, interval)) : scale;
60 };
61
62 scale.copy = function() {
63 return copy(scale, calendar(ticks, tickInterval, year, month, week, day, hour, minute, second, format));
64 };
65
66 return scale;
67}
68
69export default function time() {
70 return initRange.apply(calendar(timeTicks, timeTickInterval, timeYear, timeMonth, timeWeek, timeDay, timeHour, timeMinute, timeSecond, timeFormat).domain([new Date(2000, 0, 1), new Date(2000, 0, 2)]), arguments);
71}
Note: See TracBrowser for help on using the repository browser.