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).
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.
1 Like
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).
Also, I converted my code to TypeScript (gotta give my flowers to CoPilot for the assistance).