diff --git a/src/components/legend/draw.js b/src/components/legend/draw.js index 4a81fb12f1d..0e8d9f88600 100644 --- a/src/components/legend/draw.js +++ b/src/components/legend/draw.js @@ -568,18 +568,40 @@ function computeLegendDimensions(gd, groups, traces) { else { opts.width = 0; opts.height = 0; + var rowHeight = 0, + maxTraceHeight = 0, + maxTraceWidth = 0, + offsetX = 0; + //calculate largest width for traces and use for width of all legend items traces.each(function(d) { + maxTraceWidth = Math.max(40 + d[0].width, maxTraceWidth); + }); + + traces.each(function(d) { + var legendItem = d[0], - traceWidth = 40 + legendItem.width, + traceWidth = maxTraceWidth, traceGap = opts.tracegroupgap || 5; + if((borderwidth + offsetX + traceGap + traceWidth) > (fullLayout.width - (fullLayout.margin.r + fullLayout.margin.l))) { + offsetX = 0; + rowHeight = rowHeight + maxTraceHeight; + opts.height = opts.height + maxTraceHeight; + //reset for next row + maxTraceHeight = 0; + } + Lib.setTranslate(this, - (borderwidth + opts.width), - (5 + borderwidth + legendItem.height / 2)); + (borderwidth + offsetX), + (5 + borderwidth + legendItem.height / 2) + rowHeight); opts.width += traceGap + traceWidth; opts.height = Math.max(opts.height, legendItem.height); + + //keep track of tallest trace in group + offsetX += traceGap + traceWidth; + maxTraceHeight = Math.max(legendItem.height, maxTraceHeight); }); opts.width += borderwidth * 2; diff --git a/test/image/baselines/legend_horizontal_autowrap.png b/test/image/baselines/legend_horizontal_autowrap.png new file mode 100644 index 00000000000..afd09cf300e Binary files /dev/null and b/test/image/baselines/legend_horizontal_autowrap.png differ diff --git a/test/image/mocks/legend_horizontal_autowrap.json b/test/image/mocks/legend_horizontal_autowrap.json new file mode 100644 index 00000000000..1e682eb2149 --- /dev/null +++ b/test/image/mocks/legend_horizontal_autowrap.json @@ -0,0 +1,542 @@ +{ + "data": [ + { + "x": [1, 2, 3, 4], + "y": [63.69, 62.55, 61.64, 61.39], + "text": ["FiscalYear: 2012
Percent: 63.69
Students: 1235
Race: 1
Race: Average", "FiscalYear: 2013
Percent: 62.55
Students: 1149
Race: 1
Race: Average", "FiscalYear: 2014
Percent: 61.64
Students: 1080
Race: 1
Race: Average", "FiscalYear: 2015
Percent: 61.39
Students: 1035
Race: 1
Race: Average"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Average", + "line": { + "width": 1.8898, + "color": "rgba(0,0,0,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [58.24, 54.93, 42.11, 50.75], + "text": ["FiscalYear: 2012
Percent: 58.24
Students: 53
Race: 2
Race: African American", "FiscalYear: 2013
Percent: 54.93
Students: 39
Race: 2
Race: African American", "FiscalYear: 2014
Percent: 42.11
Students: 32
Race: 2
Race: African American", "FiscalYear: 2015
Percent: 50.75
Students: 34
Race: 2
Race: African American"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "African American", + "line": { + "width": 1.8898, + "color": "rgba(248,118,109,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [51.49, 49.59, 37.12, 31.45], + "text": ["FiscalYear: 2012
Percent: 51.49
Students: 69
Race: 3
Race: Alaska Native", "FiscalYear: 2013
Percent: 49.59
Students: 60
Race: 3
Race: Alaska Native", "FiscalYear: 2014
Percent: 37.12
Students: 49
Race: 3
Race: Alaska Native", "FiscalYear: 2015
Percent: 31.45
Students: 39
Race: 3
Race: Alaska Native"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Alaska Native", + "line": { + "width": 1.8898, + "color": "rgba(216,144,0,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [49.09, 58.54, 53.91, 43.12], + "text": ["FiscalYear: 2012
Percent: 49.09
Students: 54
Race: 4
Race: Alaska Native Multirace", "FiscalYear: 2013
Percent: 58.54
Students: 72
Race: 4
Race: Alaska Native Multirace", "FiscalYear: 2014
Percent: 53.91
Students: 62
Race: 4
Race: Alaska Native Multirace", "FiscalYear: 2015
Percent: 43.12
Students: 47
Race: 4
Race: Alaska Native Multirace"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Alaska Native Multirace", + "line": { + "width": 1.8898, + "color": "rgba(163,165,0,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [70.53, 72.51, 72.28, 78.65], + "text": ["FiscalYear: 2012
Percent: 70.53
Students: 146
Race: 5
Race: Asian", "FiscalYear: 2013
Percent: 72.51
Students: 124
Race: 5
Race: Asian", "FiscalYear: 2014
Percent: 72.28
Students: 133
Race: 5
Race: Asian", "FiscalYear: 2015
Percent: 78.65
Students: 140
Race: 5
Race: Asian"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Asian", + "line": { + "width": 1.8898, + "color": "rgba(57,182,0,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [62.69, 59.09, 63.82, 62], + "text": ["FiscalYear: 2012
Percent: 62.69
Students: 84
Race: 6
Race: Hispanic", "FiscalYear: 2013
Percent: 59.09
Students: 91
Race: 6
Race: Hispanic", "FiscalYear: 2014
Percent: 63.82
Students: 97
Race: 6
Race: Hispanic", "FiscalYear: 2015
Percent: 62
Students: 93
Race: 6
Race: Hispanic"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Hispanic", + "line": { + "width": 1.8898, + "color": "rgba(0,191,125,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [76.27, 71.43, 59.83, 64.34], + "text": ["FiscalYear: 2012
Percent: 76.27
Students: 90
Race: 7
Race: Non Hispanic Multirace", "FiscalYear: 2013
Percent: 71.43
Students: 95
Race: 7
Race: Non Hispanic Multirace", "FiscalYear: 2014
Percent: 59.83
Students: 70
Race: 7
Race: Non Hispanic Multirace", "FiscalYear: 2015
Percent: 64.34
Students: 83
Race: 7
Race: Non Hispanic Multirace"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Non Hispanic Multirace", + "line": { + "width": 1.8898, + "color": "rgba(0,191,196,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [71.15, 81.82, 88.46, 74.29], + "text": ["FiscalYear: 2012
Percent: 71.15
Students: 37
Race: 8
Race: Non Resident Alien", "FiscalYear: 2013
Percent: 81.82
Students: 27
Race: 8
Race: Non Resident Alien", "FiscalYear: 2014
Percent: 88.46
Students: 23
Race: 8
Race: Non Resident Alien", "FiscalYear: 2015
Percent: 74.29
Students: 26
Race: 8
Race: Non Resident Alien"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Non Resident Alien", + "line": { + "width": 1.8898, + "color": "rgba(0,176,246,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [57.89, 57.38, 52.08, 63.83], + "text": ["FiscalYear: 2012
Percent: 57.89
Students: 44
Race: 9
Race: Unknown", "FiscalYear: 2013
Percent: 57.38
Students: 35
Race: 9
Race: Unknown", "FiscalYear: 2014
Percent: 52.08
Students: 25
Race: 9
Race: Unknown", "FiscalYear: 2015
Percent: 63.83
Students: 30
Race: 9
Race: Unknown"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "Unknown", + "line": { + "width": 1.8898, + "color": "rgba(149,144,255,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [65.4, 63.27, 65.78, 64.03], + "text": ["FiscalYear: 2012
Percent: 65.4
Students: 635
Race: 10
Race: White", "FiscalYear: 2013
Percent: 63.27
Students: 577
Race: 10
Race: White", "FiscalYear: 2014
Percent: 65.78
Students: 571
Race: 10
Race: White", "FiscalYear: 2015
Percent: 64.03
Students: 518
Race: 10
Race: White"], + "key": null, + "type": "scatter", + "mode": "lines", + "name": "White", + "line": { + "width": 1.8898, + "color": "rgba(231,107,243,1)", + "dash": "solid" + }, + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [63.69, 62.55, 61.64, 61.39], + "text": ["FiscalYear: 2012
Percent: 63.69
Students: 1235
Race: Average", "FiscalYear: 2013
Percent: 62.55
Students: 1149
Race: Average", "FiscalYear: 2014
Percent: 61.64
Students: 1080
Race: Average", "FiscalYear: 2015
Percent: 61.39
Students: 1035
Race: Average"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(0,0,0,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(0,0,0,1)" + } + }, + "name": "Average", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [58.24, 54.93, 42.11, 50.75], + "text": ["FiscalYear: 2012
Percent: 58.24
Students: 53
Race: African American", "FiscalYear: 2013
Percent: 54.93
Students: 39
Race: African American", "FiscalYear: 2014
Percent: 42.11
Students: 32
Race: African American", "FiscalYear: 2015
Percent: 50.75
Students: 34
Race: African American"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(248,118,109,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(248,118,109,1)" + } + }, + "name": "African American", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [51.49, 49.59, 37.12, 31.45], + "text": ["FiscalYear: 2012
Percent: 51.49
Students: 69
Race: Alaska Native", "FiscalYear: 2013
Percent: 49.59
Students: 60
Race: Alaska Native", "FiscalYear: 2014
Percent: 37.12
Students: 49
Race: Alaska Native", "FiscalYear: 2015
Percent: 31.45
Students: 39
Race: Alaska Native"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(216,144,0,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(216,144,0,1)" + } + }, + "name": "Alaska Native", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [49.09, 58.54, 53.91, 43.12], + "text": ["FiscalYear: 2012
Percent: 49.09
Students: 54
Race: Alaska Native Multirace", "FiscalYear: 2013
Percent: 58.54
Students: 72
Race: Alaska Native Multirace", "FiscalYear: 2014
Percent: 53.91
Students: 62
Race: Alaska Native Multirace", "FiscalYear: 2015
Percent: 43.12
Students: 47
Race: Alaska Native Multirace"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(163,165,0,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(163,165,0,1)" + } + }, + "name": "Alaska Native Multirace", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [70.53, 72.51, 72.28, 78.65], + "text": ["FiscalYear: 2012
Percent: 70.53
Students: 146
Race: Asian", "FiscalYear: 2013
Percent: 72.51
Students: 124
Race: Asian", "FiscalYear: 2014
Percent: 72.28
Students: 133
Race: Asian", "FiscalYear: 2015
Percent: 78.65
Students: 140
Race: Asian"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(57,182,0,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(57,182,0,1)" + } + }, + "name": "Asian", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [62.69, 59.09, 63.82, 62], + "text": ["FiscalYear: 2012
Percent: 62.69
Students: 84
Race: Hispanic", "FiscalYear: 2013
Percent: 59.09
Students: 91
Race: Hispanic", "FiscalYear: 2014
Percent: 63.82
Students: 97
Race: Hispanic", "FiscalYear: 2015
Percent: 62
Students: 93
Race: Hispanic"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(0,191,125,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(0,191,125,1)" + } + }, + "name": "Hispanic", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [76.27, 71.43, 59.83, 64.34], + "text": ["FiscalYear: 2012
Percent: 76.27
Students: 90
Race: Non Hispanic Multirace", "FiscalYear: 2013
Percent: 71.43
Students: 95
Race: Non Hispanic Multirace", "FiscalYear: 2014
Percent: 59.83
Students: 70
Race: Non Hispanic Multirace", "FiscalYear: 2015
Percent: 64.34
Students: 83
Race: Non Hispanic Multirace"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(0,191,196,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(0,191,196,1)" + } + }, + "name": "Non Hispanic Multirace", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [71.15, 81.82, 88.46, 74.29], + "text": ["FiscalYear: 2012
Percent: 71.15
Students: 37
Race: Non Resident Alien", "FiscalYear: 2013
Percent: 81.82
Students: 27
Race: Non Resident Alien", "FiscalYear: 2014
Percent: 88.46
Students: 23
Race: Non Resident Alien", "FiscalYear: 2015
Percent: 74.29
Students: 26
Race: Non Resident Alien"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(0,176,246,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(0,176,246,1)" + } + }, + "name": "Non Resident Alien", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [57.89, 57.38, 52.08, 63.83], + "text": ["FiscalYear: 2012
Percent: 57.89
Students: 44
Race: Unknown", "FiscalYear: 2013
Percent: 57.38
Students: 35
Race: Unknown", "FiscalYear: 2014
Percent: 52.08
Students: 25
Race: Unknown", "FiscalYear: 2015
Percent: 63.83
Students: 30
Race: Unknown"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(149,144,255,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(149,144,255,1)" + } + }, + "name": "Unknown", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + }, + { + "x": [1, 2, 3, 4], + "y": [65.4, 63.27, 65.78, 64.03], + "text": ["FiscalYear: 2012
Percent: 65.4
Students: 635
Race: White", "FiscalYear: 2013
Percent: 63.27
Students: 577
Race: White", "FiscalYear: 2014
Percent: 65.78
Students: 571
Race: White", "FiscalYear: 2015
Percent: 64.03
Students: 518
Race: White"], + "key": null, + "type": "scatter", + "mode": "markers", + "marker": { + "autocolorscale": false, + "color": "rgba(231,107,243,1)", + "opacity": 1, + "size": 5.6693, + "symbol": "circle", + "line": { + "width": 1.8898, + "color": "rgba(231,107,243,1)" + } + }, + "name": "White", + "showlegend": true, + "xaxis": "x", + "yaxis": "y", + "hoverinfo": "text" + } + ], + "layout": { + "margin": { + "t": 34.8976, + "r": 75.5906, + "b": 51.243, + "l": 57.6191 + }, + "plot_bgcolor": "rgba(255,255,255,1)", + "paper_bgcolor": "rgba(255,255,255,1)", + "font": { + "color": "rgba(34,34,34,1)", + "family": "Roboto", + "size": 15.9402 + }, + "xaxis": { + "type": "linear", + "autorange": false, + "tickmode": "array", + "range": [0.4, 4.6], + "ticktext": ["2012", "2013", "2014", "2015"], + "tickvals": [1, 2, 3, 4], + "ticks": "", + "tickcolor": null, + "ticklen": 3.653, + "tickwidth": 0, + "showticklabels": true, + "tickfont": { + "color": "rgba(77,77,77,1)", + "family": "Roboto", + "size": 12.7522 + }, + "tickangle": -0, + "showline": false, + "linecolor": null, + "linewidth": 0, + "showgrid": true, + "domain": [0, 1], + "gridcolor": "rgba(216,216,216,1)", + "gridwidth": 0.6642, + "zeroline": false, + "anchor": "y", + "title": "Fiscal year", + "titlefont": { + "color": "rgba(34,34,34,1)", + "family": "Roboto", + "size": 15.9402 + }, + "hoverformat": ".2f" + }, + "yaxis": { + "type": "linear", + "autorange": false, + "tickmode": "array", + "range": [-5, 105], + "ticktext": ["0", "25", "50", "75", "100"], + "tickvals": [0, 25, 50, 75, 100], + "ticks": "", + "tickcolor": null, + "ticklen": 3.653, + "tickwidth": 0, + "showticklabels": true, + "tickfont": { + "color": "rgba(77,77,77,1)", + "family": "Roboto", + "size": 12.7522 + }, + "tickangle": -0, + "showline": false, + "linecolor": null, + "linewidth": 0, + "showgrid": true, + "domain": [0, 1], + "gridcolor": "rgba(216,216,216,1)", + "gridwidth": 0.6642, + "zeroline": false, + "anchor": "x", + "title": "% of students", + "titlefont": { + "color": "rgba(34,34,34,1)", + "family": "Roboto", + "size": 15.9402 + }, + "hoverformat": ".2f" + }, + "shapes": [ + { + "type": "rect", + "fillcolor": "transparent", + "line": { + "color": "rgba(0,0,0,1)", + "width": 0, + "linetype": "solid" + }, + "yref": "paper", + "xref": "paper", + "x0": 0, + "x1": 1, + "y0": 0, + "y1": 1 + } + ], + "showlegend": true, + "legend": { + "bgcolor": "rgba(255,255,255,1)", + "bordercolor": "transparent", + "borderwidth": 1.8898, + "font": { + "color": "rgba(34,34,34,1)", + "family": "Roboto", + "size": 11.9552 + }, + "y": -0.4, + "orientation": "h" + }, + "hovermode": "closest", + "width": 680, + "height": 450 + } +} diff --git a/test/jasmine/tests/legend_test.js b/test/jasmine/tests/legend_test.js index 1e6b3490aa8..e2934588545 100644 --- a/test/jasmine/tests/legend_test.js +++ b/test/jasmine/tests/legend_test.js @@ -540,3 +540,26 @@ describe('legend relayout update', function() { }); }); }); + +describe('legend orientation change:', function() { + 'use strict'; + + afterEach(destroyGraphDiv); + + it('should update plot background', function(done) { + var mock = require('@mocks/legend_horizontal_autowrap.json'), + gd = createGraphDiv(), + initialLegendBGColor; + + Plotly.plot(gd, mock.data, mock.layout).then(function() { + initialLegendBGColor = gd._fullLayout.legend.bgcolor; + return Plotly.relayout(gd, 'legend.bgcolor', '#000000'); + }).then(function() { + expect(gd._fullLayout.legend.bgcolor).toBe('#000000'); + return Plotly.relayout(gd, 'legend.bgcolor', initialLegendBGColor); + }).then(function() { + expect(gd._fullLayout.legend.bgcolor).toBe(initialLegendBGColor); + done(); + }); + }); +});