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

Initialize Selections #4139

Merged
merged 19 commits into from
Feb 23, 2019
Merged

Initialize Selections #4139

merged 19 commits into from
Feb 23, 2019

Conversation

arvind
Copy link
Member

@arvind arvind commented Aug 20, 2018

This PR relies on the refactoring introduced by #4068, so please merge that one first.

This PR adds an init property to selection definitions to initialize them using the fields or encoding channels a selection is projected over. For example, here is an updated version of our interactive_query_widgets example that initializes the selection to select cars with 8 cylinders manufactured in 1971.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "description": "Drag the sliders to highlight points.",
  "data": {"url": "data/cars.json"},
  "transform": [{"calculate": "year(datum.Year)", "as": "Year"}],
  "layer": [{
    "selection": {
      "CylYr": {
        "type": "single", "fields": ["Cylinders", "Year"],
        "init": {"Cylinders": 4, "Year": 1977},
        "bind": {
          "Cylinders": {"input": "range", "min": 3, "max": 8, "step": 1},
          "Year": {"input": "range", "min": 1969, "max": 1981, "step": 1}
        }
      }
    },
    "mark": "circle",
    "encoding": {
      "x": {"field": "Horsepower", "type": "quantitative"},
      "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
      "color": {
        "condition": {"selection": "CylYr", "field": "Origin", "type": "nominal"},
        "value": "grey"
      }
    }
  }, {
    "transform": [{"filter": {"selection": "CylYr"}}],
    "mark": "circle",
    "encoding": {
      "x": {"field": "Horsepower", "type": "quantitative"},
      "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
      "color": {"field": "Origin", "type": "nominal"},
      "size": {"value": 100}
    }
  }]
}

Similarly, an updated interactive_brush example to initialize the brush selection is as follows:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "description": "Drag out a rectangular brush to highlight points.",
  "data": {"url": "data/cars.json"},
  "selection": {
    "brush": {
      "type": "interval",
      "init": {"x": [55, 160], "y": [13, 37]}
    }
  },
  "mark": "point",
  "encoding": {
    "x": {"field": "Horsepower", "type": "quantitative"},
    "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
    "color": {
      "condition": {"selection": "brush", "field": "Cylinders", "type": "ordinal"},
      "value": "grey"
    }
  }
}

Fixes #3409, #3431, and vega/altair#599.

/cc @jakevdp

@arvind arvind force-pushed the as/initSelections branch from c5c5c44 to c475bb3 Compare August 20, 2018 18:22
@arvind arvind changed the title [WIP] Initialize Selections Initialize Selections Aug 20, 2018
@arvind arvind added the Blocked 🕐 For issues that are blocked by other issues label Aug 20, 2018
@arvind arvind requested review from domoritz and kanitw August 20, 2018 18:28
@arvind
Copy link
Member Author

arvind commented Aug 20, 2018

Tagged as blocked until #4068 is merged (which itself is blocked by vega/vega-parser#89). But, once the upstream PRs are merged, this one should be good to go. Compile- and runtime tests are passing locally.

@arvind arvind changed the title Initialize Selections [WIP] Initialize Selections Aug 22, 2018
@arvind
Copy link
Member Author

arvind commented Aug 22, 2018

With this spec, an infinite loop occurs when initializing the interval selection. I suspect something to do with scale triggers on the filter. Marked this PR as WIP until I have a chance to resolve this bug.

{
  "layer": [
    {
      "mark": {"type": "point", "color": "lightgray"},
      "selection": {
        "box": {
          "type": "interval", 
          "init": {"x": [50, 100], "y": [5, 10]
          }
        }
      }
    },
    {
      "mark": "point",
      "encoding": {
        "color": {
          "type": "quantitative", "field": "Cylinders", 
          "legend": false
        }
      },
      "transform": [{"filter": {"selection": "box"}}]
    }
  ],
  "encoding": {
    "x": {"type": "quantitative", "field": "Horsepower"},
    "y": {"type": "quantitative", "field": "Acceleration"}
  },
  "data": {"url": "data/cars.json"},
  "$schema": "https://vega.github.io/schema/vega-lite/v2.4.1.json"
}

@arvind
Copy link
Member Author

arvind commented Oct 11, 2018

After investigating this issue, I suspect there may be a bug in Vega (vega/vega#1428) where signals continue to listen for scale updates even if they're marked as react: false.

@arvind arvind force-pushed the as/initSelections branch from 130a791 to a0f4bc9 Compare October 12, 2018 15:35
@arvind
Copy link
Member Author

arvind commented Oct 12, 2018

Hooray, with Vega 4.3, this PR is no longer blocked and ready for review as well @domoritz, @kanitw! And I've verified that the infinite loop bug observed/discussed above has also been fixed in Vega (and doesn't require any changes in Vega-Lite).

