Skip to content

Period positioning for Cartesian traces #5074

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 35 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
95d253d
introduce period positioning for cartesian traces
archmoj Aug 13, 2020
dd05f3c
add image tests for period positioning on cartesian traces
archmoj Aug 13, 2020
a4e6bba
handle monthly periods
archmoj Sep 1, 2020
7341abe
Merge remote-tracking branch 'origin/master' into period-positioning
archmoj Sep 2, 2020
bdffa60
update period labels on baselines
archmoj Sep 2, 2020
8de4021
update gl2d baseline
archmoj Sep 3, 2020
103e710
compute the exact number of days for mothly periods
archmoj Sep 2, 2020
6321beb
support positive integers for months
archmoj Sep 3, 2020
35b7954
ensure positive ms periods
archmoj Sep 3, 2020
42d4d8d
fixup monthly mock
archmoj Sep 3, 2020
33cf742
fixup first month and improve test
archmoj Sep 3, 2020
0212372
improve image test by using relavant tickformats and colors
archmoj Sep 3, 2020
1948e03
Merge remote-tracking branch 'origin/master' into period-positioning
archmoj Sep 8, 2020
14c5512
improve monthly and yearly periods and implement (x|y)period0
archmoj Sep 12, 2020
46ec3f4
calculate years from months - add 2020 Mondays test
archmoj Sep 14, 2020
f1f0995
add tick0 to the mock
archmoj Sep 14, 2020
866b069
move align period step to traces so that we could record original pos…
archmoj Sep 16, 2020
8d9411b
display original position in hover for scatter and bar traces
archmoj Sep 16, 2020
1952483
initial attempt to use Lib.incrementMonth to compute endDate
archmoj Sep 17, 2020
6b68e34
rename variable name
archmoj Sep 17, 2020
be52281
remove time zone offset and use UTC getters
archmoj Sep 17, 2020
afa8965
separate monthly periods from daily periods defined by milliseconds
archmoj Sep 17, 2020
05bb135
fix period0 leap year in mock
archmoj Sep 17, 2020
94a164e
display start and end periods on hover
archmoj Sep 17, 2020
ba48115
fixup histogram & histogram2d hover
archmoj Sep 18, 2020
74070e0
refactor instanceOrPeriod function
archmoj Sep 21, 2020
f0c6d80
add jasmine tests for hover on period positions
archmoj Sep 21, 2020
a90f95d
Do not display start and end periods on hover by default
archmoj Sep 22, 2020
69d6af9
fix hover for funnel, waterfall, heatmap, contour, box, ohlc, candles…
archmoj Sep 22, 2020
222b5fe
improve scattergl hover and tests
archmoj Sep 23, 2020
5ae9e1e
Merge branch 'master' into period-positioning
archmoj Sep 25, 2020
83b22a0
apply AlexJ's suggestion to use Lib.incrementMonth and simplify case …
archmoj Sep 25, 2020
397bf63
set period0 to be on first Monday of 2000
archmoj Sep 28, 2020
083eae3
use a Sunday instead of Monday for weekly period0
archmoj Sep 28, 2020
72f6de8
reuse Lib.dateTick0
archmoj Sep 28, 2020
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
69 changes: 69 additions & 0 deletions src/plots/cartesian/align_period.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2012-2020, 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 isNumeric = require('fast-isnumeric');
var ms2DateTime = require('../../lib').ms2DateTime;
var ONEDAY = require('../../constants/numerical').ONEDAY;

