Skip to content

Commit bf52cd7

Browse files
committed
add support for histogram* traces on multicategory axes
1 parent b792b8d commit bf52cd7

File tree

6 files changed

+133
-8
lines changed

6 files changed

+133
-8
lines changed

src/plots/cartesian/axes.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ axes.autoBin = function(data, ax, nbins, is2d, calendar, size) {
278278
var dataMin = Lib.aggNums(Math.min, null, data);
279279
var dataMax = Lib.aggNums(Math.max, null, data);
280280

281-
// TODO multicategory, if we allow multicategory histograms
282-
if(ax.type === 'category') {
281+
if(ax.type === 'category' || ax.type === 'multicategory') {
283282
return {
284283
start: dataMin - 0.5,
285284
end: dataMax + 0.5,

src/traces/histogram/calc.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ function calcAllAutoBins(gd, trace, pa, mainData, _overlayEdgeCase) {
270270

271271
// Edge case: single-valued histogram overlaying others
272272
// Use them all together to calculate the bin size for the single-valued one
273-
if(isOverlay && newBinSpec._dataSpan === 0 && pa.type !== 'category') {
273+
if(isOverlay && newBinSpec._dataSpan === 0 &&
274+
pa.type !== 'category' && pa.type !== 'multicategory') {
274275
// Several single-valued histograms! Stop infinite recursion,
275276
// just return an extra flag that tells handleSingleValueOverlays
276277
// to sort out this trace too
@@ -327,7 +328,7 @@ function calcAllAutoBins(gd, trace, pa, mainData, _overlayEdgeCase) {
327328
Lib.aggNums(Math.min, null, pos0);
328329

329330
var dummyAx = {
330-
type: pa.type === 'category' ? 'linear' : pa.type,
331+
type: (pa.type === 'category' || pa.type === 'multicategory') ? 'linear' : pa.type,
331332
r2l: pa.r2l,
332333
dtick: binOpts.size,
333334
tick0: mainStart,

src/traces/histogram/defaults.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3636
var sampleLetter = orientation === 'v' ? 'x' : 'y';
3737
var aggLetter = orientation === 'v' ? 'y' : 'x';
3838

39-
var len = (x && y) ? Math.min(x.length && y.length) : (traceOut[sampleLetter] || []).length;
39+
var len = (x && y) ?
40+
Math.min(Lib.minRowLength(x) && Lib.minRowLength(y)) :
41+
Lib.minRowLength(traceOut[sampleLetter] || []);
4042

4143
if(!len) {
4244
traceOut.visible = false;

src/traces/histogram2d/sample_defaults.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Registry = require('../../registry');
12+
var Lib = require('../../lib');
1313

1414
module.exports = function handleSampleDefaults(traceIn, traceOut, coerce, layout) {
1515
var x = coerce('x');
1616
var y = coerce('y');
17+
var xlen = Lib.minRowLength(x);
18+
var ylen = Lib.minRowLength(y);
1719

1820
// we could try to accept x0 and dx, etc...
1921
// but that's a pretty weird use case.
2022
// for now require both x and y explicitly specified.
21-
if(!(x && x.length && y && y.length)) {
23+
if(!(x && xlen && y && ylen)) {
2224
traceOut.visible = false;
2325
return;
2426
}
2527

26-
traceOut._length = Math.min(x.length, y.length);
28+
traceOut._length = Math.min(xlen, ylen);
2729

2830
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
2931
handleCalendarDefaults(traceIn, traceOut, ['x', 'y'], layout);
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{
2+
"data": [
3+
{
4+
"type": "histogram2d",
5+
"name": "hist2d",
6+
"x": [
7+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018 ],
8+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c" ]
9+
],
10+
"y": [
11+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2018, 2017, 2017 ],
12+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c", "b", "c", "a" ]
13+
],
14+
"colorscale": "Viridis",
15+
"opacity": 0.8,
16+
"colorbar": {
17+
"x": 0.7,
18+
"xanchor": "left",
19+
"y": 0.7,
20+
"yanchor": "bottom",
21+
"len": 0.3,
22+
"title": {
23+
"side": "right",
24+
"text": "hist2d"
25+
}
26+
}
27+
},
28+
{
29+
"mode": "markers",
30+
"x": [
31+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018 ],
32+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c" ]
33+
],
34+
"y": [
35+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2018, 2017, 2017 ],
36+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c", "b", "c", "a" ]
37+
],
38+
"marker": {
39+
"color": "#d3d3d3",
40+
"size": 18,
41+
"opacity": 0.3,
42+
"line": {"color": "black", "width": 1}
43+
}
44+
},
45+
46+
{
47+
"type": "histogram2dcontour",
48+
"name": "hist2dcontour",
49+
"x": [
50+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018 ],
51+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c" ]
52+
],
53+
"y": [
54+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2018, 2017, 2017 ],
55+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c", "b", "c", "a" ]
56+
],
57+
"contours": {
58+
"coloring": "lines"
59+
},
60+
"line": {
61+
"width": 4
62+
},
63+
"colorscale": "Viridis",
64+
"colorbar": {
65+
"x": 1,
66+
"xanchor": "right",
67+
"y": 0.7,
68+
"yanchor": "bottom",
69+
"len": 0.3,
70+
"title": {
71+
"side": "right",
72+
"text": "hist2dcontour"
73+
}
74+
}
75+
},
76+
{
77+
"type": "histogram",
78+
"name": "hist-x",
79+
"x": [
80+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018 ],
81+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c" ]
82+
],
83+
"yaxis": "y2",
84+
"marker": {
85+
"color": "#008080"
86+
}
87+
},
88+
{
89+
"type": "histogram",
90+
"name": "hist-y",
91+
"y": [
92+
[ 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2017, 2018, 2018, 2017, 2017 ],
93+
[ "a", "a", "a", "a", "a", "a", "b", "b", "c", "c", "c", "c", "b", "c", "a" ]
94+
],
95+
"xaxis": "x2",
96+
"marker": {
97+
"color": "#008080"
98+
}
99+
}
100+
],
101+
"layout": {
102+
"title": {
103+
"text": "Multi-category histograms",
104+
"xref": "paper",
105+
"x": 0
106+
},
107+
"xaxis": {
108+
"domain": [0, 0.65]
109+
},
110+
"yaxis": {
111+
"domain": [0, 0.65]
112+
},
113+
"xaxis2": {
114+
"domain": [0.7, 1]
115+
},
116+
"yaxis2": {
117+
"domain": [0.7, 1]
118+
},
119+
"showlegend": false
120+
}
121+
}

0 commit comments

Comments
 (0)