@arvind arvind removed the Blocked 🕐 For issues that are blocked by other issues label Oct 12, 2018
@arvind arvind changed the title [WIP] Initialize Selections Initialize Selections Oct 12, 2018
@arvind arvind force-pushed the as/initSelections branch from a0f4bc9 to 018fea0 Compare October 12, 2018 20:50
Copy link
Member

@domoritz domoritz left a comment

Choose a reason for hiding this comment

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

Please update the documentation as well.

I'll make a full review after #4068 has been merged.

@arvind arvind force-pushed the as/initSelections branch 3 times, most recently from 9a54954 to d787382 Compare October 13, 2018 19:12
@arvind
Copy link
Member Author

arvind commented Oct 13, 2018

@domoritz: As there'll be several documentation updates related to all the new selections work, I wanted to do all of it in a separate more comphreneisve pass/PR rather than piecemeal if that's ok.

@domoritz
Copy link
Member

That makes sense. If you think this will take more than a week, please create an issue that describes what needs to be documented.

@domoritz
Copy link
Member

Does this PR support initialization of selections with dates? What about booleans? I think it would be great if we supported the same types that are supported as values in encodings.

@@ -4,7 +4,8 @@
"data": {"url": "data/cars.json"},
"selection": {
"brush": {
"type": "interval"
"type": "interval",
"init": {"x": [55, 160], "y": [13, 37]}
Copy link
Member

Choose a reason for hiding this comment

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

@domoritz
Copy link
Member

Let me know when you get the chance to update the PR. I'd like to make a release soon so we can test these changes thoroughly.

@arvind arvind added the Blocked 🕐 For issues that are blocked by other issues label Oct 17, 2018
@arvind arvind force-pushed the as/initSelections branch from 59101af to 6914fe2 Compare October 17, 2018 20:59
@arvind
Copy link
Member Author

arvind commented Oct 17, 2018

Supporting datetime objects proved trickier than I expected since it involves juggling expression strings with regular JSON primitives. But, I think I got it working. And also extended initialization to support initializing multi selections with an array of values.

Unfortunately, merging this issue is blocked by three issues/PRs in Vega:

  1. Signal scale updates on initialization vega#1433 which results in a bug where the brush mark doesn't appear for initialized interval selections (introduced, ironically, when React: false continues to listen for scale updates vega#1428 fixed our infinite loop bug above)
  2. Initialize a bound signal with expression vega#1435 which results in a bug when initializing a bound selection (introduced as datetimes can currently only be initialized via an expression function in update)
  3. Fix identity typing vega-util#10 which provides correct typings for the identity function, and without which the CI will not run succesfully.

@domoritz
Copy link
Member

Okay, let's wait for a Vega release then. Luckily, no other PR is blocked by this and we can make a new Vega-Lite rc release without this PR.

@eharkins
Copy link

eharkins commented Nov 1, 2018

To clarify, the solution involving the init property, that will solve problems like vega/vega#1435, will also be added to Vega in addition to Vega-Lite once you have a chance to merge this and/or a Vega version of the same PR? Looking forward to having this functionality in Vega! Thanks

kanitw
kanitw previously approved these changes Feb 23, 2019
@kanitw kanitw requested a review from domoritz February 23, 2019 17:40
@kanitw
Copy link
Member

kanitw commented Feb 23, 2019

I just resolved conflicts and clean up the typings (while trying to preserve the logic).

@domoritz and @arvind -- can one of you double check so we can merge this for 3.0?

@kanitw kanitw force-pushed the as/initSelections branch 2 times, most recently from 763fce2 to 835e440 Compare February 23, 2019 17:50
if (isIntervalSelection(selDef)) {
selCmpt.init = parseInit(selDef.init);
} else {
const init = isArray(selDef.init) ? selDef.init : [selDef.init];
Copy link
Member

@kanitw kanitw Feb 23, 2019

Choose a reason for hiding this comment

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

We could use array again here once we have vega/vega#1618 in, but I'd say let's just merge this.

Copy link
Member

Choose a reason for hiding this comment

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

That relies on the Vega 5 transition to let's do it later.

@kanitw kanitw dismissed stale reviews from domoritz and themself February 23, 2019 19:38

Need another review.

@domoritz
Copy link
Member

Not sure why the yarn lockfile needs to change but okay.

@kanitw
Copy link
Member

kanitw commented Feb 23, 2019

Not sure why the yarn lockfile needs to change but okay.

Good catch! I think that's a result of auto-conflict merge. I'll remove it.

@kanitw kanitw merged commit 07fcab9 into master Feb 23, 2019
@kanitw kanitw deleted the as/initSelections branch February 23, 2019 22:53
@kanitw
Copy link
Member

kanitw commented Feb 23, 2019

🎉

const str = init.map(v => assembleInit(v, wrap)).join(', ');
return `[${str}]`;
} else if (isDateTime(init)) {
return wrap(dateTimeExpr(init));
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be dateTimeExpr(init, true)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants