Skip to content

Commit f5c6f47

Browse files
authored
Merge pull request #6038 from plotly/histogram-text
New attributes to display text on the `histogram` trace
2 parents de1664b + c8f9f73 commit f5c6f47

21 files changed

+225
-3
lines changed

Diff for: draftlogs/6038_add.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Add `texttemplate`, `textposition`, `textfont`, `textangle`,
2+
`outsidetextfont`, `insidetextfont`, `insidetextanchor`,
3+
`constraintext` and `cliponaxis` to `histogram` trace [[#6038](https://github.com/plotly/plotly.js/pull/6038)]

Diff for: src/traces/bar/plot.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, opts, makeOnCom
428428
}
429429

430430
transform.fontSize = font.size;
431-
recordMinTextSize(trace.type, transform, fullLayout);
431+
recordMinTextSize(trace.type === 'histogram' ? 'bar' : trace.type, transform, fullLayout);
432432
calcBar.transform = transform;
433433

434434
transition(textSelection, fullLayout, opts, makeOnCompleteCallback)

Diff for: src/traces/histogram/attributes.js

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var barAttrs = require('../bar/attributes');
44
var axisHoverFormat = require('../../plots/cartesian/axis_format_attributes').axisHoverFormat;
55
var hovertemplateAttrs = require('../../plots/template_attributes').hovertemplateAttrs;
6+
var texttemplateAttrs = require('../../plots/template_attributes').texttemplateAttrs;
7+
var fontAttrs = require('../../plots/font_attributes');
68
var makeBinAttrs = require('./bin_attributes');
79
var constants = require('./constants');
810
var extendFlat = require('../../lib/extend').extendFlat;
@@ -198,6 +200,44 @@ module.exports = {
198200
keys: constants.eventDataKeys
199201
}),
200202

203+
texttemplate: texttemplateAttrs({
204+
arrayOk: false,
205+
editType: 'plot'
206+
}, {
207+
keys: ['label', 'value']
208+
}),
209+
210+
textposition: extendFlat({}, barAttrs.textposition, {
211+
arrayOk: false
212+
}),
213+
214+
textfont: fontAttrs({
215+
arrayOk: false,
216+
editType: 'plot',
217+
colorEditType: 'style',
218+
description: 'Sets the text font.'
219+
}),
220+
221+
outsidetextfont: fontAttrs({
222+
arrayOk: false,
223+
editType: 'plot',
224+
colorEditType: 'style',
225+
description: 'Sets the font used for `text` lying outside the bar.'
226+
}),
227+
228+
insidetextfont: fontAttrs({
229+
arrayOk: false,
230+
editType: 'plot',
231+
colorEditType: 'style',
232+
description: 'Sets the font used for `text` lying inside the bar.'
233+
}),
234+
235+
insidetextanchor: barAttrs.insidetextanchor,
236+
237+
textangle: barAttrs.textangle,
238+
cliponaxis: barAttrs.cliponaxis,
239+
constraintext: barAttrs.constraintext,
240+
201241
marker: barAttrs.marker,
202242

203243
offsetgroup: barAttrs.offsetgroup,

Diff for: src/traces/histogram/defaults.js

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var Registry = require('../../registry');
44
var Lib = require('../../lib');
55
var Color = require('../../components/color');
66

7+
var handleText = require('../bar/defaults').handleText;
78
var handleStyleDefaults = require('../bar/style_defaults');
89
var attributes = require('./attributes');
910

@@ -22,6 +23,16 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2223
}
2324

2425
coerce('text');
26+
var textposition = coerce('textposition');
27+
handleText(traceIn, traceOut, layout, coerce, textposition, {
28+
moduleHasSelected: true,
29+
moduleHasUnselected: true,
30+
moduleHasConstrain: true,
31+
moduleHasCliponaxis: true,
32+
moduleHasTextangle: true,
33+
moduleHasInsideanchor: true
34+
});
35+
2536
coerce('hovertext');
2637
coerce('hovertemplate');
2738
coerce('xhoverformat');

Diff for: test/image/baselines/hist_all_integer.png

3.47 KB
Loading

Diff for: test/image/baselines/hist_category.png

2.41 KB
Loading

Diff for: test/image/baselines/hist_cum_stacked.png

2.73 KB
Loading

Diff for: test/image/baselines/hist_stacked.png

4.32 KB
Loading

Diff for: test/image/baselines/hist_summed.png

3.46 KB
Loading

Diff for: test/image/baselines/histogram-offsetgroups.png

-16.6 KB
Loading

