Skip to content

World calendars #1220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d2a5e4b
simpleMap utility so we don't pass unexpected args
alexcjohnson Dec 2, 2016
6151575
change gl2d_date_axes mock to date string axis range
alexcjohnson Nov 30, 2016
4b9edec
add world calendar support part 1
alexcjohnson Nov 30, 2016
4d8f79a
overhaul set_convert so I can tell where calendars go
alexcjohnson Dec 2, 2016
84a51c2
add world calendar support part 2
alexcjohnson Dec 2, 2016
13cf6b1
world-cals image mock
alexcjohnson Dec 4, 2016
165125b
fix 3d calendar attributes
alexcjohnson Dec 4, 2016
86b31ea
fix gl3d with world calendars
alexcjohnson Dec 5, 2016
78b6646
test world-cals in scattergl & gl2d axes
alexcjohnson Dec 5, 2016
1b563f5
test world calendars with finance charts
alexcjohnson Dec 5, 2016
4e9a632
remove mistaken paste
alexcjohnson Dec 6, 2016
c1c24e8
support world cals in rangesliders
alexcjohnson Dec 6, 2016
a435981
partial support for range selectors on world calendars
alexcjohnson Dec 6, 2016
6653da7
get the right date string replacement for candlestick mock
alexcjohnson Dec 6, 2016
8e1747f
bigger tolerance on updatemenus_test width test
alexcjohnson Dec 6, 2016
00ae2dd
fix and test layout.calendar inheritance
alexcjohnson Dec 6, 2016
3143099
move world-calendar logic in lib/dates.js to new 'calendars' component
etpinard Dec 6, 2016
5984106
coerce calendar attributes in calenders component
etpinard Dec 6, 2016
03ab34f
add requirable 'calendars' module + include it in main bundle
etpinard Dec 6, 2016
db3d18b
fix typo in getComponentMethod call
etpinard Dec 6, 2016
05b2f96
skip over calendar attribute in findArrayAttributes
etpinard Dec 6, 2016
61ecd42
generalise plot schema handling of component attributes
etpinard Dec 6, 2016
68af287
add schema attributes in calendars module
etpinard Dec 6, 2016
cb2c54b
move calendar defaults after early return
etpinard Dec 7, 2016
bc457a9
Merge pull request #1230 from plotly/world-cals-component
etpinard Dec 7, 2016
1444f55
use only the calendars we need from 'world-calendars'
etpinard Dec 7, 2016
08f18ca
Merge pull request #1237 from plotly/world-cals-trimmed
etpinard Dec 7, 2016
dcddcee
support chinese calendar
alexcjohnson Dec 8, 2016
bbb76a4
Merge branch 'master' into world-cals
alexcjohnson Dec 8, 2016
8d8e936
fix some tests for chinese, and robustify dateTime2ms
alexcjohnson Dec 8, 2016
0e05f95
update baseline image with chinese calendar
alexcjohnson Dec 8, 2016
509f287
perf: parseInt for string-leading int extraction
alexcjohnson Dec 9, 2016
7bd501f
prevent non-gregorian month/year todate range selectors
alexcjohnson Dec 9, 2016
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
11 changes: 11 additions & 0 deletions lib/calendars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

module.exports = require('../src/components/calendars');
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ Plotly.register([
require('./groupby')
]);

// components
Plotly.register([
require('./calendars')
]);

