Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to display text over heatmaps & histogram2d #6028

Merged
merged 16 commits into from Dec 6, 2021

Conversation

archmoj
Copy link
Contributor

@archmoj archmoj commented Nov 18, 2021

Resolves #1116 by adding texttemplate and textfont options.

@plotly/plotly_js

@nicolaskruchten
Copy link
Member

Looks awesome! Let's make sure we get auto-color-contrasting and a new attribute to optionally force the color. And let's get this in histogram2d if it's not already done ;)

@archmoj
Copy link
Contributor Author

archmoj commented Nov 18, 2021

Let's make sure we get auto-color-contrasting and a new attribute to optionally force the color.

Done in b52adf2.

@nicolaskruchten
Copy link
Member

Nice!

For baseline 13, white-on-red seems very hard to read to me... is that normal?

@archmoj archmoj changed the title Add options to display text over heatmaps Add options to display text over heatmaps & histogram2d Nov 19, 2021
@archmoj archmoj added this to the v2.7.0 milestone Nov 23, 2021
@@ -42,6 +42,18 @@ module.exports = extendFlat({
yhoverformat: axisHoverFormat('y'),
zhoverformat: axisHoverFormat('z', 1),
hovertemplate: heatmapAttrs.hovertemplate,
texttemplate: extendFlat({}, heatmapAttrs.texttemplate, {
description: [
'For this trace it only has an effect if `coloring` is set to *heatmap*.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this restriction? Not a big deal, probably won't get a lot of use in contour maps anyway. But the only issue I can think of is what autocolor to give the text - for coloring='fill' it should work pretty well with the same logic as heatmap, and for other coloring values we'd just contrast with the plot_bgcolor. No?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we may try to add this option for other coloring values in another PR.
But that would require extra work as those use different plot paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A contour plot with a coloring other than heatmap has different plotting path. So enabling this feature requires extra work. So it could potentially be added later on the road.

@@ -332,6 +352,185 @@ module.exports = function(gd, plotinfo, cdheatmaps, heatmapLayer) {
y: top,
'xlink:href': canvas.toDataURL('image/png')
});

removeLabels(plotGroup);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In principle we should be able to do this with the d3 add/remove/update idiom, rather than removing and re-adding everything. The performance seems fine with all the mocks you added text to here, but I worry about it bogging down during interactions in more intense cases, like if we update https://dash.plotly.com/dash-bio/alignmentchart with this feature (try dragging the rangeslider there right now, it's terrible)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When trying

Plotly.relayout(gd, 'xaxis.rangeslider', {});
Plotly.restyle(gd, 'texttemplate', '%{z}');

e.g. on earth_heatmap the performance seems good.

So I suggest we move forward with this feature and come back to further optimize it if we hit a performance problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - it is a little fiddly to get the add/remove/update idiom working properly, and anyway perhaps the higher-value addition to this feature would be a way to disable showing labels at all beyond a certain scale as you zoom out, which we have already decided not to do in this PR.


labels
.enter()
.append('g')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need each text element wrapped in a separate group? Could we get away with one group with all the text elements inside it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This quite similar to the way we draw ticklabels:

tickLabels.enter().append('g')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I think this is important for MathJax purposes, so I guess if we think supporting that in heatmap text is something we want to do in the future we can leave it.

@archmoj archmoj removed this from the v2.7.0 milestone Dec 3, 2021
@archmoj archmoj added this to the v2.8.0 milestone Dec 3, 2021
Copy link
Contributor

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃 Let's do it :)

@archmoj archmoj merged commit 285b923 into master Dec 6, 2021
@archmoj archmoj deleted the heatmap-with-text branch December 6, 2021 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

texttemplate for heatmaps & histogram2d traces
3 participants