Diff for: test/image/baselines/histogram_barmode_relative.png

4.32 KB
Loading

Diff for: test/image/baselines/histogram_colorscale.png

5.09 KB
Loading

Diff for: test/image/mocks/hist_all_integer.json

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
22
"data":[{
33
"x":["1","2","3","1","2","4","2","3","4","1","2","3","1","2","3","1","2","3","1","2","3","1","2","3","1","2","3","2","3","4","2","3","4","2","3","4","2","3","4"],
4+
"texttemplate": "%{value}",
5+
"textangle": -45,
6+
"textfont": {
7+
"size": 32,
8+
"color": "rgba(255,255,0,0.5)",
9+
"family": "Times New Roman"
10+
},
411
"type":"histogram"
512
}],
613
"layout":{"height":300,"width":400}

Diff for: test/image/mocks/hist_category.json

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1-
{"data":[{"x":["a","b","c","a","b","d","b","c","d"],"type":"histogram"}],
2-
"layout":{"height":300,"width":400}}
1+
{
2+
"data": [
3+
{
4+
"x": [
5+
"a",
6+
"b",
7+
"c",
8+
"a",
9+
"b",
10+
"d",
11+
"b",
12+
"c",
13+
"d"
14+
],
15+
"texttemplate": "%{value} X %{label}",
16+
"type": "histogram"
17+
}
18+
],
19+
"layout": {
20+
"height": 300,
21+
"width": 400
22+
}
23+
}

Diff for: test/image/mocks/hist_cum_stacked.json

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"data": [{
33
"x": [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10],
44
"type": "histogram",
5+
"texttemplate": "%{value}",
6+
"insidetextanchor": "middle",
57
"cumulative": {"enabled": true},
68
"xbins": {"start": 0.5, "end": 10.5, "size": 1},
79
"marker": {"color": "blue", "line": {"width": 2, "color": "#000"}},
@@ -10,6 +12,8 @@
1012
{
1113
"x": [3, 3, 4, 5, 6, 7, 7],
1214
"type": "histogram",
15+
"texttemplate": "%{value}",
16+
"insidetextanchor": "middle",
1317
"cumulative": {"enabled": true, "currentbin": "exclude"},
1418
"xbins": {"start": 0.5, "end": 10.5},
1519
"marker": {"color": "red", "line": {"width": 2, "color": "#000"}},

Diff for: test/image/mocks/hist_stacked.json

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
{
22
"data": [{
33
"x": [1, 1, 1, 2, 2],
4+
"text": "Blues",
5+
"textposition": "inside",
6+
"texttemplate": "%{value}<br>%{text}",
47
"type": "histogram"
58
}, {
69
"x": [1, 2, 3, 4],
10+
"text": "Orange",
11+
"textposition": "outside",
12+
"cliponaxis": false,
13+
"texttemplate": "%{value}<br>%{text}",
714
"type": "histogram"
815
}],
916
"layout": {"height": 300, "width": 400, "barmode": "stack"}

Diff for: test/image/mocks/hist_summed.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"Oranges",
1717
"Bananas"
1818
],
19+
"texttemplate": "%{value}<br>%{label}",
1920
"type": "histogram"
2021
}
2122
],

Diff for: test/image/mocks/histogram-offsetgroups.json

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
}
5959
],
6060
"layout": {
61+
"uniformtext": {
62+
"mode": "hide",
63+
"minsize": 5
64+
},
6165
"showlegend": false,
6266
"grid": {
6367
"rows": 2,

Diff for: test/image/mocks/histogram_barmode_relative.json

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"data": [{
33
"type": "histogram",
4+
"texttemplate": "%{value}",
45
"x": [9, 9, 3, 2, 5, 1, 3, 0, 6, 8, 5, 7, 2, 9, 9, 8, 5, 4, 1, 9, 2, 8, 0, 7, 2, 3, 5, 0, 3, 8, 2, 1, 7, 7, 7, 3, 9, 7, 8, 1, 7, 9, 4, 6, 2, 4, 2, 9, 3, 1, 5, 1, 6, 7, 6, 1, 6, 8, 6, 7, 8, 3, 7, 3, 1, 0, 2, 6, 1, 2, 7, 2, 9, 6, 2, 8, 0, 0, 9, 8, 5, 5, 8, 3, 5, 7, 7, 8, 3, 9, 3, 1, 5, 3, 5, 0, 8, 9, 4, 3]
56
}, {
67
"type": "histogram",
8+
"texttemplate": "%{value}",
79
"x": [6, 7, 8, 0, 8, 1, 5, 4, 4, 3, 4, 7, 5, 3, 9, 5, 2, 5, 5, 4, 3, 5, 2, 6, 3, 9, 8, 5, 3, 8, 7, 2, 2, 7, 3, 7, 0, 1, 1, 1, 2, 1, 4, 9, 3, 5, 4, 1, 1, 2, 0, 2, 8, 1, 0, 3, 1, 2, 3, 5, 3, 8, 6, 1, 1, 0, 0, 0, 8, 6, 0, 8, 6, 8, 0, 9, 4, 4, 0, 7, 7, 9, 2, 8, 0, 9, 0, 5, 7, 2, 9, 6, 5, 0, 0, 4, 6, 0, 9, 8]
810
}],
911
"layout": {

Diff for: test/image/mocks/histogram_colorscale.json

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"cmax": 5
1414
},
1515
"x": [ 0, 1, 1, 2, 2, 2, 3, 3, 4 ],
16+
"texttemplate": "%{value}",
17+
"textposition": "inside",
18+
"textfont": {
19+
"size": 64
20+
},
1621
"type": "histogram"
1722
}
1823
],

Diff for: test/plot-schema.json

+117
Original file line numberDiff line numberDiff line change
@@ -27922,6 +27922,24 @@
2792227922
"editType": "calc",
2792327923
"valType": "string"
2792427924
},
27925+
"cliponaxis": {
27926+
"description": "Determines whether the text nodes are clipped about the subplot axes. To show the text nodes above axis lines and tick labels, make sure to set `xaxis.layer` and `yaxis.layer` to *below traces*.",
27927+
"dflt": true,
27928+
"editType": "plot",
27929+
"valType": "boolean"
27930+
},
27931+
"constraintext": {
27932+
"description": "Constrain the size of text inside or outside a bar to be no larger than the bar itself.",
27933+
"dflt": "both",
27934+
"editType": "calc",
27935+
"valType": "enumerated",
27936+
"values": [
27937+
"inside",
27938+
"outside",
27939+
"both",
27940+
"none"
27941+
]
27942+
},
2792527943
"cumulative": {
2792627944
"currentbin": {
2792727945
"description": "Only applies if cumulative is enabled. Sets whether the current bin is included, excluded, or has half of its value included in the current cumulative value. *include* is the default for compatibility with various other tools, however it introduces a half-bin bias to the results. *exclude* makes the opposite half-bin bias, and *half* removes it.",
@@ -28338,6 +28356,38 @@
2833828356
"editType": "none",
2833928357
"valType": "string"
2834028358
},
28359+
"insidetextanchor": {
28360+
"description": "Determines if texts are kept at center or start/end points in `textposition` *inside* mode.",
28361+
"dflt": "end",
28362+
"editType": "plot",
28363+
"valType": "enumerated",
28364+
"values": [
28365+
"end",
28366+
"middle",
28367+
"start"
28368+
]
28369+
},
28370+
"insidetextfont": {
28371+
"color": {
28372+
"editType": "style",
28373+
"valType": "color"
28374+
},
28375+
"description": "Sets the font used for `text` lying inside the bar.",
28376+
"editType": "plot",
28377+
"family": {
28378+
"description": "HTML font family - the typeface that will be applied by the web browser. The web browser will only be able to apply a font if it is available on the system which it operates. Provide multiple font families, separated by commas, to indicate the preference in which to apply fonts if they aren't available on the system. The Chart Studio Cloud (at https://chart-studio.plotly.com or on-premise) generates images on a server, where only a select number of fonts are installed and supported. These include *Arial*, *Balto*, *Courier New*, *Droid Sans*,, *Droid Serif*, *Droid Sans Mono*, *Gravitas One*, *Old Standard TT*, *Open Sans*, *Overpass*, *PT Sans Narrow*, *Raleway*, *Times New Roman*.",
28379+
"editType": "plot",
28380+
"noBlank": true,
28381+
"strict": true,
28382+
"valType": "string"
28383+
},
28384+
"role": "object",
28385+
"size": {
28386+
"editType": "plot",
28387+
"min": 1,
28388+
"valType": "number"
28389+
}
28390+
},
2834128391
"legendgroup": {
2834228392
"description": "Sets the legend group for this trace. Traces part of the same legend group hide/show at the same time when toggling legend items.",
2834328393
"dflt": "",
@@ -29182,6 +29232,27 @@
2918229232
"h"
2918329233
]
2918429234
},
29235+
"outsidetextfont": {
29236+
"color": {
29237+
"editType": "style",
29238+
"valType": "color"
29239+
},
29240+
"description": "Sets the font used for `text` lying outside the bar.",
29241+
"editType": "plot",
29242+
"family": {
29243+
"description": "HTML font family - the typeface that will be applied by the web browser. The web browser will only be able to apply a font if it is available on the system which it operates. Provide multiple font families, separated by commas, to indicate the preference in which to apply fonts if they aren't available on the system. The Chart Studio Cloud (at https://chart-studio.plotly.com or on-premise) generates images on a server, where only a select number of fonts are installed and supported. These include *Arial*, *Balto*, *Courier New*, *Droid Sans*,, *Droid Serif*, *Droid Sans Mono*, *Gravitas One*, *Old Standard TT*, *Open Sans*, *Overpass*, *PT Sans Narrow*, *Raleway*, *Times New Roman*.",
29244+
"editType": "plot",
29245+
"noBlank": true,
29246+
"strict": true,
29247+
"valType": "string"
29248+
},
29249+
"role": "object",
29250+
"size": {
29251+
"editType": "plot",
29252+
"min": 1,
29253+
"valType": "number"
29254+
}
29255+
},
2918529256
"selected": {
2918629257
"editType": "style",
2918729258
"marker": {
@@ -29248,11 +29319,57 @@
2924829319
"editType": "calc",
2924929320
"valType": "string"
2925029321
},
29322+
"textangle": {
29323+
"description": "Sets the angle of the tick labels with respect to the bar. For example, a `tickangle` of -90 draws the tick labels vertically. With *auto* the texts may automatically be rotated to fit with the maximum size in bars.",
29324+
"dflt": "auto",
29325+
"editType": "plot",
29326+
"valType": "angle"
29327+
},
29328+
"textfont": {
29329+
"color": {
29330+
"editType": "style",
29331+
"valType": "color"
29332+
},
29333+
"description": "Sets the text font.",
29334+
"editType": "plot",
29335+
"family": {
29336+
"description": "HTML font family - the typeface that will be applied by the web browser. The web browser will only be able to apply a font if it is available on the system which it operates. Provide multiple font families, separated by commas, to indicate the preference in which to apply fonts if they aren't available on the system. The Chart Studio Cloud (at https://chart-studio.plotly.com or on-premise) generates images on a server, where only a select number of fonts are installed and supported. These include *Arial*, *Balto*, *Courier New*, *Droid Sans*,, *Droid Serif*, *Droid Sans Mono*, *Gravitas One*, *Old Standard TT*, *Open Sans*, *Overpass*, *PT Sans Narrow*, *Raleway*, *Times New Roman*.",
29337+
"editType": "plot",
29338+
"noBlank": true,
29339+
"strict": true,
29340+
"valType": "string"
29341+
},
29342+
"role": "object",
29343+
"size": {
29344+
"editType": "plot",
29345+
"min": 1,
29346+
"valType": "number"
29347+
}
29348+
},
29349+
"textposition": {
29350+
"arrayOk": false,
29351+
"description": "Specifies the location of the `text`. *inside* positions `text` inside, next to the bar end (rotated and scaled if needed). *outside* positions `text` outside, next to the bar end (scaled if needed), unless there is another bar stacked on this one, then the text gets pushed inside. *auto* tries to position `text` inside the bar, but if the bar is too small and no bar is stacked on this one the text is moved outside. If *none*, no text appears.",
29352+
"dflt": "auto",
29353+
"editType": "calc",
29354+
"valType": "enumerated",
29355+
"values": [
29356+
"inside",
29357+
"outside",
29358+
"auto",
29359+
"none"
29360+
]
29361+
},
2925129362
"textsrc": {
2925229363
"description": "Sets the source reference on Chart Studio Cloud for `text`.",
2925329364
"editType": "none",
2925429365
"valType": "string"
2925529366
},
29367+
"texttemplate": {
29368+
"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}\". Numbers are formatted using d3-format's syntax %{variable:d3-format}, for example \"Price: %{y:$.2f}\". https://github.com/d3/d3-format/tree/v1.4.5#d3-format 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}\". https://github.com/d3/d3-time-format/tree/v2.2.3#locale_format for details on the date formatting syntax. Every attributes that can be specified per-point (the ones that are `arrayOk: true`) are available. variables `label` and `value`.",
29369+
"dflt": "",
29370+
"editType": "plot",
29371+
"valType": "string"
29372+
},
2925629373
"transforms": {
2925729374
"items": {
2925829375
"transform": {

0 commit comments

Comments
 (0)