# Fix "d3.scale.linear is not a function" after upgrading D3 past version 3

Yes. An agent maps every nested D3 3 call in the chart to its flat D3 4 and later name, fixes the data join that changed with it, and rebuilds the chart.

[Run this on Rush](https://cloud.getrush.ai/run/fix-d3-scale-linear-is-not-a-function/d3/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent inventories the calls before changing the dependency. In a chart component like tj/d3-heatmap, `package.json` declares `"d3": "^3.5.16"` and `index.js` imports it once and uses it in eight ways: `d3.scale.linear` four times (the x, y, opacity and colour scales at lines 129 to 139), `d3.svg.axis` twice (lines 145 and 152, each with `.orient('bottom')` or `.orient('left')` plus `ticks`, `tickPadding`, `tickSize` and `tickFormat`), `d3.time.scale` once (line 126, when `axisType` is `time`), and `d3.select`, `d3.interpolateHcl`, `d3.min`, `d3.max` and `d3.extent`. The build is `make build`, which runs Babel over `index.js` into `build/index.js` through the Makefile pulled in from `react-fatigue-dev`; there is no test suite, and `example/index.js` is a small React page that renders two charts.

Then it reproduces on the new major and names the message precisely. With D3 7 installed, `d3.scale` and `d3.time` are not defined, so the first scale line throws `TypeError: Cannot read properties of undefined (reading 'linear')` under V8, the wording the search box usually gets paraphrased into. D3's own 4.0 changelog explains it: "every symbol in D3 4.0 now shares a flat namespace rather than the nested one of D3 3.x. For example, d3.scale.linear is now d3.scaleLinear", and "In place of d3.svg.axis and axis.orient, D3 4.0 now provides four constructors for each orientation: d3.axisTop, d3.axisRight, d3.axisBottom, d3.axisLeft. These constructors accept a scale". The agent checks each replacement exists on the installed build: `scaleLinear`, `scaleTime`, `axisBottom`, `axisLeft`, `interpolateHcl`, `select`, `min`, `max` and `extent` all do.

The port is mechanical for the scales and axes and not for the join. `d3.scale.linear()` becomes `d3.scaleLinear()`, `d3.time.scale()` becomes `d3.scaleTime()`, and `d3.svg.axis().orient('bottom').scale(this.x)` becomes `d3.axisBottom(this.x)` with the same `ticks`, `tickPadding`, `tickSize` and `tickFormat` chained on. The trap is in `renderBuckets`: the code does `col.enter().append('g')` and then sets `transform` on `col`, the update selection, which in 3.x included the entered nodes. The same changelog says "selection.append no longer merges entering nodes into the update selection; use selection.merge to combine enter and update after a data join", so without a `.merge()` every new column would be appended with no position. The agent rewrites that join with `merge` and keeps the exit branch.

Proof without a test suite is a rendered chart. The agent runs `make build` on the new major, then loads the example page's two charts in the sandbox's browser with the same generated data as before and after, and compares the counts of `.column` groups and bins and the axis tick labels, attaching both screenshots. It leaves the `interpolate: 'basis'` default and the React example alone, and lists `react-fatigue-dev`, pinned to a commit on GitHub, as the build dependency the maintainer may also want to look at.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Our chart throws \"d3.scale.linear is not a function\" (or `Cannot read properties of undefined (reading 'linear')`) after upgrading D3 past version 3. List every d3.* call in the chart code with its line, map each nested D3 3 name to its flat D3 4+ name, and port them, including axes built with d3.svg.axis and orient. Check every data join for enter/update code that relied on D3 3 merging entered nodes, and add merge where needed. Build the component, render the example before and after with the same data, compare the rendered elements, and open a pull request with the screenshots and the mapping table.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes `index.js` and `package.json` only: the four scale calls, the time scale, the two axes, and the column join with `merge`, plus `"d3"` moved to the current major. The description carries a table of each old name, its line and its new name, quotes the two changelog sentences that justify the rename and the join change, and attaches before and after screenshots of the example page with the element counts the agent compared.

A reviewer checks the join rewrite most closely, since it is the one change that would fail silently rather than throw, confirms the tick formatting options survived on the new axis constructors, and decides whether to keep the Babel build as it is.

## Run it on every Dependabot pull request that bumps d3

The break lands with the major bump, so the trigger is the pull request that moves D3 past 3. An automation with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` runs this prompt on each one, with the event appended after the prompt, and pushes the port onto the bump's branch when the build or render fails on `d3.scale`.

For many internal charts still on D3 3, your server sends this request once per repository, each with its own UUID as `request_id` so a retry returns the run already started, and reads each `run.completed` webhook; a key starts at most `max_concurrent` runs at once and queues the rest.

## Why run it on Rush?

The rename is the easy half of a D3 3 to 4 port; the data-join change is the half that renders wrong without an error. In a Rush sandbox the agent builds the component, draws the example in a real browser before and after, and keeps the screenshots and element counts, so a reviewer sees the chart rather than trusting a search-and-replace.

The changelog that explains each change is long and the agent quotes the exact sentences for the calls this chart uses, nothing more. The pull request is a mapping table with evidence, which is what a port of a chart library needs to be reviewable.

## Frequently asked questions

### Why does my browser say `Cannot read properties of undefined` instead of `is not a function`?

Because `d3.scale` itself no longer exists on D3 4 and later, so reading `.linear` on it fails before any call. Different engines word that differently; the cause and the fix are the same, `d3.scaleLinear`.

### What replaces d3.svg.axis().orient('bottom')?

`d3.axisBottom(scale)`, with `d3.axisLeft`, `d3.axisTop` and `d3.axisRight` for the other orientations. The constructors take the scale directly, and `ticks`, `tickSize`, `tickPadding` and `tickFormat` chain on as before.

### Why does the chart render empty columns after the rename?

Because D3 4 stopped merging entered nodes into the update selection. Code that appends in `enter()` and then sets attributes on the update selection positions only the nodes that already existed. The agent adds `.merge()` after the enter append so the attribute calls reach both.

### There are no tests. How is the port checked?

By rendering. The agent builds the component, loads the example page with the same generated data on the old and new D3, counts the column groups, bins and axis ticks, and attaches both screenshots to the pull request.

## More D3 jobs

- [Fix d3.event Is Undefined After Upgrading D3](https://getrush.ai/run/fix-d3-event-removed/d3/)

## More jobs like this: Fix a bug from an alert

- [Fix "database is locked" Errors in a GORM and SQLite App](https://getrush.ai/run/fix-database-is-locked/gorm/)
- [Fix an ASP.NET Core App Bug From a Datadog Monitor Alert](https://getrush.ai/run/fix-datadog-alerts/aspnet-core/)
- [Fix a Django App Bug From a Datadog Monitor Alert](https://getrush.ai/run/fix-datadog-alerts/django/)
- [Fix an Express API Bug From a Datadog Monitor Alert](https://getrush.ai/run/fix-datadog-alerts/express/)
- [Fix a FastAPI App Bug From a Datadog Monitor Alert](https://getrush.ai/run/fix-datadog-alerts/fastapi/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
