Skip to content

Commit d158235

Browse files
committed
Don’t bother localizing time scale format.
It wasn’t really localized anyway, since the set of format strings weren’t really locale-independent. A better way of localizing this format is to implement a custom time format using locale.timeFormat, as in the custom time format example: http://bl.ocks.org/mbostock/4149176
1 parent f152a9e commit d158235

9 files changed

Lines changed: 113 additions & 120 deletions

File tree

d3.js

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,58 +2723,10 @@ d3 = function() {
27232723
var n = d3_time_percentRe.exec(string.substring(i, i + 1));
27242724
return n ? i + n[0].length : -1;
27252725
}
2726-
function d3_true() {
2727-
return true;
2728-
}
2729-
function d3_locale_timeScaleFormat(localFormat) {
2730-
var utcFormat = localFormat.utc;
2731-
var localFormats = [ [ localFormat("%Y"), d3_true ], [ localFormat("%B"), function(d) {
2732-
return d.getMonth();
2733-
} ], [ localFormat("%b %d"), function(d) {
2734-
return d.getDate() != 1;
2735-
} ], [ localFormat("%a %d"), function(d) {
2736-
return d.getDay() && d.getDate() != 1;
2737-
} ], [ localFormat("%I %p"), function(d) {
2738-
return d.getHours();
2739-
} ], [ localFormat("%I:%M"), function(d) {
2740-
return d.getMinutes();
2741-
} ], [ localFormat(":%S"), function(d) {
2742-
return d.getSeconds();
2743-
} ], [ localFormat(".%L"), function(d) {
2744-
return d.getMilliseconds();
2745-
} ] ];
2746-
var utcFormats = [ [ utcFormat("%Y"), d3_true ], [ utcFormat("%B"), function(d) {
2747-
return d.getUTCMonth();
2748-
} ], [ utcFormat("%b %d"), function(d) {
2749-
return d.getUTCDate() != 1;
2750-
} ], [ utcFormat("%a %d"), function(d) {
2751-
return d.getUTCDay() && d.getUTCDate() != 1;
2752-
} ], [ utcFormat("%I %p"), function(d) {
2753-
return d.getUTCHours();
2754-
} ], [ utcFormat("%I:%M"), function(d) {
2755-
return d.getUTCMinutes();
2756-
} ], [ utcFormat(":%S"), function(d) {
2757-
return d.getUTCSeconds();
2758-
} ], [ utcFormat(".%L"), function(d) {
2759-
return d.getUTCMilliseconds();
2760-
} ] ];
2761-
var format = d3_locale_timeScaleMultiFormat(localFormats);
2762-
format.utc = d3_locale_timeScaleMultiFormat(utcFormats);
2763-
return format;
2764-
}
2765-
function d3_locale_timeScaleMultiFormat(formats) {
2766-
return function(date) {
2767-
var i = formats.length - 1, f = formats[i];
2768-
while (!f[1](date)) f = formats[--i];
2769-
return f[0](date);
2770-
};
2771-
}
27722726
d3.locale = function(locale) {
2773-
var timeFormat = d3_locale_timeFormat(locale);
27742727
return {
27752728
numberFormat: d3_locale_numberFormat(locale),
2776-
timeFormat: timeFormat,
2777-
timeScaleFormat: d3_locale_timeScaleFormat(timeFormat)
2729+
timeFormat: d3_locale_timeFormat(locale)
27782730
};
27792731
};
27802732
var d3_locale_enUS = d3.locale({
@@ -3167,6 +3119,9 @@ d3 = function() {
31673119
d3_geo_centroidPointXYZ(x0, y0, z0);
31683120
}
31693121
}
3122+
function d3_true() {
3123+
return true;
3124+
}
31703125
function d3_geo_clipPolygon(segments, compare, clipStartInside, interpolate, listener) {
31713126
var subject = [], clip = [];
31723127
segments.forEach(function(segment) {
@@ -9169,7 +9124,8 @@ d3 = function() {
91699124
};
91709125
var d3_svg_brushResizes = [ [ "n", "e", "s", "w", "nw", "ne", "se", "sw" ], [ "e", "w" ], [ "n", "s" ], [] ];
91719126
var d3_time_format = d3_time.format = d3_locale_enUS.timeFormat;
9172-
var d3_time_formatIso = d3_time_format.utc("%Y-%m-%dT%H:%M:%S.%LZ");
9127+
var d3_time_formatUtc = d3_time_format.utc;
9128+
var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ");
91739129
d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z") ? d3_time_formatIsoNative : d3_time_formatIso;
91749130
function d3_time_formatIsoNative(date) {
91759131
return date.toISOString();
@@ -9273,17 +9229,61 @@ d3 = function() {
92739229
}
92749230
var d3_time_scaleSteps = [ 1e3, 5e3, 15e3, 3e4, 6e4, 3e5, 9e5, 18e5, 36e5, 108e5, 216e5, 432e5, 864e5, 1728e5, 6048e5, 2592e6, 7776e6, 31536e6 ];
92759231
var d3_time_scaleLocalMethods = [ [ d3_time.second, 1 ], [ d3_time.second, 5 ], [ d3_time.second, 15 ], [ d3_time.second, 30 ], [ d3_time.minute, 1 ], [ d3_time.minute, 5 ], [ d3_time.minute, 15 ], [ d3_time.minute, 30 ], [ d3_time.hour, 1 ], [ d3_time.hour, 3 ], [ d3_time.hour, 6 ], [ d3_time.hour, 12 ], [ d3_time.day, 1 ], [ d3_time.day, 2 ], [ d3_time.week, 1 ], [ d3_time.month, 1 ], [ d3_time.month, 3 ], [ d3_time.year, 1 ] ];
9276-
d3_time_scaleLocalMethods.year = d3_time.year;
9277-
d3_time.scale = function() {
9278-
return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_locale_enUS.timeScaleFormat);
9279-
};
9232+
var d3_time_scaleLocalFormat = d3_time_scaleMultiFormat([ [ d3_time_format("%Y"), d3_true ], [ d3_time_format("%B"), function(d) {
9233+
return d.getMonth();
9234+
} ], [ d3_time_format("%b %d"), function(d) {
9235+
return d.getDate() != 1;
9236+
} ], [ d3_time_format("%a %d"), function(d) {
9237+
return d.getDay() && d.getDate() != 1;
9238+
} ], [ d3_time_format("%I %p"), function(d) {
9239+
return d.getHours();
9240+
} ], [ d3_time_format("%I:%M"), function(d) {
9241+
return d.getMinutes();
9242+
} ], [ d3_time_format(":%S"), function(d) {
9243+
return d.getSeconds();
9244+
} ], [ d3_time_format(".%L"), function(d) {
9245+
return d.getMilliseconds();
9246+
} ] ]);
92809247
var d3_time_scaleMilliseconds = {
92819248
range: function(start, stop, step) {
92829249
return d3.range(+start, +stop, step).map(d3_time_scaleDate);
92839250
},
92849251
floor: d3_identity,
92859252
ceil: d3_identity
92869253
};
9254+
d3_time_scaleLocalMethods.year = d3_time.year;
9255+
d3_time.scale = function() {
9256+
return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat);
9257+
};
9258+
function d3_time_scaleMultiFormat(formats) {
9259+
return function(date) {
9260+
var i = formats.length - 1, f = formats[i];
9261+
while (!f[1](date)) f = formats[--i];
9262+
return f[0](date);
9263+
};
9264+
}
9265+
var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) {
9266+
return [ m[0].utc, m[1] ];
9267+
});
9268+
var d3_time_scaleUtcFormat = d3_time_scaleMultiFormat([ [ d3_time_formatUtc("%Y"), d3_true ], [ d3_time_formatUtc("%B"), function(d) {
9269+
return d.getUTCMonth();
9270+
} ], [ d3_time_formatUtc("%b %d"), function(d) {
9271+
return d.getUTCDate() != 1;
9272+
} ], [ d3_time_formatUtc("%a %d"), function(d) {
9273+
return d.getUTCDay() && d.getUTCDate() != 1;
9274+
} ], [ d3_time_formatUtc("%I %p"), function(d) {
9275+
return d.getUTCHours();
9276+
} ], [ d3_time_formatUtc("%I:%M"), function(d) {
9277+
return d.getUTCMinutes();
9278+
} ], [ d3_time_formatUtc(":%S"), function(d) {
9279+
return d.getUTCSeconds();
9280+
} ], [ d3_time_formatUtc(".%L"), function(d) {
9281+
return d.getUTCMilliseconds();
9282+
} ] ]);
9283+
d3_time_scaleUtcMethods.year = d3_time.year.utc;
9284+
d3_time.scale.utc = function() {
9285+
return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat);
9286+
};
92879287
d3.text = d3_xhrType(function(request) {
92889288
return request.responseText;
92899289
});

d3.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/locale/locale.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import "number-format";
22
import "time-format";
3-
import "time-scale-format";
43

54
d3.locale = function(locale) {
6-
var timeFormat = d3_locale_timeFormat(locale);
75
return {
86
numberFormat: d3_locale_numberFormat(locale),
9-
timeFormat: timeFormat,
10-
timeScaleFormat: d3_locale_timeScaleFormat(timeFormat)
7+
timeFormat: d3_locale_timeFormat(locale)
118
};
129
};

src/locale/time-scale-format.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/time/format-iso.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import "format";
2+
import "format-utc";
23
import "time";
34

4-
var d3_time_formatIso = d3_time_format.utc("%Y-%m-%dT%H:%M:%S.%LZ");
5+
var d3_time_formatIso = d3_time_formatUtc("%Y-%m-%dT%H:%M:%S.%LZ");
56

67
d3_time_format.iso = Date.prototype.toISOString && +new Date("2000-01-01T00:00:00.000Z")
78
? d3_time_formatIsoNative

src/time/format-utc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
import "format";
2+
3+
var d3_time_formatUtc = d3_time_format.utc;

src/time/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import "time";
22
import "format";
3+
import "format-utc";
34
import "format-iso";
45
import "interval";
56
import "second";
@@ -10,3 +11,4 @@ import "week";
1011
import "month";
1112
import "year";
1213
import "scale";
14+
import "scale-utc";

