Skip to content

Fix gl3d ticks when converting dates to milliseconds #4903

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 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ lib.bBoxIntersect = function(a, b, pad) {
* func: the function to apply
* x1, x2: optional extra args
*/
lib.simpleMap = function(array, func, x1, x2) {
lib.simpleMap = function(array, func, x1, x2, opts) {
var len = array.length;
var out = new Array(len);
for(var i = 0; i < len; i++) out[i] = func(array[i], x1, x2);
for(var i = 0; i < len; i++) out[i] = func(array[i], x1, x2, opts);
return out;
};

Expand Down
16 changes: 8 additions & 8 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ function autoShiftMonthBins(binStart, data, dtick, dataMin, calendar) {
// ----------------------------------------------------

// ensure we have tick0, dtick, and tick rounding calculated
axes.prepTicks = function(ax) {
var rng = Lib.simpleMap(ax.range, ax.r2l);
axes.prepTicks = function(ax, opts) {
var rng = Lib.simpleMap(ax.range, ax.r2l, undefined, undefined, opts);

// calculate max number of (auto) ticks to display based on plot size
if(ax.tickmode === 'auto' || !ax.dtick) {
Expand Down Expand Up @@ -563,16 +563,16 @@ axes.prepTicks = function(ax) {
// if ticks are set to automatic, determine the right values (tick0,dtick)
// in any case, set tickround to # of digits to round tick labels to,
// or codes to this effect for log and date scales
axes.calcTicks = function calcTicks(ax) {
axes.prepTicks(ax);
var rng = Lib.simpleMap(ax.range, ax.r2l);
axes.calcTicks = function calcTicks(ax, opts) {
axes.prepTicks(ax, opts);
var rng = Lib.simpleMap(ax.range, ax.r2l, undefined, undefined, opts);

// now that we've figured out the auto values for formatting
// in case we're missing some ticktext, we can break out for array ticks
if(ax.tickmode === 'array') return arrayTicks(ax);

// find the first tick
ax._tmin = axes.tickFirst(ax);
ax._tmin = axes.tickFirst(ax, opts);

// add a tiny bit so we get ticks which may have rounded out
var exRng = expandRange(rng);
Expand Down Expand Up @@ -962,9 +962,9 @@ axes.tickIncrement = function(x, dtick, axrev, calendar) {
};

// calculate the first tick on an axis
axes.tickFirst = function(ax) {
axes.tickFirst = function(ax, opts) {
var r2l = ax.r2l || Number;
var rng = Lib.simpleMap(ax.range, r2l);
var rng = Lib.simpleMap(ax.range, r2l, undefined, undefined, opts);
var axrev = rng[1] < rng[0];
var sRound = axrev ? Math.floor : Math.ceil;
// add a tiny extra bit to make sure we get ticks
Expand Down
10 changes: 5 additions & 5 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = function setConvert(ax, fullLayout) {
* - inserts a dummy arg so calendar is the 3rd arg (see notes below).
* - defaults to ax.calendar
*/
function dt2ms(v, _, calendar, msUTC) {
function dt2ms(v, _, calendar, opts) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

good call. At some point I bet we could collapse all these extra args into a single opts arg (ie dt2ms(v, opts)) but that's a big and potentially risky project - which some users may consider a breaking change though the ax.a2b functions are sufficiently "internal" that I personally wouldn't be bothered by it. Anyway not a change to make now.

// NOTE: Changed this behavior: previously we took any numeric value
// to be a ms, even if it was a string that could be a bare year.
// Now we convert it as a date if at all possible, and only try
Expand All @@ -99,7 +99,7 @@ module.exports = function setConvert(ax, fullLayout) {
if(ms === BADNUM) {
if(isNumeric(v)) {
v = +v;
if(msUTC) {
if((opts || {}).msUTC) {
// For now it is only used
// to fix bar length in milliseconds.
// It could be applied in other places in v2
Expand Down Expand Up @@ -798,7 +798,7 @@ module.exports = function setConvert(ax, fullLayout) {
// the first letter of ax._id?)
// in case the expected data isn't there, make a list of
// integers based on the opposite data
ax.makeCalcdata = function(trace, axLetter, msUTC) {
ax.makeCalcdata = function(trace, axLetter, opts) {
var arrayIn, arrayOut, i, len;

var axType = ax.type;
Expand All @@ -822,10 +822,10 @@ module.exports = function setConvert(ax, fullLayout) {

arrayOut = new Array(len);
for(i = 0; i < len; i++) {
arrayOut[i] = ax.d2c(arrayIn[i], 0, cal, msUTC);
arrayOut[i] = ax.d2c(arrayIn[i], 0, cal, opts);
}
} else {
var v0 = ((axLetter + '0') in trace) ? ax.d2c(trace[axLetter + '0'], 0, cal, false) : 0;
var v0 = ((axLetter + '0') in trace) ? ax.d2c(trace[axLetter + '0'], 0, cal) : 0;
var dv = (trace['d' + axLetter]) ? Number(trace['d' + axLetter]) : 1;

// the opposing data, for size if we have x and dx etc
Expand Down
2 changes: 1 addition & 1 deletion src/plots/gl3d/layout/tick_marks.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function computeTickMarks(scene) {
var nticks = axes.nticks || Lib.constrain((axes._length / 40), 4, 9);
Axes.autoTicks(axes, Math.abs(axes.range[1] - axes.range[0]) / nticks);
}
var dataTicks = Axes.calcTicks(axes);
var dataTicks = Axes.calcTicks(axes, { msUTC: true });
for(var j = 0; j < dataTicks.length; ++j) {
dataTicks[j].x = dataTicks[j].x * scene.dataScale[i];

Expand Down
8 changes: 5 additions & 3 deletions src/traces/bar/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ module.exports = function calc(gd, trace) {
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
var size, pos;

var msUTC = !!(trace.base || trace.base === 0);
var sizeOpts = {
msUTC: !!(trace.base || trace.base === 0)
};

if(trace.orientation === 'h') {
size = xa.makeCalcdata(trace, 'x', msUTC);
size = xa.makeCalcdata(trace, 'x', sizeOpts);
pos = ya.makeCalcdata(trace, 'y');
} else {
size = ya.makeCalcdata(trace, 'y', msUTC);
size = ya.makeCalcdata(trace, 'y', sizeOpts);
pos = xa.makeCalcdata(trace, 'x');
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions test/image/mocks/gl3d_ticks-milliseconds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"data": [{
"type": "scatter3d",
"x": ["1970-01-01 01:00:00", "1970-01-01 13:00:00"],
"y": ["1970-01-01 01:00:00", "1970-01-01 02:00:00"],
"z": ["1970-01-01 01:00:00", "1970-01-01 01:30:00"]
}],
"layout": {
"width": 400,
"height": 400,
"margin": {
"t": 10,
"b": 10,
"l": 10,
"r": 10
},
"scene": {
"xaxis": { "nticks": 12, "type": "date" },
"yaxis": { "nticks": 12, "type": "date" },
"zaxis": { "nticks": 12, "type": "date" },
"camera": {
"eye": { "x": 0.25, "y": 2.5, "z": 0.25 }
}
}
}
}