Skip to content

Commit e2508bc

Browse files
committed
add half-year format directive
1 parent 608337e commit e2508bc

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/lib/dates.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,19 @@ exports.cleanDate = function(v, dflt, calendar) {
370370
*/
371371

372372
/*
373-
* modDateFormat: Support world calendars, and add one item to
373+
* modDateFormat: Support world calendars, and add two items to
374374
* d3's vocabulary:
375375
* %{n}f where n is the max number of digits of fractional seconds
376+
* %h formats: half of the year as a decimal number [1,2]
376377
*/
377378
var fracMatch = /%\d?f/g;
379+
var halfYearMatch = /%h/g;
380+
var quarterToHalfYear = {
381+
'1': 1,
382+
'2': 1,
383+
'3': 2,
384+
'4': 2,
385+
};
378386
function modDateFormat(fmt, x, formatter, calendar) {
379387
fmt = fmt.replace(fracMatch, function(match) {
380388
var digits = Math.min(+(match.charAt(1)) || 6, 6);
@@ -386,6 +394,10 @@ function modDateFormat(fmt, x, formatter, calendar) {
386394

387395
var d = new Date(Math.floor(x + 0.05));
388396

397+
fmt = fmt.replace(halfYearMatch, function() {
398+
return quarterToHalfYear[formatter('%q')(d)];
399+
});
400+
389401
if(isWorldCalendar(calendar)) {
390402
try {
391403
fmt = Registry.getComponentMethod('calendars', 'worldCalFmt')(fmt, x, calendar);
14.7 KB
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"data": [
3+
{
4+
"xperiod": "M6",
5+
"name": "Half year",
6+
"type": "bar",
7+
"y": [1, 2, 3, 4, 5, 6],
8+
"x": ["2001-01", "2001-07", "2002-01", "2002-07", "2003-01", "2003-07"],
9+
"texttemplate": "%{x}",
10+
"textangle": 0
11+
}
12+
],
13+
"layout": {
14+
"title": {
15+
"text": "half-year periods and tickformat"
16+
},
17+
"width": 400,
18+
"height": 300,
19+
"margin": {
20+
"t": 50,
21+
"b": 50,
22+
"l": 25,
23+
"r": 25
24+
},
25+
26+
"xaxis": {
27+
"dtick": "M6",
28+
"tickformat": "%Y H%h",
29+
"ticklabelmode": "period",
30+
"tickcolor": "black"
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)