src/time/scale-utc.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
import "../core/true";
12
import "../scale/linear";
23
import "scale";
4+
import "format-utc";
35

46
var d3_time_scaleUtcMethods = d3_time_scaleLocalMethods.map(function(m) {
57
return [m[0].utc, m[1]];
68
});
79

10+
var d3_time_scaleUtcFormat = d3_time_scaleMultiFormat([
11+
[d3_time_formatUtc("%Y"), d3_true],
12+
[d3_time_formatUtc("%B"), function(d) { return d.getUTCMonth(); }],
13+
[d3_time_formatUtc("%b %d"), function(d) { return d.getUTCDate() != 1; }],
14+
[d3_time_formatUtc("%a %d"), function(d) { return d.getUTCDay() && d.getUTCDate() != 1; }],
15+
[d3_time_formatUtc("%I %p"), function(d) { return d.getUTCHours(); }],
16+
[d3_time_formatUtc("%I:%M"), function(d) { return d.getUTCMinutes(); }],
17+
[d3_time_formatUtc(":%S"), function(d) { return d.getUTCSeconds(); }],
18+
[d3_time_formatUtc(".%L"), function(d) { return d.getUTCMilliseconds(); }]
19+
]);
20+
821
d3_time_scaleUtcMethods.year = d3_time.year.utc;
922

1023
d3_time.scale.utc = function() {
11-
return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_locale_enUS.timeScaleFormat.utc);
24+
return d3_time_scale(d3.scale.linear(), d3_time_scaleUtcMethods, d3_time_scaleUtcFormat);
1225
};

src/time/scale.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import "../core/rebind";
55
import "../core/true";
66
import "../scale/linear";
77
import "../scale/nice";
8-
import "../locale/en-US";
9-
import "../time/day";
10-
import "../time/hour";
11-
import "../time/minute";
12-
import "../time/month";
13-
import "../time/second";
14-
import "../time/time";
15-
import "../time/week";
16-
import "../time/year";
8+
import "format";
9+
import "day";
10+
import "hour";
11+
import "minute";
12+
import "month";
13+
import "second";
14+
import "time";
15+
import "week";
16+
import "year";
1717

1818
function d3_time_scale(linear, methods, format) {
1919

@@ -132,11 +132,16 @@ var d3_time_scaleLocalMethods = [
132132
[d3_time.year, 1]
133133
];
134134

135-
d3_time_scaleLocalMethods.year = d3_time.year;
136-
137-
d3_time.scale = function() {
138-
return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_locale_enUS.timeScaleFormat);
139-
};
135+
var d3_time_scaleLocalFormat = d3_time_scaleMultiFormat([
136+
[d3_time_format("%Y"), d3_true],
137+
[d3_time_format("%B"), function(d) { return d.getMonth(); }],
138+
[d3_time_format("%b %d"), function(d) { return d.getDate() != 1; }],
139+
[d3_time_format("%a %d"), function(d) { return d.getDay() && d.getDate() != 1; }],
140+
[d3_time_format("%I %p"), function(d) { return d.getHours(); }],
141+
[d3_time_format("%I:%M"), function(d) { return d.getMinutes(); }],
142+
[d3_time_format(":%S"), function(d) { return d.getSeconds(); }],
143+
[d3_time_format(".%L"), function(d) { return d.getMilliseconds(); }]
144+
]);
140145

141146
var d3_time_scaleMilliseconds = {
142147
range: function(start, stop, step) {
@@ -145,3 +150,17 @@ var d3_time_scaleMilliseconds = {
145150
floor: d3_identity,
146151
ceil: d3_identity
147152
};
153+
154+
d3_time_scaleLocalMethods.year = d3_time.year;
155+
156+
d3_time.scale = function() {
157+
return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat);
158+
};
159+
160+
function d3_time_scaleMultiFormat(formats) {
161+
return function(date) {
162+
var i = formats.length - 1, f = formats[i];
163+
while (!f[1](date)) f = formats[--i];
164+
return f[0](date);
165+
};
166+
}

0 commit comments

Comments
 (0)