# D3 Scatterplot Graph - Demand Graph

**URL:** <https://forum.freecodecamp.org/t/d3-scatterplot-graph-demand-graph/822771>\
**Category:** I Built This\
**Tags:** functions, d3, front-end\
**Created:** [November 24, 2026, 10:27pm UTC](https://forum.freecodecamp.org/t/d3-scatterplot-graph-demand-graph/822771 "2026-11-24T22:27:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CasualWanderer20XX](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/casualwanderer20xx/32/387661_2.png) [@CasualWanderer20XX](https://forum.freecodecamp.org/u/CasualWanderer20XX)\
**Post date:** [November 24, 2026, 10:27pm UTC](https://forum.freecodecamp.org/t/d3-scatterplot-graph-demand-graph/822771/1 "2026-11-24T22:27:14Z")

</div>

> **[D3 Project - Scatterplot Graph (11/24/26)](https://codepen.io/editor/CasualWanderer20XX/pen/01a20b1f-d4c7-7d0a-9442-321e5695146a)**
>
> A code demo by CasualWanderer20XX created on CodePen

This is a Scatterplot graph.

Using functions to break my code down made this SO MUCH easier to manage.

I’m hoping to figure out how to make a best-fit curve on this graph.

It’s supposed to be a Demand Curve (I’m currently reading an Economics Textbook for fun).

---

<div class="post-metadata">

**Author:** ![SikhamiLearn](https://avatars.discourse-cdn.com/v4/letter/s/8491ac/32.png) [@SikhamiLearn](https://forum.freecodecamp.org/u/SikhamiLearn)\
**Post date:** [November 26, 2026, 7:38am UTC](https://forum.freecodecamp.org/t/d3-scatterplot-graph-demand-graph/822771/2 "2026-11-26T07:38:31Z")

</div>

Nice work, and breaking the code into small functions really does pay off. For the best-fit curve, one approach is to fit the line yourself. Demand data often looks roughly like a power curve, so take the natural log of both price and quantity, run a simple least squares fit on those logged values to get a slope and intercept, then convert back. Generate maybe 50 evenly spaced x values across your domain, compute the fitted y for each, and feed those points to d3.line() with your existing scales. Draw it as a path with fill set to none. If a straight line is enough, the same idea works without the logs. Check that your axes are not clipping the curve at the low end, since power curves climb fast there.

---

<div class="post-metadata">

**Author:** ![CasualWanderer20XX](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/casualwanderer20xx/32/387661_2.png) [@CasualWanderer20XX](https://forum.freecodecamp.org/u/CasualWanderer20XX)\
**Post date:** [November 27, 2026, 9:24pm UTC](https://forum.freecodecamp.org/t/d3-scatterplot-graph-demand-graph/822771/3 "2026-11-27T21:24:02Z")

</div>

I’ll look through d3 line and see how that works.

In the meantime, I just went and did the calculations for the d property fo the path by hand (with help of AI to find the Least Squares Regression equation).

---

<div class="post-metadata">

**Author:** ![CasualWanderer20XX](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/casualwanderer20xx/32/387661_2.png) [@CasualWanderer20XX](https://forum.freecodecamp.org/u/CasualWanderer20XX)\
**Post date:** [November 27, 2026, 10:04pm UTC](https://forum.freecodecamp.org/t/d3-scatterplot-graph-demand-graph/822771/4 "2026-11-27T22:04:31Z")

</div>

Also, I converted my code to TypeScript (gotta give my flowers to CoPilot for the assistance).
