Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add half-year format directive
  • Loading branch information
archmoj committed Jun 23, 2021
commit e2508bc4e0abea1e27c689881343aba436a4139f
14 changes: 13 additions & 1 deletion src/lib/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,19 @@ exports.cleanDate = function(v, dflt, calendar) {
*/

/*
* modDateFormat: Support world calendars, and add one item to
* modDateFormat: Support world calendars, and add two items to
* d3's vocabulary:
* %{n}f where n is the max number of digits of fractional seconds
* %h formats: half of the year as a decimal number [1,2]
*/
var fracMatch = /%\d?f/g;
var halfYearMatch = /%h/g;
var quarterToHalfYear = {
'1': 1,
'2': 1,
'3': 2,
'4': 2,
};
function modDateFormat(fmt, x, formatter, calendar) {
fmt = fmt.replace(fracMatch, function(match) {
var digits = Math.min(+(match.charAt(1)) || 6, 6);
Expand All @@ -386,6 +394,10 @@ function modDateFormat(fmt, x, formatter, calendar) {

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

fmt = fmt.replace(halfYearMatch, function() {
return quarterToHalfYear[formatter('%q')(d)];
});

if(isWorldCalendar(calendar)) {
try {
fmt = Registry.getComponentMethod('calendars', 'worldCalFmt')(fmt, x, calendar);
Expand Down
Binary file added test/image/baselines/period_positioning9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions test/image/mocks/period_positioning9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"data": [
{
"xperiod": "M6",
"name": "Half year",
"type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01", "2001-07", "2002-01", "2002-07", "2003-01", "2003-07"],
"texttemplate": "%{x}",
"textangle": 0
}
],
"layout": {
"title": {
"text": "half-year periods and tickformat"
},
"width": 400,
"height": 300,
"margin": {
"t": 50,
"b": 50,
"l": 25,
"r": 25
},

"xaxis": {
"dtick": "M6",
"tickformat": "%Y H%h",
"ticklabelmode": "period",
"tickcolor": "black"
}
}
}