module.exports = Plotly;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"superscript-text": "^1.0.0",
"tinycolor2": "^1.3.0",
"topojson-client": "^2.1.0",
"webgl-context": "^2.2.0"
"webgl-context": "^2.2.0",
"world-calendars": "^1.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍻

},
"devDependencies": {
"brfs": "^1.4.3",
Expand Down
4 changes: 2 additions & 2 deletions src/components/annotations/calc_autorange.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ function annAutorange(gd) {
}

if(xa && xa.autorange) {
Axes.expand(xa, [xa.l2c(xa.r2l(ann.x))], {
Axes.expand(xa, [xa.r2c(ann.x)], {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

ppadplus: rightSize,
ppadminus: leftSize
});
}

if(ya && ya.autorange) {
Axes.expand(ya, [ya.l2c(ya.r2l(ann.y))], {
Axes.expand(ya, [ya.r2c(ann.y)], {
ppadplus: bottomSize,
ppadminus: topSize
});
Expand Down
31 changes: 31 additions & 0 deletions src/components/calendars/calendars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

// a trimmed down version of:
// https://github.com/alexcjohnson/world-calendars/blob/master/dist/index.js

module.exports = require('world-calendars/dist/main');

require('world-calendars/dist/plus');

require('world-calendars/dist/calendars/chinese');
require('world-calendars/dist/calendars/coptic');
require('world-calendars/dist/calendars/discworld');
require('world-calendars/dist/calendars/ethiopian');
require('world-calendars/dist/calendars/hebrew');
require('world-calendars/dist/calendars/islamic');
require('world-calendars/dist/calendars/julian');
require('world-calendars/dist/calendars/mayan');
require('world-calendars/dist/calendars/nanakshahi');
require('world-calendars/dist/calendars/nepali');
require('world-calendars/dist/calendars/persian');
require('world-calendars/dist/calendars/taiwan');
require('world-calendars/dist/calendars/thai');
require('world-calendars/dist/calendars/ummalqura');
253 changes: 253 additions & 0 deletions src/components/calendars/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var calendars = require('./calendars');

var Lib = require('../../lib');
var constants = require('../../constants/numerical');

var EPOCHJD = constants.EPOCHJD;
var ONEDAY = constants.ONEDAY;

var attributes = {
valType: 'enumerated',
values: Object.keys(calendars.calendars),
role: 'info',
dflt: 'gregorian'
};

var handleDefaults = function(contIn, contOut, attr, dflt) {
var attrs = {};
attrs[attr] = attributes;

return Lib.coerce(contIn, contOut, attrs, attr, dflt);
};

var handleTraceDefaults = function(traceIn, traceOut, coords, layout) {
for(var i = 0; i < coords.length; i++) {
handleDefaults(traceIn, traceOut, coords[i] + 'calendar', layout.calendar);
}
};

// each calendar needs its own default canonical tick. I would love to use
// 2000-01-01 (or even 0000-01-01) for them all but they don't necessarily
// all support either of those dates. Instead I'll use the most significant
// number they *do* support, biased toward the present day.
var CANONICAL_TICK = {
chinese: '2000-01-01',
coptic: '2000-01-01',
discworld: '2000-01-01',
ethiopian: '2000-01-01',
hebrew: '5000-01-01',
islamic: '1000-01-01',
julian: '2000-01-01',
mayan: '5000-01-01',
nanakshahi: '1000-01-01',
nepali: '2000-01-01',
persian: '1000-01-01',
jalali: '1000-01-01',
taiwan: '1000-01-01',
thai: '2000-01-01',
ummalqura: '1400-01-01'
};

// Start on a Sunday - for week ticks
// Discworld and Mayan calendars don't have 7-day weeks but we're going to give them
// 7-day week ticks so start on our Sundays.
// If anyone really cares we can customize the auto tick spacings for these calendars.
var CANONICAL_SUNDAY = {
chinese: '2000-01-02',
coptic: '2000-01-03',
discworld: '2000-01-03',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug fix or typo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the comment change above - I decided it would be better to keep our Sundays unless and until we support the native weeks in these calendars.

ethiopian: '2000-01-05',
hebrew: '5000-01-01',
islamic: '1000-01-02',
julian: '2000-01-03',
mayan: '5000-01-01',
nanakshahi: '1000-01-05',
nepali: '2000-01-05',
persian: '1000-01-01',
jalali: '1000-01-01',
taiwan: '1000-01-04',
thai: '2000-01-04',
ummalqura: '1400-01-06'
};

var DFLTRANGE = {
chinese: ['2000-01-01', '2001-01-01'],
coptic: ['1700-01-01', '1701-01-01'],
discworld: ['1800-01-01', '1801-01-01'],
ethiopian: ['2000-01-01', '2001-01-01'],
hebrew: ['5700-01-01', '5701-01-01'],
islamic: ['1400-01-01', '1401-01-01'],
julian: ['2000-01-01', '2001-01-01'],
mayan: ['5200-01-01', '5201-01-01'],
nanakshahi: ['0500-01-01', '0501-01-01'],
nepali: ['2000-01-01', '2001-01-01'],
persian: ['1400-01-01', '1401-01-01'],
jalali: ['1400-01-01', '1401-01-01'],
taiwan: ['0100-01-01', '0101-01-01'],
thai: ['2500-01-01', '2501-01-01'],
ummalqura: ['1400-01-01', '1401-01-01']
};

/*
* convert d3 templates to world-calendars templates, so our users only need
* to know d3's specifiers. Map space padding to no padding, and unknown fields
* to an ugly placeholder
*/
var UNKNOWN = '##';
var d3ToWorldCalendars = {
'd': {'0': 'dd', '-': 'd'}, // 2-digit or unpadded day of month
'a': {'0': 'D', '-': 'D'}, // short weekday name
'A': {'0': 'DD', '-': 'DD'}, // full weekday name
'j': {'0': 'oo', '-': 'o'}, // 3-digit or unpadded day of the year
'W': {'0': 'ww', '-': 'w'}, // 2-digit or unpadded week of the year (Monday first)
'm': {'0': 'mm', '-': 'm'}, // 2-digit or unpadded month number
'b': {'0': 'M', '-': 'M'}, // short month name
'B': {'0': 'MM', '-': 'MM'}, // full month name
'y': {'0': 'yy', '-': 'yy'}, // 2-digit year (map unpadded to zero-padded)
'Y': {'0': 'yyyy', '-': 'yyyy'}, // 4-digit year (map unpadded to zero-padded)
'U': UNKNOWN, // Sunday-first week of the year
'w': UNKNOWN, // day of the week [0(sunday),6]
// combined format, we replace the date part with the world-calendar version
// and the %X stays there for d3 to handle with time parts
'%c': {'0': 'D M m %X yyyy', '-': 'D M m %X yyyy'},
'%x': {'0': 'mm/dd/yyyy', '-': 'mm/dd/yyyy'}
};

function worldCalFmt(fmt, x, calendar) {
var dateJD = Math.floor(x + 0.05 / ONEDAY) + EPOCHJD,
cDate = getCal(calendar).fromJD(dateJD),
i = 0,
modifier, directive, directiveLen, directiveObj, replacementPart;
while((i = fmt.indexOf('%', i)) !== -1) {
modifier = fmt.charAt(i + 1);
if(modifier === '0' || modifier === '-' || modifier === '_') {
directiveLen = 3;
directive = fmt.charAt(i + 1);
if(modifier === '_') modifier = '-';
}
else {
directive = modifier;
modifier = '0';
directiveLen = 2;
}
directiveObj = d3ToWorldCalendars[directive];
if(!directiveObj) {
i += directiveLen;
}
else {
// code is recognized as a date part but world-calendars doesn't support it
if(directiveObj === UNKNOWN) replacementPart = UNKNOWN;

// format the cDate according to the translated directive
else replacementPart = cDate.formatDate(directiveObj[modifier]);

fmt = fmt.substr(0, i) + replacementPart + fmt.substr(i + directiveLen);
i += replacementPart.length;
}
}
return fmt;
}

// cache world calendars, so we don't have to reinstantiate
// during each date-time conversion
var allCals = {};
function getCal(calendar) {
var calendarObj = allCals[calendar];
if(calendarObj) return calendarObj;

calendarObj = allCals[calendar] = calendars.instance(calendar);
return calendarObj;
}

function makeAttrs(description) {
return Lib.extendFlat({}, attributes, { description: description });
}

function makeTraceAttrsDescription(coord) {
return 'Sets the calendar system to use with `' + coord + '` date data.';
}

var xAttrs = {
xcalendar: makeAttrs(makeTraceAttrsDescription('x'))
};

var xyAttrs = Lib.extendFlat({}, xAttrs, {
ycalendar: makeAttrs(makeTraceAttrsDescription('y'))
});

var xyzAttrs = Lib.extendFlat({}, xyAttrs, {
zcalendar: makeAttrs(makeTraceAttrsDescription('z'))
});

var axisAttrs = makeAttrs([
'Sets the calendar system to use for `range` and `tick0`',
'if this is a date axis. This does not set the calendar for',
'interpreting data on this axis, that\'s specified in the trace',
'or via the global `layout.calendar`'
].join(' '));

module.exports = {
moduleType: 'component',
name: 'calendars',

schema: {
traces: {
scatter: xyAttrs,
bar: xyAttrs,
heatmap: xyAttrs,
contour: xyAttrs,
histogram: xyAttrs,
histogram2d: xyAttrs,
histogram2dcontour: xyAttrs,
scatter3d: xyzAttrs,
surface: xyzAttrs,
mesh3d: xyzAttrs,
scattergl: xyAttrs,
ohlc: xAttrs,
candlestick: xAttrs
},
layout: {
calendar: makeAttrs([
'Sets the default calendar system to use for interpreting and',
'displaying dates throughout the plot.'
].join(' ')),
'xaxis.calendar': axisAttrs,
'yaxis.calendar': axisAttrs,
'scene.xaxis.calendar': axisAttrs,
'scene.yaxis.calendar': axisAttrs,
'scene.zaxis.calendar': axisAttrs
},
transforms: {
filter: {
calendar: makeAttrs([
'Sets the calendar system to use for `value`, if it is a date.',
'Note that this is not necessarily the same calendar as is used',
'for the target data; that is set by its own calendar attribute,',
'ie `trace.x` uses `trace.xcalendar` etc.'
].join(' '))
}
}
},

layoutAttributes: attributes,

handleDefaults: handleDefaults,
handleTraceDefaults: handleTraceDefaults,

CANONICAL_SUNDAY: CANONICAL_SUNDAY,
CANONICAL_TICK: CANONICAL_TICK,
DFLTRANGE: DFLTRANGE,

getCal: getCal,
worldCalFmt: worldCalFmt
};
3 changes: 2 additions & 1 deletion src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ module.exports = function draw(gd, id) {
axisOptions = {
letter: 'y',
font: fullLayout.font,
noHover: true
noHover: true,
calendar: fullLayout.calendar // not really necessary (yet?)
};

// Coerce w.r.t. Axes layoutAttributes:
Expand Down
9 changes: 3 additions & 6 deletions src/components/rangeselector/get_update_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

var d3 = require('d3');

var Lib = require('../../lib');


module.exports = function getUpdateObject(axisLayout, buttonLayout) {
var axName = axisLayout._name;
var update = {};
Expand All @@ -33,7 +30,7 @@ module.exports = function getUpdateObject(axisLayout, buttonLayout) {

function getXRange(axisLayout, buttonLayout) {
var currentRange = axisLayout.range;
var base = new Date(Lib.dateTime2ms(currentRange[1]));
var base = new Date(axisLayout.r2l(currentRange[1]));

var step = buttonLayout.step,
count = buttonLayout.count;
Expand All @@ -42,13 +39,13 @@ function getXRange(axisLayout, buttonLayout) {

switch(buttonLayout.stepmode) {
case 'backward':
range0 = Lib.ms2DateTime(+d3.time[step].utc.offset(base, -count));
range0 = axisLayout.l2r(+d3.time[step].utc.offset(base, -count));
break;

case 'todate':
var base2 = d3.time[step].utc.offset(base, -count);

range0 = Lib.ms2DateTime(+d3.time[step].utc.ceil(base2));
range0 = axisLayout.l2r(+d3.time[step].utc.ceil(base2));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a weird one. At first when I played with range selectors it seemed like they just worked ™️ - if only we were that lucky. They worked (to within errors I would not have been able to notice) as long as the range end is valid in the gregorian calendar and the new start is valid in the axis calendar, but there's no guarantee that that will be true in general. The fix here at least means we properly convert between range values and milliseconds.

What it doesn't get right is stepping backward by some months or years - these steps are going to be somewhat different in each calendar.

What it really doesn't get right is month or year to date. It's going to go to the beginning of the Gregorian month or year, which is generally totally different from in the other calendar. I propose to leave this as an open item for now, because it's going to have all sorts of edge cases to sort out (like in the Hebrew calendar, when the year changes at month 7, not month 1) for a niche-upon-niche feature.

Copy link
Contributor

@etpinard etpinard Dec 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to leave this as an open item for now, because it's going to have all sorts of edge cases to sort out

I agree 👍 . But let's make sure the range selector supplyDefaults doesn't allow axis.calendar !== 'gregorian' with axis.rangeselector.buttons[i].stepmode = 'todate'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no year/month todate (but smaller increments are still allowed) range selectors outside Gregorian calendars: 7bd501f

break;
}

Expand Down
7 changes: 6 additions & 1 deletion src/components/rangeselector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ module.exports = {
moduleType: 'component',
name: 'rangeselector',

layoutNodes: ['xaxis.'],
schema: {
layout: {
'xaxis.rangeselector': require('./attributes')
}
},

layoutAttributes: require('./attributes'),
handleDefaults: require('./defaults'),

Expand Down
Loading