Skip to content

Commit 6c949ef

Browse files
authored
Merge pull request #4900 from plotly/bar-milliseconds-from-base
Fix bar lengths in milliseconds from base
2 parents f90690f + bb5bec3 commit 6c949ef

File tree

8 files changed

+180
-6
lines changed

8 files changed

+180
-6
lines changed

src/plots/cartesian/set_convert.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = function setConvert(ax, fullLayout) {
9090
* - inserts a dummy arg so calendar is the 3rd arg (see notes below).
9191
* - defaults to ax.calendar
9292
*/
93-
function dt2ms(v, _, calendar) {
93+
function dt2ms(v, _, calendar, msUTC) {
9494
// NOTE: Changed this behavior: previously we took any numeric value
9595
// to be a ms, even if it was a string that could be a bare year.
9696
// Now we convert it as a date if at all possible, and only try
@@ -99,6 +99,13 @@ module.exports = function setConvert(ax, fullLayout) {
9999
if(ms === BADNUM) {
100100
if(isNumeric(v)) {
101101
v = +v;
102+
if(msUTC) {
103+
// For now it is only used
104+
// to fix bar length in milliseconds.
105+
// It could be applied in other places in v2
106+
return v;
107+
}
108+
102109
// keep track of tenths of ms, that `new Date` will drop
103110
// same logic as in Lib.ms2DateTime
104111
var msecTenths = Math.floor(Lib.mod(v + 0.05, 1) * 10);
@@ -791,7 +798,7 @@ module.exports = function setConvert(ax, fullLayout) {
791798
// the first letter of ax._id?)
792799
// in case the expected data isn't there, make a list of
793800
// integers based on the opposite data
794-
ax.makeCalcdata = function(trace, axLetter) {
801+
ax.makeCalcdata = function(trace, axLetter, msUTC) {
795802
var arrayIn, arrayOut, i, len;
796803

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

816823
arrayOut = new Array(len);
817824
for(i = 0; i < len; i++) {
818-
arrayOut[i] = ax.d2c(arrayIn[i], 0, cal);
825+
arrayOut[i] = ax.d2c(arrayIn[i], 0, cal, msUTC);
819826
}
820827
} else {
821-
var v0 = ((axLetter + '0') in trace) ? ax.d2c(trace[axLetter + '0'], 0, cal) : 0;
828+
var v0 = ((axLetter + '0') in trace) ? ax.d2c(trace[axLetter + '0'], 0, cal, false) : 0;
822829
var dv = (trace['d' + axLetter]) ? Number(trace['d' + axLetter]) : 1;
823830

824831
// the opposing data, for size if we have x and dx etc

src/traces/bar/calc.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ module.exports = function calc(gd, trace) {
1919
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
2020
var size, pos;
2121

22+
var msUTC = !!(trace.base || trace.base === 0);
23+
2224
if(trace.orientation === 'h') {
23-
size = xa.makeCalcdata(trace, 'x');
25+
size = xa.makeCalcdata(trace, 'x', msUTC);
2426
pos = ya.makeCalcdata(trace, 'y');
2527
} else {
26-
size = ya.makeCalcdata(trace, 'y');
28+
size = ya.makeCalcdata(trace, 'y', msUTC);
2729
pos = xa.makeCalcdata(trace, 'x');
2830
}
2931

16.6 KB
Loading
48.8 KB
Loading
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"data": [
3+
{
4+
"base": [
5+
"2020-02-02 00:00"
6+
],
7+
"orientation": "h",
8+
"type": "bar",
9+
"x": [
10+
86400000
11+
],
12+
"y": [
13+
1
14+
],
15+
"yaxis": "y"
16+
},
17+
{
18+
"base": [
19+
"2020-02-02 00:00"
20+
],
21+
"orientation": "h",
22+
"type": "bar",
23+
"x": [
24+
"1970-01-02 00:00"
25+
],
26+
"y": [
27+
1
28+
],
29+
"yaxis": "y2"
30+
}
31+
],
32+
"layout": {
33+
"width": 800,
34+
"height": 400,
35+
"xaxis": {
36+
"type": "date"
37+
},
38+
"yaxis": {
39+
"domain": [
40+
0,
41+
0.45
42+
]
43+
},
44+
"yaxis2": {
45+
"domain": [
46+
0.55,
47+
1
48+
]
49+
}
50+
}
51+
}

test/image/mocks/bar_gantt-chart.json

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"data": [
3+
{
4+
"base": [
5+
"2020-03-01",
6+
"2020-03-05",
7+
"2020-03-05",
8+
"2020-03-01",
9+
"2020-03-06",
10+
"2020-03-01",
11+
"2020-03-07",
12+
"2020-03-08",
13+
"2020-03-09"
14+
],
15+
"orientation": "h",
16+
"type": "bar",
17+
"x": [
18+
345600000,
19+
172800000,
20+
259200000,
21+
432000000,
22+
86400000,
23+
518400000,
24+
302400000,
25+
86400000,
26+
172800000
27+
],
28+
"marker": {
29+
"colorscale": "Portland",
30+
"color": [
31+
1,
32+
0,
33+
1,
34+
0,
35+
0,
36+
0.9,
37+
0.3,
38+
1,
39+
1
40+
]
41+
},
42+
"y": [
43+
"Activity A",
44+
"Activity B",
45+
"Activity C",
46+
"Activity D",
47+
"Activity E",
48+
"Activity F",
49+
"Activity X",
50+
"Activity Y",
51+
"Activity Z"
52+
],
53+
"text": [
54+
"4 days",
55+
"2 days",
56+
"3 days",
57+
"5 days",
58+
"1 day",
59+
"6 days",
60+
"3½ days",
61+
"1 day",
62+
"2 days"
63+
],
64+
"textfont": { "size": 16 },
65+
"textposition": "inside",
66+
"insidetextanchor": "middle",
67+
"hovertemplate": "%{y}<br>start: %{base}<br>duration: %{text}<br>end: %{x}"
68+
}
69+
],
70+
"layout": {
71+
"width": 1000,
72+
"height": 500,
73+
"title": {
74+
"text": "Gantt chart using Plotly.js bar"
75+
},
76+
"xaxis": {
77+
"gridwidth": 5,
78+
"tickwidth": 5,
79+
"ticklen": 10,
80+
"type": "date"
81+
},
82+
"yaxis": {
83+
"tickfont": {
84+
"family": "Times New Roman",
85+
"size": 16
86+
},
87+
"autorange": "reversed"
88+
}
89+
}
90+
}

test/jasmine/tests/bar_test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,6 +2600,26 @@ describe('bar hover', function() {
26002600
});
26012601
});
26022602
});
2603+
2604+
describe('gantt chart using milliseconds from base', function() {
2605+
beforeAll(function(done) {
2606+
gd = createGraphDiv();
2607+
2608+
var mock = Lib.extendDeep({}, require('@mocks/bar-with-milliseconds.json'));
2609+
2610+
Plotly.newPlot(gd, mock.data, mock.layout)
2611+
.catch(failTest)
2612+
.then(done);
2613+
});
2614+
2615+
it('should display the correct bar length passed in milliseconds from base', function() {
2616+
var out = _hover(gd, 0.5, 0.75, 'y');
2617+
2618+
var xEnd = out.style[2];
2619+
expect(xEnd).not.toBe(1580670000000);
2620+
expect(xEnd).toBe(1580688000000);
2621+
});
2622+
});
26032623
});
26042624

26052625
describe('Text templates on bar traces:', function() {

test/jasmine/tests/mock_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ var list = [
111111
'bar_display_height_zero_no_line_width',
112112
'bar_display_height_zero_only_line_width',
113113
'bar_errorbars_inherit_color',
114+
'bar_gantt-chart',
114115
'bar_group_percent',
115116
'bar_hide_nulls',
116117
'bar_line',
@@ -137,6 +138,7 @@ var list = [
137138
'bar-like_traces_tozero',
138139
'bar-marker-line-colorscales',
139140
'bar-offsetgroups',
141+
'bar-with-milliseconds',
140142
'basic_area',
141143
'basic_bar',
142144
'basic_error_bar',
@@ -1150,6 +1152,7 @@ figs['bar_display_height_zero'] = require('@mocks/bar_display_height_zero');
11501152
figs['bar_display_height_zero_no_line_width'] = require('@mocks/bar_display_height_zero_no_line_width');
11511153
figs['bar_display_height_zero_only_line_width'] = require('@mocks/bar_display_height_zero_only_line_width');
11521154
figs['bar_errorbars_inherit_color'] = require('@mocks/bar_errorbars_inherit_color');
1155+
figs['bar_gantt-chart'] = require('@mocks/bar_gantt-chart');
11531156
figs['bar_group_percent'] = require('@mocks/bar_group_percent');
11541157
figs['bar_hide_nulls'] = require('@mocks/bar_hide_nulls');
11551158
figs['bar_line'] = require('@mocks/bar_line');
@@ -1176,6 +1179,7 @@ figs['bar-like_traces_no-tozero_stack'] = require('@mocks/bar-like_traces_no-toz
11761179
figs['bar-like_traces_tozero'] = require('@mocks/bar-like_traces_tozero');
11771180
figs['bar-marker-line-colorscales'] = require('@mocks/bar-marker-line-colorscales');
11781181
figs['bar-offsetgroups'] = require('@mocks/bar-offsetgroups');
1182+
figs['bar-with-milliseconds'] = require('@mocks/bar-with-milliseconds');
11791183
figs['basic_area'] = require('@mocks/basic_area');
11801184
figs['basic_bar'] = require('@mocks/basic_bar');
11811185
figs['basic_error_bar'] = require('@mocks/basic_error_bar');

0 commit comments

Comments
 (0)