plotly.js icon indicating copy to clipboard operation
plotly.js copied to clipboard

Add subtitle to plots

Open emilykl opened this issue 1 year ago • 16 comments
trafficstars

Closes #6856

Adds ability to add a subtitle to plots.

Screen Shot 2024-06-04 at 5 44 29 PM
  • [x] Able to add subtitle using the following API:
    "layout": {
        "title": {
            "text": "A simple plot",
            "subtitle": {
                "text": "With a subtitle"
            }
  • [x] Ability to change the font options of the subtitle by passing font options to layout.title.subtitle.font
  • [x] Ability to edit the subtitle when editable: true

emilykl avatar Jun 04 '24 21:06 emilykl

  • [ ] Subtitle should wrap automatically onto new lines when too long (maybe? Ideally yes but this is complicated)

I think word wrap is out of the scope of this PR. cc: #382

archmoj avatar Jun 05 '24 14:06 archmoj

@emilykl Do you think that the height of subtitle should push the margins? In your example if I set subtitle.text: '1st<br>2nd<br>3rd<br>4th' the subtitle would go over the cartesian subplot area.

archmoj avatar Jun 05 '24 18:06 archmoj

This is quite minor. But I noticed when config.editable is set to true and having an empty subtitle I get a black subtitle instead of gray on Click to enter Plot subtitle.

        "title": {
            "text": "A simple plot",
            "subtitle": {
                "text": ""
            }
        },

archmoj avatar Jun 05 '24 18:06 archmoj

Just addressed a baseline update in #7019. Please pull master and merge it into this branch. Thank you!

archmoj avatar Jun 06 '24 15:06 archmoj

@emilykl Do you think that the height of subtitle should push the margins? In your example if I set subtitle.text: '1st<br>2nd<br>3rd<br>4th' the subtitle would go over the cartesian subplot area.

@archmoj I would say "Yes, obviously", except that it looks like currently, even the height of the title doesn't push the margins -- if you add too many lines to the title, it will overlap the chart (see codepen).

So I think if we make the subtitle push the margins dynamically, we need to make that the behavior for the title as well.

I guess the other option would be to push the margin by a fixed amount if there is a subtitle present. I'm not sure whether that's necessary though; the current amount of space looks OK to me if the subtitle is one or two lines.

emilykl avatar Jun 06 '24 19:06 emilykl

This is quite minor. But I noticed when config.editable is set to true and having an empty subtitle I get a black subtitle instead of gray on Click to enter Plot subtitle.

good catch, thanks. Do you know where this styling is set in the code?

I think this is probably also related to the bug I noticed where the size of the text currently being edited is too large for the subtitle.

emilykl avatar Jun 06 '24 19:06 emilykl

For subtitles please add a French (or other languages that you know) translation to lib/locales e.g. here: https://github.com/plotly/plotly.js/blob/caf32e57cfdbd370076b926f84c114093f62f0b9/lib/locales/fr.js#L13

archmoj avatar Jun 17 '24 14:06 archmoj

Screenshot from 2024-06-17 15-05-51

@emilykl Please note that we discouraged force pushing (into this repository) when having a PR open with comments as it could confuse reviewers specially when they have started the review of the PR. Please don't worry and this is just fine for this pull request. I've done it in the past and I would possibly consider force pushing in certain cases in future. But in general it's better to do a rebase on most recent commits, if you want to edit a mistake on last push. And in case you want to have the changes from the master branch, you should consider merging master instead of rebasing. Thank you!

archmoj avatar Jun 17 '24 19:06 archmoj

Now only 3 tests fails when running the following command on my machine as well as on the CI.

npm run test-jasmine colorbar

archmoj avatar Jun 18 '24 20:06 archmoj

Please also add a draft log for this PR. Thank you!

archmoj avatar Jun 18 '24 20:06 archmoj

Yes I'm getting those same failures locally too @archmoj . I'm having trouble figuring out how to debug, please let me know if you have any suggestions!

emilykl avatar Jun 18 '24 20:06 emilykl

Yes I'm getting those same failures locally too @archmoj . I'm having trouble figuring out how to debug, please let me know if you have any suggestions!

I get different graphs for this on the master branch compared to this branch related to the size of an empty colorbar title:

Plotly.newPlot(gd,
  [{z: [[1, 2], [3, 4]], type: 'heatmap'}],
  {width: 400, height: 400},
  {editable: true}
);

On master: Screenshot from 2024-06-18 17-09-43

This branch: Screenshot from 2024-06-18 17-09-32

The behavior on master may be more desirable. No?

archmoj avatar Jun 18 '24 21:06 archmoj

@archmoj Good catch.

Overall the behavior on master is better, I agree. Although the location of the placeholder (above the colorbar rather than overlapping it) is better on this branch. I will try to figure out how to prevent the placeholder from crowding the chart over to the left. There must be a subtle difference between the element properties on master vs. this branch.

emilykl avatar Jun 18 '24 21:06 emilykl

@archmoj I believe the above issue with the colorbar title is fixed. (Colorbar tests are passing on my machine; we will see if they pass in the CI.)

The issue was that the colorbar title was missing the js-placeholder class, which I have fixed. "unfortunately" that has also made it revert to the weird placement seen on master; however I think fixing that is beyond the scope of this PR.

Screen Shot 2024-06-18 at 6 27 49 PM

emilykl avatar Jun 18 '24 22:06 emilykl

@emilykl The PR looks great! :1st_place_medal: Is there is anything else on your toDo list? You may consider updating the PR description too.

archmoj avatar Jun 25 '24 18:06 archmoj

@archmoj I believe all outstanding issues are resolved.

Tests are passing.

Can I have a final 👍 to merge?

emilykl avatar Jun 28 '24 19:06 emilykl

@emilykl I was trying to add a subtitle to automargin-title-paper-multiline-bottom mock; but it may not render properly? Please check

{
  "data": [
    {
      "showlegend": false,
      "type": "scatter",
      "x": [1, 2, 3],
      "y": [4, 5, 6]
    }
  ],
  "layout": {
    "height": 300,
    "width": 400,
    "margin": { "t": 0, "b": 0, "l": 0, "r": 0 },
    "title": {
      "subtitle": {
        "text": "subtitle"
      },
      "automargin": true,
      "text": "Paper<br>Multi-line",
      "pad": { "t": 15, "b": 10 },
      "yref": "paper",
      "y": 0
    }
  }
}

archmoj avatar Jul 02 '24 13:07 archmoj

Also a mock like 19 that has a tile.text: 'Click to enter Plot title' it does not render a subtitle unless one changes the title!

archmoj avatar Jul 02 '24 13:07 archmoj

Mock 28 has a title stating with a <br>. When the subtitle is added the position of it is not under the title.

archmoj avatar Jul 02 '24 14:07 archmoj

The position of subtitle is not correct when adding a subtitle to automargin-title-paper-multiline mock.

archmoj avatar Jul 02 '24 14:07 archmoj

Also please check the position of subtitle when added to bar-alignment-offset mock.

archmoj avatar Jul 02 '24 14:07 archmoj

Similarly on the worldcup mock the positioning of subtitle could be improved.

archmoj avatar Jul 02 '24 14:07 archmoj

Also a mock like 19 that has a tile.text: 'Click to enter Plot title' it does not render a subtitle unless one changes the title!

@archmoj Good catch. I've modified the logic so that the subtitle is rendered even if there is no title (or the title is just a placeholder). But the placement is a bit odd since the subtitle is positioned according to the height of the title, and the title height is 0. Would you suggest any changes here? I'm inclined to leave as-is since this is a pretty rare edge case and doing anything more complicated would be kind of tricky and involve a bunch of extra logic.

Screen Shot 2024-07-09 at 4 29 35 PM

emilykl avatar Jul 09 '24 20:07 emilykl

Also a mock like 19 that has a tile.text: 'Click to enter Plot title' it does not render a subtitle unless one changes the title!

@archmoj Good catch. I've modified the logic so that the subtitle is rendered even if there is no title (or the title is just a placeholder). But the placement is a bit odd since the subtitle is positioned according to the height of the title, and the title height is 0. Would you suggest any changes here? I'm inclined to leave as-is since this is a pretty rare edge case and doing anything more complicated would be kind of tricky and involve a bunch of extra logic.

Screen Shot 2024-07-09 at 4 29 35 PM

If an empty title (title: '') renders similar to a blank title (title: ' ') then yes this should be the correct behavior IMHO.

archmoj avatar Jul 09 '24 21:07 archmoj

Similarly on the worldcup mock the positioning of subtitle could be improved.

This one is fixed now.

archmoj avatar Jul 09 '24 21:07 archmoj

Mock 28 has a title stating with a <br>. When the subtitle is added the position of it is not under the title.

This renders OK now after the fixes. Thanks!

archmoj avatar Jul 11 '24 20:07 archmoj

@emilykl Would you try taking into account the height of subtitle text in the automargin calculations?

archmoj avatar Jul 11 '24 20:07 archmoj

@emilykl Would you try taking into account the height of subtitle text in the automargin calculations?

@archmoj Yes I am working on it, it's a bit tricky as the code for the automargin is pretty convoluted

emilykl avatar Jul 16 '24 14:07 emilykl

Thanks @emilykl for this great PR :trophy: :dancer:

archmoj avatar Jul 18 '24 12:07 archmoj

Wow thanks @archmoj @emilykl I was literally just looking for this feature this morning! 🥳

hannahker avatar Jul 18 '24 19:07 hannahker