Skip to content

Commit 4f54bcd

Browse files
authored
Merge pull request plotly#5762 from plotly/half-year-format-directive
Add half-year directive for formatting dates and improve descriptions to include extra date formatting options
2 parents 642f529 + 7808217 commit 4f54bcd

31 files changed

+130
-123
lines changed

src/lib/dates.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,19 @@ exports.cleanDate = function(v, dflt, calendar) {
370370
*/
371371

372372
/*
373-
* modDateFormat: Support world calendars, and add one item to
373+
* modDateFormat: Support world calendars, and add two items to
374374
* d3's vocabulary:
375375
* %{n}f where n is the max number of digits of fractional seconds
376+
* %h formats: half of the year as a decimal number [1,2]
376377
*/
377378
var fracMatch = /%\d?f/g;
379+
var halfYearMatch = /%h/g;
380+
var quarterToHalfYear = {
381+
'1': '1',
382+
'2': '1',
383+
'3': '2',
384+
'4': '2',
385+
};
378386
function modDateFormat(fmt, x, formatter, calendar) {
379387
fmt = fmt.replace(fracMatch, function(match) {
380388
var digits = Math.min(+(match.charAt(1)) || 6, 6);
@@ -386,6 +394,10 @@ function modDateFormat(fmt, x, formatter, calendar) {
386394

387395
var d = new Date(Math.floor(x + 0.05));
388396

397+
fmt = fmt.replace(halfYearMatch, function() {
398+
return quarterToHalfYear[formatter('%q')(d)];
399+
});
400+
389401
if(isWorldCalendar(calendar)) {
390402
try {
391403
fmt = Registry.getComponentMethod('calendars', 'worldCalFmt')(fmt, x, calendar);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
var docs = require('../../constants/docs');
4+
var FORMAT_LINK = docs.FORMAT_LINK;
5+
var DATE_FORMAT_LINK = docs.DATE_FORMAT_LINK;
6+
7+
function axisHoverFormat(x, noDates) {
8+
return {
9+
valType: 'string',
10+
dflt: '',
11+
editType: 'none',
12+
description: (
13+
noDates ? descriptionOnlyNumbers : descriptionWithDates
14+
)('hover text', x) + [
15+
'By default the values are formatted using ' + (
16+
noDates ?
17+
'generic number format' :
18+
('`' + x + 'axis.hoverformat`')
19+
) + '.',
20+
].join(' ')
21+
};
22+
}
23+
24+
function descriptionOnlyNumbers(label, x) {
25+
return [
26+
'Sets the ' + label + ' formatting rule' + (x ? 'for `' + x + '` ' : ''),
27+
'using d3 formatting mini-languages',
28+
'which are very similar to those in Python. For numbers, see: ' + FORMAT_LINK + '.'
29+
].join(' ');
30+
}
31+
32+
function descriptionWithDates(label, x) {
33+
return descriptionOnlyNumbers(label, x) + [
34+
' And for dates see: ' + DATE_FORMAT_LINK + '.',
35+
'We add two items to d3\'s date formatter:',
36+
'*%h* for half of the year as a decimal number as well as',
37+
'*%{n}f* for fractional seconds',
38+
'with n digits. For example, *2016-10-13 09:15:23.456* with tickformat',
39+
'*%H~%M~%S.%2f* would display *09~15~23.46*'
40+
].join(' ');
41+
}
42+
43+
module.exports = {
44+
axisHoverFormat: axisHoverFormat,
45+
descriptionOnlyNumbers: descriptionOnlyNumbers,
46+
descriptionWithDates: descriptionWithDates
47+
};

src/plots/cartesian/layout_attributes.js

+3-24
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ var colorAttrs = require('../../components/color/attributes');
55
var dash = require('../../components/drawing/attributes').dash;
66
var extendFlat = require('../../lib/extend').extendFlat;
77
var templatedArray = require('../../plot_api/plot_template').templatedArray;
8-
9-
var docs = require('../../constants/docs');
10-
var FORMAT_LINK = docs.FORMAT_LINK;
11-
var DATE_FORMAT_LINK = docs.DATE_FORMAT_LINK;
8+
var descriptionWithDates = require('../../plots/cartesian/axis_format_attributes').descriptionWithDates;
129

1310
var ONEDAY = require('../../constants/numerical').ONEDAY;
1411
var constants = require('./constants');
@@ -702,16 +699,7 @@ module.exports = {
702699
valType: 'string',
703700
dflt: '',
704701
editType: 'ticks',
705-
description: [
706-
'Sets the tick label formatting rule using d3 formatting mini-languages',
707-
'which are very similar to those in Python. For numbers, see:',
708-
FORMAT_LINK,
709-
'And for dates see:',
710-
DATE_FORMAT_LINK,
711-
'We add one item to d3\'s date formatter: *%{n}f* for fractional seconds',
712-
'with n digits. For example, *2016-10-13 09:15:23.456* with tickformat',
713-
'*%H~%M~%S.%2f* would display *09~15~23.46*'
714-
].join(' ')
702+
description: descriptionWithDates('tick label')
715703
},
716704
tickformatstops: templatedArray('tickformatstop', {
717705
enabled: {
@@ -750,16 +738,7 @@ module.exports = {
750738
valType: 'string',
751739
dflt: '',
752740
editType: 'none',
753-
description: [
754-
'Sets the hover text formatting rule using d3 formatting mini-languages',
755-
'which are very similar to those in Python. For numbers, see:',
756-
FORMAT_LINK,
757-
'And for dates see:',
758-
DATE_FORMAT_LINK,
759-
'We add one item to d3\'s date formatter: *%{n}f* for fractional seconds',
760-
'with n digits. For example, *2016-10-13 09:15:23.456* with tickformat',
761-
'*%H~%M~%S.%2f* would display *09~15~23.46*'
762-
].join(' ')
741+
description: descriptionWithDates('hover text')
763742
},
764743
// lines and grids
765744
showline: {

src/plots/hoverformat_attributes.js

-27
This file was deleted.

src/traces/bar/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var scatterAttrs = require('../scatter/attributes');
4-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
4+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
55
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
66
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
77
var colorScaleAttrs = require('../../components/colorscale/attributes');

src/traces/box/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var scatterAttrs = require('../scatter/attributes');
44
var barAttrs = require('../bar/attributes');
55
var colorAttrs = require('../../components/color/attributes');
6-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
6+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
77
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
88
var extendFlat = require('../../lib/extend').extendFlat;
99

src/traces/candlestick/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var extendFlat = require('../../lib').extendFlat;
4-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
4+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
55
var OHLCattrs = require('../ohlc/attributes');
66
var boxAttrs = require('../box/attributes');
77

src/traces/carpet/axis_attributes.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
var fontAttrs = require('../../plots/font_attributes');
44
var colorAttrs = require('../../components/color/attributes');
55
var axesAttrs = require('../../plots/cartesian/layout_attributes');
6+
var descriptionWithDates = require('../../plots/cartesian/axis_format_attributes').descriptionWithDates;
67
var overrideAll = require('../../plot_api/edit_types').overrideAll;
78

8-
var docs = require('../../constants/docs');
9-
var FORMAT_LINK = docs.FORMAT_LINK;
10-
var DATE_FORMAT_LINK = docs.DATE_FORMAT_LINK;
11-
129
module.exports = {
1310
color: {
1411
valType: 'color',
@@ -279,16 +276,7 @@ module.exports = {
279276
valType: 'string',
280277
dflt: '',
281278
editType: 'calc',
282-
description: [
283-
'Sets the tick label formatting rule using d3 formatting mini-languages',
284-
'which are very similar to those in Python. For numbers, see:',
285-
FORMAT_LINK,
286-
'And for dates see:',
287-
DATE_FORMAT_LINK,
288-
'We add one item to d3\'s date formatter: *%{n}f* for fractional seconds',
289-
'with n digits. For example, *2016-10-13 09:15:23.456* with tickformat',
290-
'*%H~%M~%S.%2f* would display *09~15~23.46*'
291-
].join(' ')
279+
description: descriptionWithDates('tick label')
292280
},
293281
tickformatstops: overrideAll(axesAttrs.tickformatstops, 'calc', 'from-root'),
294282
categoryorder: {

src/traces/cone/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var colorScaleAttrs = require('../../components/colorscale/attributes');
4-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
4+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
55
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
66
var mesh3dAttrs = require('../mesh3d/attributes');
77
var baseAttrs = require('../../plots/attributes');

src/traces/contour/attributes.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
var heatmapAttrs = require('../heatmap/attributes');
44
var scatterAttrs = require('../scatter/attributes');
5-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
5+
var axisFormat = require('../../plots/cartesian/axis_format_attributes');
6+
var axisHoverFormat = axisFormat.axisHoverFormat;
7+
var descriptionOnlyNumbers = axisFormat.descriptionOnlyNumbers;
68
var colorScaleAttrs = require('../../components/colorscale/attributes');
79
var dash = require('../../components/drawing/attributes').dash;
810
var fontAttrs = require('../../plots/font_attributes');
@@ -12,7 +14,6 @@ var filterOps = require('../../constants/filter_ops');
1214
var COMPARISON_OPS2 = filterOps.COMPARISON_OPS2;
1315
var INTERVAL_OPS = filterOps.INTERVAL_OPS;
1416

15-
var FORMAT_LINK = require('../../constants/docs').FORMAT_LINK;
1617

1718
var scatterLineAttrs = scatterAttrs.line;
1819

@@ -181,11 +182,7 @@ module.exports = extendFlat({
181182
valType: 'string',
182183
dflt: '',
183184
editType: 'plot',
184-
description: [
185-
'Sets the contour label formatting rule using d3 formatting',
186-
'mini-language which is very similar to Python, see:',
187-
FORMAT_LINK
188-
].join(' ')
185+
description: descriptionOnlyNumbers('contour label')
189186
},
190187
operation: {
191188
valType: 'enumerated',

src/traces/funnel/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var barAttrs = require('../bar/attributes');
44
var lineAttrs = require('../scatter/attributes').line;
55
var baseAttrs = require('../../plots/attributes');
6-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
6+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
77
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
88
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
99
var constants = require('./constants');

src/traces/heatmap/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var scatterAttrs = require('../scatter/attributes');
44
var baseAttrs = require('../../plots/attributes');
5-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
5+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
66
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
77
var colorScaleAttrs = require('../../components/colorscale/attributes');
88

src/traces/histogram/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var barAttrs = require('../bar/attributes');
4-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
4+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
55
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
66
var makeBinAttrs = require('./bin_attributes');
77
var constants = require('./constants');

src/traces/histogram2d/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var histogramAttrs = require('../histogram/attributes');
44
var makeBinAttrs = require('../histogram/bin_attributes');
55
var heatmapAttrs = require('../heatmap/attributes');
66
var baseAttrs = require('../../plots/attributes');
7-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
7+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
88
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
99
var colorScaleAttrs = require('../../components/colorscale/attributes');
1010

src/traces/histogram2dcontour/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var histogram2dAttrs = require('../histogram2d/attributes');
44
var contourAttrs = require('../contour/attributes');
55
var colorScaleAttrs = require('../../components/colorscale/attributes');
6-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
6+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
77

88
var extendFlat = require('../../lib/extend').extendFlat;
99

src/traces/indicator/attributes.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var domainAttrs = require('../../plots/domain').attributes;
99
var axesAttrs = require('../../plots/cartesian/layout_attributes');
1010
var templatedArray = require('../../plot_api/plot_template').templatedArray;
1111
var delta = require('../../constants/delta.js');
12-
var FORMAT_LINK = require('../../constants/docs').FORMAT_LINK;
12+
var descriptionOnlyNumbers = require('../../plots/cartesian/axis_format_attributes').descriptionOnlyNumbers;
1313

1414
var textFontAttrs = fontAttrs({
1515
editType: 'plot',
@@ -147,11 +147,7 @@ module.exports = {
147147
valType: 'string',
148148
dflt: '',
149149
editType: 'plot',
150-
description: [
151-
'Sets the value formatting rule using d3 formatting mini-language',
152-
'which is similar to those of Python. See',
153-
FORMAT_LINK
154-
].join(' ')
150+
description: descriptionOnlyNumbers('value')
155151
},
156152
font: extendFlat({}, textFontAttrs, {
157153
description: [
@@ -205,11 +201,7 @@ module.exports = {
205201
valueformat: {
206202
valType: 'string',
207203
editType: 'plot',
208-
description: [
209-
'Sets the value formatting rule using d3 formatting mini-language',
210-
'which is similar to those of Python. See',
211-
FORMAT_LINK
212-
].join(' ')
204+
description: descriptionOnlyNumbers('value')
213205
},
214206
increasing: {
215207
symbol: {

src/traces/isosurface/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var colorScaleAttrs = require('../../components/colorscale/attributes');
4-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
4+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
55
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
66
var meshAttrs = require('../mesh3d/attributes');
77
var baseAttrs = require('../../plots/attributes');

src/traces/mesh3d/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var colorScaleAttrs = require('../../components/colorscale/attributes');
4-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
4+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
55
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
66
var surfaceAttrs = require('../surface/attributes');
77
var baseAttrs = require('../../plots/attributes');

src/traces/ohlc/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var extendFlat = require('../../lib').extendFlat;
44
var scatterAttrs = require('../scatter/attributes');
5-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
5+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
66
var dash = require('../../components/drawing/attributes').dash;
77
var fxAttrs = require('../../components/fx/attributes');
88
var delta = require('../../constants/delta.js');

src/traces/sankey/attributes.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ var domainAttrs = require('../../plots/domain').attributes;
88
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
99
var colorAttributes = require('../../components/colorscale/attributes');
1010
var templatedArray = require('../../plot_api/plot_template').templatedArray;
11+
var descriptionOnlyNumbers = require('../../plots/cartesian/axis_format_attributes').descriptionOnlyNumbers;
1112

1213
var extendFlat = require('../../lib/extend').extendFlat;
1314
var overrideAll = require('../../plot_api/edit_types').overrideAll;
1415

15-
var FORMAT_LINK = require('../../constants/docs').FORMAT_LINK;
16-
1716
var attrs = module.exports = overrideAll({
1817
hoverinfo: extendFlat({}, baseAttrs.hoverinfo, {
1918
flags: [],
@@ -39,11 +38,7 @@ var attrs = module.exports = overrideAll({
3938
valueformat: {
4039
valType: 'string',
4140
dflt: '.3s',
42-
description: [
43-
'Sets the value formatting rule using d3 formatting mini-language',
44-
'which is similar to those of Python. See',
45-
FORMAT_LINK
46-
].join(' ')
41+
description: descriptionOnlyNumbers('value')
4742
},
4843

4944
valuesuffix: {

src/traces/scatter/attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var axisHoverFormat = require('../../plots/hoverformat_attributes');
3+
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
44
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
55
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
66
var colorScaleAttrs = require('../../components/colorscale/attributes');

0 commit comments

Comments
 (0)