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

Plot title additions #2337

Merged
merged 10 commits into from Oct 17, 2023
Merged

Plot title additions #2337

merged 10 commits into from Oct 17, 2023

Conversation

OAGr
Copy link
Contributor

@OAGr OAGr commented Oct 15, 2023

This PR makes a few additions to plots:

  1. Before, Plot.dist and Plot.dists accepted title attributes, but the other plot types didn't. Now, they all do.
  2. Primary titles are done in html, not d3. This is an addition to functions and scatterplots, and a change to Plot.dists, which used d3 before.
  3. Scales now get a title attribute. This allows you to specify a title for the xAxis and yAxis. This is done in D3.
  4. A few Error Boundaries are added to the playground. Previously, invalid tickFormats broke the editor.
  5. We now validate tickFormats in squiggle-language, using a regex.
  6. The patchedScales.ts default uses 3 points of precision, instead of 9. This means that hovering typically provides a better experience.
  7. The Dist SummaryChart now uses the xAxis TickFormat(), if provided.
  8. Minor: inspect() now outputs the JS object to the console, instead of its toString().
  9. Minor: A few tests fixed, some code cleaned up.
  10. Docs: New changes added to documentation.
image image image image

One minor issue (shown here), is that it might be better if the xScaleTitle was shown under the samples bar. Or maybe, we should move the samples bar to be over the x-axis, overlapping with the distribution. I'm not sure.

@changeset-bot
Copy link

changeset-bot bot commented Oct 15, 2023

🦋 Changeset detected

Latest commit: 35aea89

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@quri/squiggle-lang Patch
@quri/squiggle-components Patch
@quri/prettier-plugin-squiggle Patch
vscode-squiggle Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@OAGr OAGr temporarily deployed to Preview October 15, 2023 22:28 — with GitHub Actions Inactive
@vercel
Copy link

vercel bot commented Oct 15, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
quri-hub ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 16, 2023 10:39pm
squiggle-components ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 16, 2023 10:39pm
squiggle-website ✅ Ready (Inspect) Visit Preview Oct 16, 2023 10:39pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
quri-ui ⬜️ Ignored (Inspect) Visit Preview Oct 16, 2023 10:39pm