module.exports = function alignPeriod(trace, ax, axLetter, vals) {
var alignment = trace[axLetter + 'periodalignment'];
if(!alignment || alignment === 'start') return vals;

var dynamic = false;
var period = trace[axLetter + 'period'];
if(isNumeric(period)) {
period = +period; // milliseconds
if(period <= 0) return vals;
} else if(typeof period === 'string' && period.charAt(0) === 'M') {
var v = +(period.substring(1));
if(v > 0 && Math.round(v) === v) period = v; // positive integer months
else return vals;

dynamic = true;
}

if(period > 0) {
var ratio = (alignment === 'end') ? 1 : 0.5;

var len = vals.length;
for(var i = 0; i < len; i++) {
var delta;

if(dynamic) {
var dateStr = ms2DateTime(vals[i], 0, ax.calendar);
var d = new Date(dateStr);
var year = d.getFullYear();
var month = d.getMonth() + 1;

var totalDaysInMonths = 0;
for(var k = 0; k < period; k++) {
month += 1;
if(month > 12) {
month = 1;
year++;
}

var monthDays = (
new Date(year, month, 0)
).getDate();

totalDaysInMonths += monthDays;
}

delta = ONEDAY * totalDaysInMonths; // convert to ms
} else {
delta = period;
}

vals[i] += ratio * delta;
}
}
return vals;
};
4 changes: 4 additions & 0 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,10 @@ function formatDate(ax, out, hover, extraPrecision) {
var tr = ax._tickround;
var fmt = (hover && ax.hoverformat) || axes.getTickFormat(ax);

if(ax.ticklabelmode === 'period') {
extraPrecision = false;
}

if(extraPrecision) {
// second or sub-second precision: extra always shows max digits.
// for other fields, extra precision just adds one field.
Expand Down
5 changes: 5 additions & 0 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var ONEMIN = numConstants.ONEMIN;
var ONESEC = numConstants.ONESEC;

var axisIds = require('./axis_ids');
var alignPeriod = require('./align_period');

var constants = require('./constants');
var HOUR_PATTERN = constants.HOUR_PATTERN;
Expand Down Expand Up @@ -847,6 +848,10 @@ module.exports = function setConvert(ax, fullLayout) {
}
}

if(axType === 'date') {
arrayOut = alignPeriod(trace, ax, axLetter, arrayOut);
}

return arrayOut;
};

Expand Down
5 changes: 5 additions & 0 deletions src/traces/bar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ module.exports = {
y0: scatterAttrs.y0,
dy: scatterAttrs.dy,

xperiod: scatterAttrs.xperiod,
yperiod: scatterAttrs.yperiod,
xperiodalignment: scatterAttrs.xperiodalignment,
yperiodalignment: scatterAttrs.yperiodalignment,

text: scatterAttrs.text,
texttemplate: texttemplateAttrs({editType: 'plot'}, {
keys: constants.eventDataKeys
Expand Down
3 changes: 3 additions & 0 deletions src/traces/bar/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Color = require('../../components/color');
var Registry = require('../../registry');

var handleXYDefaults = require('../scatter/xy_defaults');
var handlePeriodDefaults = require('../scatter/period_defaults');
var handleStyleDefaults = require('./style_defaults');
var getAxisGroup = require('../../plots/cartesian/axis_ids').getAxisGroup;
var attributes = require('./attributes');
Expand All @@ -30,6 +31,8 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
return;
}

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

coerce('orientation', (traceOut.x && !traceOut.y) ? 'h' : 'v');
coerce('base');
coerce('offset');
Expand Down
5 changes: 5 additions & 0 deletions src/traces/box/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ module.exports = {
].join(' ')
},

xperiod: scatterAttrs.xperiod,
yperiod: scatterAttrs.yperiod,
xperiodalignment: scatterAttrs.xperiodalignment,
yperiodalignment: scatterAttrs.yperiodalignment,

name: {
valType: 'string',
role: 'info',
Expand Down
3 changes: 3 additions & 0 deletions src/traces/box/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var Lib = require('../../lib');
var Registry = require('../../registry');
var Color = require('../../components/color');
var handlePeriodDefaults = require('../scatter/period_defaults');
var handleGroupingDefaults = require('../bar/defaults').handleGroupingDefaults;
var autoType = require('../../plots/cartesian/axis_autotype');
var attributes = require('./attributes');
Expand All @@ -23,6 +24,8 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
handleSampleDefaults(traceIn, traceOut, coerce, layout);
if(traceOut.visible === false) return;

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

var hasPreCompStats = traceOut._hasPreCompStats;

if(hasPreCompStats) {
Expand Down
3 changes: 3 additions & 0 deletions src/traces/candlestick/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function directionAttrs(lineColorDefault) {
}

module.exports = {
xperiod: OHLCattrs.xperiod,
xperiodalignment: OHLCattrs.xperiodalignment,

x: OHLCattrs.x,
open: OHLCattrs.open,
high: OHLCattrs.high,
Expand Down
3 changes: 3 additions & 0 deletions src/traces/candlestick/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var Lib = require('../../lib');
var Color = require('../../components/color');
var handleOHLC = require('../ohlc/ohlc_defaults');
var handlePeriodDefaults = require('../scatter/period_defaults');
var attributes = require('./attributes');

module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
Expand All @@ -25,6 +26,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return;
}

handlePeriodDefaults(traceIn, traceOut, layout, coerce, {x: true});

coerce('line.width');

handleDirection(traceIn, traceOut, coerce, 'increasing');
Expand Down
6 changes: 6 additions & 0 deletions src/traces/contour/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ module.exports = extendFlat({
y: heatmapAttrs.y,
y0: heatmapAttrs.y0,
dy: heatmapAttrs.dy,

xperiod: heatmapAttrs.xperiod,
yperiod: heatmapAttrs.yperiod,
xperiodalignment: heatmapAttrs.xperiodalignment,
yperiodalignment: heatmapAttrs.yperiodalignment,

text: heatmapAttrs.text,
hovertext: heatmapAttrs.hovertext,
transpose: heatmapAttrs.transpose,
Expand Down
3 changes: 3 additions & 0 deletions src/traces/contour/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var Lib = require('../../lib');

var handleXYZDefaults = require('../heatmap/xyz_defaults');
var handlePeriodDefaults = require('../scatter/period_defaults');
var handleConstraintDefaults = require('./constraint_defaults');
var handleContoursDefaults = require('./contours_defaults');
var handleStyleDefaults = require('./style_defaults');
Expand All @@ -32,6 +33,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return;
}

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

coerce('text');
coerce('hovertext');
coerce('hovertemplate');
Expand Down
5 changes: 5 additions & 0 deletions src/traces/funnel/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module.exports = {
y0: barAttrs.y0,
dy: barAttrs.dy,

xperiod: barAttrs.xperiod,
yperiod: barAttrs.yperiod,
xperiodalignment: barAttrs.xperiodalignment,
yperiodalignment: barAttrs.yperiodalignment,

hovertext: barAttrs.hovertext,
hovertemplate: hovertemplateAttrs({}, {
keys: constants.eventDataKeys
Expand Down
3 changes: 3 additions & 0 deletions src/traces/funnel/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Lib = require('../../lib');
var handleGroupingDefaults = require('../bar/defaults').handleGroupingDefaults;
var handleText = require('../bar/defaults').handleText;
var handleXYDefaults = require('../scatter/xy_defaults');
var handlePeriodDefaults = require('../scatter/period_defaults');
var attributes = require('./attributes');
var Color = require('../../components/color');

Expand All @@ -27,6 +28,8 @@ function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
return;
}

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

coerce('orientation', (traceOut.y && !traceOut.x) ? 'v' : 'h');
coerce('offset');
coerce('width');
Expand Down
5 changes: 5 additions & 0 deletions src/traces/heatmap/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module.exports = extendFlat({
y0: extendFlat({}, scatterAttrs.y0, {impliedEdits: {ytype: 'scaled'}}),
dy: extendFlat({}, scatterAttrs.dy, {impliedEdits: {ytype: 'scaled'}}),

xperiod: extendFlat({}, scatterAttrs.xperiod, {impliedEdits: {xtype: 'scaled'}}),
yperiod: extendFlat({}, scatterAttrs.yperiod, {impliedEdits: {ytype: 'scaled'}}),
xperiodalignment: extendFlat({}, scatterAttrs.xperiodalignment, {impliedEdits: {xtype: 'scaled'}}),
yperiodalignment: extendFlat({}, scatterAttrs.yperiodalignment, {impliedEdits: {ytype: 'scaled'}}),

text: {
valType: 'data_array',
editType: 'calc',
Expand Down
3 changes: 3 additions & 0 deletions src/traces/heatmap/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
var Lib = require('../../lib');

var handleXYZDefaults = require('./xyz_defaults');
var handlePeriodDefaults = require('../scatter/period_defaults');
var handleStyleDefaults = require('./style_defaults');
var colorscaleDefaults = require('../../components/colorscale/defaults');
var attributes = require('./attributes');
Expand All @@ -28,6 +29,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return;
}

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

coerce('text');
coerce('hovertext');
coerce('hovertemplate');
Expand Down
5 changes: 5 additions & 0 deletions src/traces/histogram/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ module.exports = {
].join(' ')
},

xperiod: barAttrs.xperiod,
yperiod: barAttrs.yperiod,
xperiodalignment: barAttrs.xperiodalignment,
yperiodalignment: barAttrs.yperiodalignment,

text: extendFlat({}, barAttrs.text, {
description: [
'Sets hover text elements associated with each bar.',
Expand Down
3 changes: 3 additions & 0 deletions src/traces/histogram/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Registry = require('../../registry');
var Lib = require('../../lib');
var Color = require('../../components/color');

var handlePeriodDefaults = require('../scatter/period_defaults');
var handleStyleDefaults = require('../bar/style_defaults');
var attributes = require('./attributes');

Expand Down Expand Up @@ -46,6 +47,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return;
}

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

traceOut._length = len;

var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
Expand Down
5 changes: 5 additions & 0 deletions src/traces/histogram2d/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ module.exports = extendFlat(
x: histogramAttrs.x,
y: histogramAttrs.y,

xperiod: histogramAttrs.xperiod,
yperiod: histogramAttrs.yperiod,
xperiodalignment: histogramAttrs.xperiodalignment,
yperiodalignment: histogramAttrs.yperiodalignment,

z: {
valType: 'data_array',
editType: 'calc',
Expand Down
3 changes: 3 additions & 0 deletions src/traces/histogram2d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

var handlePeriodDefaults = require('../scatter/period_defaults');
var handleSampleDefaults = require('./sample_defaults');
var handleStyleDefaults = require('../heatmap/style_defaults');
var colorscaleDefaults = require('../../components/colorscale/defaults');
Expand All @@ -25,6 +26,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
handleSampleDefaults(traceIn, traceOut, coerce, layout);
if(traceOut.visible === false) return;

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

handleStyleDefaults(traceIn, traceOut, coerce, layout);
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'});
coerce('hovertemplate');
Expand Down
5 changes: 5 additions & 0 deletions src/traces/histogram2dcontour/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ var colorScaleAttrs = require('../../components/colorscale/attributes');
var extendFlat = require('../../lib/extend').extendFlat;

module.exports = extendFlat({
xperiod: histogram2dAttrs.xperiod,
yperiod: histogram2dAttrs.yperiod,
xperiodalignment: histogram2dAttrs.xperiodalignment,
yperiodalignment: histogram2dAttrs.yperiodalignment,

x: histogram2dAttrs.x,
y: histogram2dAttrs.y,
z: histogram2dAttrs.z,
Expand Down
3 changes: 3 additions & 0 deletions src/traces/histogram2dcontour/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

var handlePeriodDefaults = require('../scatter/period_defaults');
var handleSampleDefaults = require('../histogram2d/sample_defaults');
var handleContoursDefaults = require('../contour/contours_defaults');
var handleStyleDefaults = require('../contour/style_defaults');
Expand All @@ -29,6 +30,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
handleSampleDefaults(traceIn, traceOut, coerce, layout);
if(traceOut.visible === false) return;

handlePeriodDefaults(traceIn, traceOut, layout, coerce);

handleContoursDefaults(traceIn, traceOut, coerce, coerce2);
handleStyleDefaults(traceIn, traceOut, coerce, layout);
coerce('hovertemplate');
Expand Down
3 changes: 3 additions & 0 deletions src/traces/ohlc/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function directionAttrs(lineColorDefault) {

module.exports = {

xperiod: scatterAttrs.xperiod,
xperiodalignment: scatterAttrs.xperiodalignment,

x: {
valType: 'data_array',
editType: 'calc+clearAxisTypes',
Expand Down
3 changes: 3 additions & 0 deletions src/traces/ohlc/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var Lib = require('../../lib');
var handleOHLC = require('./ohlc_defaults');
var handlePeriodDefaults = require('../scatter/period_defaults');
var attributes = require('./attributes');

module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
Expand All @@ -24,6 +25,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
return;
}

handlePeriodDefaults(traceIn, traceOut, layout, coerce, {x: true});

coerce('line.width');
coerce('line.dash');

Expand Down
Loading