-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Period positioning for Cartesian traces #5074
Merged
Merged
Changes from 30 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
95d253d
introduce period positioning for cartesian traces
archmoj dd05f3c
add image tests for period positioning on cartesian traces
archmoj a4e6bba
handle monthly periods
archmoj 7341abe
Merge remote-tracking branch 'origin/master' into period-positioning
archmoj bdffa60
update period labels on baselines
archmoj 8de4021
update gl2d baseline
archmoj 103e710
compute the exact number of days for mothly periods
archmoj 6321beb
support positive integers for months
archmoj 35b7954
ensure positive ms periods
archmoj 42d4d8d
fixup monthly mock
archmoj 33cf742
fixup first month and improve test
archmoj 0212372
improve image test by using relavant tickformats and colors
archmoj 1948e03
Merge remote-tracking branch 'origin/master' into period-positioning
archmoj 14c5512
improve monthly and yearly periods and implement (x|y)period0
archmoj 46ec3f4
calculate years from months - add 2020 Mondays test
archmoj f1f0995
add tick0 to the mock
archmoj 866b069
move align period step to traces so that we could record original pos…
archmoj 8d9411b
display original position in hover for scatter and bar traces
archmoj 1952483
initial attempt to use Lib.incrementMonth to compute endDate
archmoj 6b68e34
rename variable name
archmoj be52281
remove time zone offset and use UTC getters
archmoj afa8965
separate monthly periods from daily periods defined by milliseconds
archmoj 05bb135
fix period0 leap year in mock
archmoj 94a164e
display start and end periods on hover
archmoj ba48115
fixup histogram & histogram2d hover
archmoj 74070e0
refactor instanceOrPeriod function
archmoj f0c6d80
add jasmine tests for hover on period positions
archmoj a90f95d
Do not display start and end periods on hover by default
archmoj 69d6af9
fix hover for funnel, waterfall, heatmap, contour, box, ohlc, candles…
archmoj 222b5fe
improve scattergl hover and tests
archmoj 5ae9e1e
Merge branch 'master' into period-positioning
archmoj 83b22a0
apply AlexJ's suggestion to use Lib.incrementMonth and simplify case …
archmoj 397bf63
set period0 to be on first Monday of 2000
archmoj 083eae3
use a Sunday instead of Monday for weekly period0
archmoj 72f6de8
reuse Lib.dateTick0
archmoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* 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 Lib = require('../../lib'); | ||
var ms2DateTime = Lib.ms2DateTime; | ||
var dateTime2ms = Lib.dateTime2ms; | ||
var incrementMonth = Lib.incrementMonth; | ||
var ONEDAY = require('../../constants/numerical').ONEDAY; | ||
|
||
module.exports = function alignPeriod(trace, ax, axLetter, vals) { | ||
if(ax.type !== 'date') return vals; | ||
|
||
var alignment = trace[axLetter + 'periodalignment']; | ||
if(!alignment) return vals; | ||
|
||
var period = trace[axLetter + 'period']; | ||
var mPeriod, dPeriod; | ||
if(isNumeric(period)) { | ||
dPeriod = +period; | ||
dPeriod /= ONEDAY; // convert milliseconds to days | ||
dPeriod = Math.round(dPeriod); | ||
if(dPeriod <= 0) return vals; | ||
} else if(typeof period === 'string' && period.charAt(0) === 'M') { | ||
var n = +(period.substring(1)); | ||
if(n > 0 && Math.round(n) === n) { | ||
mPeriod = n; | ||
} else return vals; | ||
} | ||
|
||
var calendar = ax.calendar; | ||
|
||
var isStart = 'start' === alignment; | ||
// var isMiddle = 'middle' === alignment; | ||
var isEnd = 'end' === alignment; | ||
|
||
var period0 = trace[axLetter + 'period0']; | ||
var base = dateTime2ms(period0, calendar) || 0; | ||
|
||
var newVals = []; | ||
var len = vals.length; | ||
for(var i = 0; i < len; i++) { | ||
var v = vals[i] - base; | ||
|
||
var dateStr = ms2DateTime(v, 0, calendar); | ||
var d = new Date(dateStr); | ||
var year = d.getUTCFullYear(); | ||
var month = d.getUTCMonth(); | ||
var day = d.getUTCDate(); | ||
|
||
var startTime, endTime; | ||
if(dPeriod) { | ||
startTime = Date.UTC(year, month, day); | ||
endTime = startTime + dPeriod * ONEDAY; | ||
} | ||
|
||
if(mPeriod) { | ||
var nYears = Math.floor(mPeriod / 12); | ||
var nMonths = mPeriod % 12; | ||
|
||
if(nMonths) { | ||
startTime = Date.UTC(year, nYears ? month : roundMonth(month, nMonths)); | ||
endTime = incrementMonth(startTime, mPeriod, calendar); | ||
} else { | ||
startTime = Date.UTC(year, 0); | ||
endTime = Date.UTC(year + nYears, 0); | ||
} | ||
} | ||
|
||
var newD = new Date( | ||
isStart ? startTime : | ||
isEnd ? endTime : | ||
(startTime + endTime) / 2 | ||
); | ||
|
||
newVals[i] = newD.getTime() + base; | ||
} | ||
return newVals; | ||
}; | ||
|
||
var monthSteps = [2, 3, 4, 6]; | ||
|
||
function roundMonth(month, step) { | ||
return (monthSteps.indexOf(step) === -1) ? month : Math.floor(month / step) * step; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this approach is still going to have problems with time zones - specifically, I think the day will be wrong this way if you're in the eastern hemisphere.
There would be ways around this, but we've already solved these problems and all sorts of other edge cases with functions in
lib/dates
so you never directly needDate
objects again and all your processing is with millisecond numbers. What I was thinking of in #5074 (comment) was something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review.
Revised in 83b22a0.