observablehq / observablehq/plot

Weighted sum?

Open
#914 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
HTML
Stars
5.4k
Forks
244
PR merge metrics
No merged PRs in 30d

Description

It’d be nice to have a weighted sum reducer. This would require two channels as input (the values, and the weights), so I’m not sure how we would specify it. As a workaround, you can write a custom reducer, assuming that your data is indexable:

Plot.ruleX(
  lockdown,
  Plot.groupZ(
    {
      x: {
        reduce(I, X) {
          return (
            d3.sum(I, (i) => lockdown[i].pop * X[i]) /
            d3.sum(I, (i) => lockdown[i].pop)
          );
        }
      }
    },
    { x: "pct_change" }
  )
)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Plot.groupZ reducer API and the custom reduce workaround shown in the issue, including its use of d3.sum. Define how a weighted sum reducer would receive values and weights, then implement and document the chosen interface; done means weighted aggregation works without the custom reducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
d3js, javascript
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.