@@ -91,7 +91,7 @@ export const library = [
name: "inspect",
definitions: [
makeDefinition([frAny], ([value]) => {
console.log(value.toString());
console.log(value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

More useful for debugging. toString() really doesn't provide much information.

Copy link
Collaborator

@berekuk berekuk left a comment

Choose a reason for hiding this comment

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

.3~f problem is significant, everything else is minor, great PR overall.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Two requests:

  • rename file to PlotTitle (properly capitalized)
  • move to src/components? (I don't know if our current fs layout is optimal, but so far we didn't have any components in src/lib/)

@@ -117,31 +115,22 @@ const InnerDistributionsChart: FC<{
suggestedPadding: {
left: 10,
right: 10,
top: 10 + legendHeight + titleHeight,
top: 10 + legendHeight,
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a slight discrepancy (4px) with the old behavior.

Before: https://www.squiggle-language.com/playground?v=dev#code=eNqrVkpJTUsszSlxzk9JVbJSqlCwVQiuzNUrzctMyy%2FK1TDSMTTQVKoFAAjgDIc%3D

After: https://squiggle-website-git-plot-additions-quantified-uncertainty.vercel.app/playground?v=dev#code=eNqrVkpJTUsszSlxzk9JVbJSqlCwVQiuzNUrzctMyy%2FK1TDSMTTQVKoFAAjgDIc%3D

In both versions, canvas is 84px tall:

  • 20px bottom padding
  • 14px top padding previously (10 + empty titleHeight); 10px now

So previously chart height itself was exactly as was requested, 50px, but it's 54px now.

Of course this doesn't matter much, but to avoid future confusion about what's right, I'd suggest:

  • move suggestedPadding outside of draw function
  • then, calculate const height based on it, const height = innerHeight + suggestedPadding.top + suggestedPadding.bottom, then we'd be confident that they match and that innerHeight is correct

Copy link
Contributor Author

@OAGr OAGr Oct 16, 2023

Choose a reason for hiding this comment

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

This is a messy area, thanks for explaining!

For one thing, I imagine we'll later just want the labels to be in HTML, which would change the constraints here.

We'd also want it underneath the samples toolbar, as discussed in my PR description.

@@ -386,6 +387,7 @@ export type Plot =
fn: Lambda;
xScale: Scale;
yScale: Scale;
title?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was there a problem with also having a title on "relativeValues" plots?

If we had it then you could extract these props into CommonPlotArgs, similar to CommonScaleArgs, and also put get title() accessor in SqAbstractPlot, so it would simplify things a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. I just didn't get to adding it to relativeValues plots.

@@ -56,6 +62,12 @@ export function drawAxes({
const tickSize = 2;

const padding: Padding = { ...suggestedPadding };
if (xAxisTitle) {
padding.bottom = padding.bottom + 20;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a potential problem, more important than the one about 4px in my other comment.

I'm not proposing any immediate changes here, just flagging a potential technical debt.

By design, drawAxes always had the potential to increase suggested paddings. But previously that didn't happen on vertical padding.

This is a problem because we determine the height of canvas first, then try to draw on it and increase padding, which decreases the chart's main area. If chartHeight in settings is low enough, main area size might become tiny or even negative.

I made this experiment, because AFAIK chartHeight in calculators is the lowest one we use currently: https://squiggle-website-git-plot-additions-quantified-uncertainty.vercel.app/playground?v=dev#code=eNpNTs0KgzAMfpWQ0wTxMNil173AwN3WHaKtKOsPaMoc2nefVdwMId9PPkImVLqhYPjqlUaBd6qMLiy99Ek6gCkNAEVMAh4SG%2B8lPvPNrb0J1g1psepfPFXjxFECzON8lDfjuVDdwKcJEgg4A3u45DCWNRktYIXCdE5Tv4S44%2BRKrAy1EiFmS%2F8vxnznO9vejNJlmOPQ%2BncZrKX%2Bg4L7oOMXsMVItw%3D%3D

Inner height there is still positive, but dangerously close to zero. Ideally, we should always respect inner height (chartHeight) as we the component received and add padding around it.

The circular dependency that needs to be untangled to fix this is a bit complex:

  • <canvas> needs a height property immediately when the component is mounted
  • height of the entire canvas is innerHeight (=chartHeight) + padding that's dynamic and depends on other attributes; sometimes padding also depends on data; right now it's just for horizontal padding (because tick labels can be of arbitrary length), but not for the vertical padding
    • but that can change in the future too; for example if we decide to output x axis tick labels diagonally when they're too long
    • even for xAxisTitle, you've added 20px here, but the most proper way would be to context.measureText
  • the problem is, measureText requires context, which is obtained through canvas.getContext("2d"), but that's too late; so we need height for canvas, and canvas.getContext for height
  • in theory, though, any canvas would do for measureText; we could initialize a single hidden canvas and use it for all measurements

So this is solvable, but would require significant changes or additions to the APIs that we have (useCanvas and drawAxes).

const fixedFormat = locale.format(".9~f");
const siFormat = locale.format(".3~s");
const expFormat = locale.format(".3~e");
const fixedFormat = locale.format(".3~f");
Copy link
Collaborator

Choose a reason for hiding this comment

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

This causes a blind spot in 0.001..0.00001 interval: https://squiggle-website-git-plot-additions-quantified-uncertainty.vercel.app/playground?v=dev#code=eNqrVkpJTUsszSlxzk9JVbJSMtAzAAJDhZJ8BTDTUElHqTgjvzy4NDc3sahSyaqkqDS1FgC78hDF

This is a problem particularly for relative values where dists like this are going to be frequent.

It's especially confusing if you try to move your cursor from 0 to some value around 0.001:

  • first you get tiny non-zero values (1e-6)
  • then you get to this blind spot where verything is 0
  • and then from 0.001 it's non-zero again

.3~r works well for values under 0.001:

> d3.format('0.3~f')(0.03123123)
'0.031'
> d3.format('0.3~f')(0.0003123123)
'0'
> d3.format('0.3~r')(0.03123123)
'0.0312'
> d3.format('0.3~r')(0.0003123123)
'0.000312'

Then above 1000 it becomes bad again:

> d3.format('0.3~r')(12345.123145125)
'12300'

How about another else if branch below that uses .3~r for values below 0.001 and .3~f above that?

It's unfortunate that there's nothing in d3 for "show all digits before decimal point, and first N significant digits after that".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea about the else if branch, will try.

I was nervous about this, but was figuring I'd address later, but happy to do it now.

Copy link
Contributor Author

@OAGr OAGr Oct 16, 2023

Choose a reason for hiding this comment

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

I just reverted this for this story, will move to a separate story.

@OAGr OAGr temporarily deployed to Preview October 16, 2023 22:26 — with GitHub Actions Inactive
@OAGr OAGr temporarily deployed to Preview October 16, 2023 22:28 — with GitHub Actions Inactive
@codecov
Copy link

codecov bot commented Oct 16, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (0e301b1) 71.69% compared to head (de20836) 71.74%.
Report is 2 commits behind head on main.

❗ Current head de20836 differs from pull request most recent head 35aea89. Consider uploading reports for the commit 35aea89 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2337      +/-   ##
==========================================
+ Coverage   71.69%   71.74%   +0.05%     
==========================================
  Files         111      111              
  Lines        5670     5688      +18     
  Branches     1111     1120       +9     
==========================================
+ Hits         4065     4081      +16     
- Misses       1597     1599       +2     
  Partials        8        8              

see 7 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@OAGr
Copy link
Contributor Author

OAGr commented Oct 18, 2023

Closed #1725

@github-actions github-actions bot mentioned this pull request Nov 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants