-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
texttemplate with date formatting #4071
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
Changes from 35 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
8deaed1
texttemplate: initial implementation
antoinerg ce3c9cc
move texttemplate_attributes.js from fx to plots
antoinerg ee0a309
do not export `templateFormatString`
antoinerg 9330290
texttemplate: test that textinfo is not coerced when it's defined
antoinerg cf826dd
texttemplate: update description
antoinerg c3e9961
set texttemplate dflt back to ''
antoinerg e0bde45
texttemplate: add customdata variable and :lock: down with test
antoinerg 902d2d3
texttemplate: add support for scatter(carpet|geo|polar|ternary)
antoinerg 0b516f7
texttemplate: use result of appendArrayPointValue as available variables
antoinerg 7d21680
texttemplate: fix scattergeo variable names
antoinerg 7fa4dc5
texttemplate: add support for scattergl
antoinerg aeb6d1f
texttemplate: add support for scatterpolargl
antoinerg f9062e4
texttemplate: add support for scatter3d
antoinerg 3ad30ab
Merge branch 'master' into texttemplate
antoinerg 2d50269
Merge branch 'texttemplate-gl' into texttemplate
antoinerg 05f4722
check_texttemplate: put @gl tests in its own `it` block
antoinerg 6729286
split gl2d and gl3d in separate mocks
antoinerg cbbcfcb
texttemplate: use d3locale in GL traces, fix style
antoinerg 11ea019
Merge branch 'texttemplate-split' into texttemplate
antoinerg 32498f8
remove unused baseline `texttemplate_2`
antoinerg 7bb3379
texttemplate: text in legends should not changed
antoinerg 9677929
texttemplate: fix GL traces with txt array longer than data
antoinerg b9188a9
Merge branch 'master' into texttemplate
antoinerg decec45
scattergl: :lock: down handling of texttemplate with fewer elements t…
antoinerg 2f6bf8a
scattergl: make sure text(template) is not mutated
antoinerg e7c034a
texttemplate: test that trace.meta is passed to template
antoinerg 4b1face
texttemplate: pass trace._meta to gl2d traces
antoinerg 2528aa6
Merge branch 'master' into texttemplate
antoinerg e381c25
Merge branch 'texttemplate-meta' into texttemplate
antoinerg 888cf68
texttemplate: DRY code for gl(2|3)d traces, pass meta to scatter3d
antoinerg 7d3fabb
texttemplate: add support for scattermapbox
antoinerg b2df623
funnelarea: fix description of variables in (hover|text)template
antoinerg 8c339df
funnel(area): fix description of variables for (hover|text)template
antoinerg c6370d9
support formatting date in (hover|text)template
antoinerg 836d5a2
templateFormatString: use d3locale.timeFormat if available fot date fmt
antoinerg 3dda912
texttemplate for scattermapbox: fix editType, improve style and mock
antoinerg 354b70f
fix baseline "mapbox_texttemplate"
antoinerg 4ef8392
merge (text|hover)templateAttrs() into a single file
antoinerg e700fc2
texttemplate: fix style
antoinerg 6780a3d
texttemplate: do not coerce texttemplate when no text is displayed
antoinerg db87e62
(hover|text)template: pie/funnel/waterfall fix attribute definition
antoinerg daa2c7e
templateFormatString: do not error out when lookup objects are undefined
antoinerg c7dd845
don't coerce texttemplate when no text - fix for scatterternary
etpinard 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
/** | ||
* Copyright 2012-2019, 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 FORMAT_LINK = require('../constants/docs').FORMAT_LINK; | ||
var DATE_FORMAT_LINK = require('../constants/docs').DATE_FORMAT_LINK; | ||
|
||
module.exports = function(opts, extra) { | ||
opts = opts || {}; | ||
extra = extra || {}; | ||
|
||
var descPart = extra.description ? ' ' + extra.description : ''; | ||
var keys = extra.keys || []; | ||
if(keys.length > 0) { | ||
var quotedKeys = []; | ||
for(var i = 0; i < keys.length; i++) { | ||
quotedKeys[i] = '`' + keys[i] + '`'; | ||
} | ||
descPart = descPart + 'Finally, the template string has access to '; | ||
if(keys.length === 1) { | ||
descPart = 'variable ' + quotedKeys[0]; | ||
} else { | ||
descPart = 'variables ' + quotedKeys.slice(0, -1).join(', ') + ' and ' + quotedKeys.slice(-1) + '.'; | ||
} | ||
} | ||
|
||
var texttemplate = { | ||
valType: 'string', | ||
role: 'info', | ||
dflt: '', | ||
editType: opts.editType || 'calc', | ||
description: [ | ||
'Template string used for rendering the information text that appear on points.', | ||
'Note that this will override `textinfo`.', | ||
'Variables are inserted using %{variable}, for example "y: %{y}".', | ||
'If the `text` attribute is set, it will be available via "%{text}"', | ||
'Numbers are formatted using d3-format\'s syntax %{variable:d3-format}, for example "Price: %{y:$.2f}".', | ||
FORMAT_LINK, | ||
'for details on the formatting syntax.', | ||
'Dates are formatted using d3-time-format\'s syntax %{variable|d3-time-format}, for example "Day: %{2019-01-01|%A}".', | ||
DATE_FORMAT_LINK, | ||
'for details on the date formatting syntax.', | ||
'Every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available.', | ||
descPart | ||
].join(' ') | ||
}; | ||
|
||
if(opts.arrayOk !== false) { | ||
texttemplate.arrayOk = true; | ||
} | ||
return texttemplate; | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -26,6 +26,8 @@ var attributes = require('./attributes'); | |
var attributeText = attributes.text; | ||
var attributeTextPosition = attributes.textposition; | ||
|
||
var appendArrayPointValue = require('../../components/fx/helpers').appendArrayPointValue; | ||
|
||
// padding in pixels around text | ||
var TEXTPAD = 3; | ||
|
||
|
@@ -226,7 +228,7 @@ function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, opts) { | |
var trace = calcTrace[0].trace; | ||
var isHorizontal = (trace.orientation === 'h'); | ||
|
||
var text = getText(calcTrace, i, xa, ya); | ||
var text = getText(fullLayout, calcTrace, i, xa, ya); | ||
textPosition = getTextPosition(trace, i); | ||
|
||
// compute text position | ||
|
@@ -537,14 +539,17 @@ function getTransform(opts) { | |
return transformTranslate + transformScale + transformRotate; | ||
} | ||
|
||
function getText(calcTrace, index, xa, ya) { | ||
function getText(fullLayout, calcTrace, index, xa, ya) { | ||
var trace = calcTrace[0].trace; | ||
var texttemplate = trace.texttemplate; | ||
|
||
var value; | ||
if(!trace.textinfo) { | ||
value = helpers.getValue(trace.text, index); | ||
} else { | ||
if(texttemplate) { | ||
value = calcTexttemplate(fullLayout, calcTrace, index, xa, ya); | ||
} else if(trace.textinfo) { | ||
value = calcTextinfo(calcTrace, index, xa, ya); | ||
} else { | ||
value = helpers.getValue(trace.text, index); | ||
} | ||
|
||
return helpers.coerceString(attributeText, value); | ||
|
@@ -555,6 +560,66 @@ function getTextPosition(trace, index) { | |
return helpers.coerceEnumerated(attributeTextPosition, value); | ||
} | ||
|
||
function calcTexttemplate(fullLayout, calcTrace, index, xa, ya) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to merge this routine with |
||
var trace = calcTrace[0].trace; | ||
var texttemplate = Lib.castOption(trace, index, 'texttemplate'); | ||
if(!texttemplate) return ''; | ||
var isHorizontal = (trace.orientation === 'h'); | ||
var isWaterfall = (trace.type === 'waterfall'); | ||
var isFunnel = (trace.type === 'funnel'); | ||
|
||
function formatLabel(u) { | ||
var pAxis = isHorizontal ? ya : xa; | ||
return tickText(pAxis, u, true).text; | ||
} | ||
|
||
function formatNumber(v) { | ||
var sAxis = isHorizontal ? xa : ya; | ||
return tickText(sAxis, +v, true).text; | ||
} | ||
|
||
var cdi = calcTrace[index]; | ||
var obj = {}; | ||
|
||
obj.label = cdi.p; | ||
obj.labelLabel = formatLabel(cdi.p); | ||
etpinard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var tx = Lib.castOption(trace, cdi.i, 'text'); | ||
if(tx === 0 || tx) obj.text = tx; | ||
|
||
obj.value = cdi.s; | ||
obj.valueLabel = formatNumber(cdi.s); | ||
|
||
var pt = {}; | ||
appendArrayPointValue(pt, trace, cdi.i); | ||
|
||
if(isWaterfall) { | ||
obj.delta = +cdi.rawS || cdi.s; | ||
obj.deltaLabel = formatNumber(obj.delta); | ||
obj.final = cdi.v; | ||
obj.finalLabel = formatNumber(obj.final); | ||
obj.initial = obj.final - obj.delta; | ||
obj.initialLabel = formatNumber(obj.initial); | ||
} | ||
|
||
if(isFunnel) { | ||
obj.value = cdi.s; | ||
obj.valueLabel = formatNumber(obj.value); | ||
|
||
obj.percentInitial = cdi.begR; | ||
obj.percentInitialLabel = Lib.formatPercent(cdi.begR); | ||
obj.percentPrevious = cdi.difR; | ||
obj.percentPreviousLabel = Lib.formatPercent(cdi.difR); | ||
obj.percentTotal = cdi.sumR; | ||
obj.percenTotalLabel = Lib.formatPercent(cdi.sumR); | ||
} | ||
|
||
var customdata = Lib.castOption(trace, cdi.i, 'customdata'); | ||
if(customdata) obj.customdata = customdata; | ||
return Lib.texttemplateString(texttemplate, obj, fullLayout._d3locale, pt, obj, trace._meta || {}); | ||
} | ||
|
||
// TODO: calcTextinfo should build a texttemplate pass it to calcTexttemplate() | ||
etpinard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function calcTextinfo(calcTrace, index, xa, ya) { | ||
var trace = calcTrace[0].trace; | ||
var isHorizontal = (trace.orientation === 'h'); | ||
|
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.