Plot working in dev environment but not once deployed to netlify.

Hello,
I’m working on a project that’s using D3 and Plot. When I deploy the project to netlify and it builds, all the D3 visualisations work fine but the ones using Plot do not appear. There’s no error in the console and I’ve checked the json files paths multiple times. In the build log files there aren’t any errors with installing the package. Is there something I’m missing?

Thanks

Below is the Plot code I’m working on:

import * as d3 from "d3";
import * as Plot from "@observablehq/plot";

document.addEventListener("DOMContentLoaded", function() {
    d3.json("/city-density.json?url").then(function(cityDensity){
        // show only top 20 cities
        const topCities = cityDensity.slice(0, 20);
        
        const plotFirst = Plot.plot({
            style: {
                backgroundColor: "#f8f8ec",
                fontSize: "16"
            },
            y: {
                grid: true,
            },
            marks: [
                Plot.ruleY([0]),
                Plot.barY(topCities, { x: "Entity", y: "Population density by city", sort: { x: "y", reverse: false }, fill:"#fa7f5e", tip:true }),
                Plot.axisX({anchor: "bottom", label: null, tickRotate: -45}),
                Plot.axisY({label: "Population density by city/km2"})
            ],
        });
    
        // append chart to div element
        const chart1 = document.getElementById("migrationChart1");
        chart1.append(plotFirst);
    
    })

    d3.json("/urban-rural.json?url").then(function(urbanRural){
        const plotSecond = Plot.plot({
            marginBottom: 80,
            color: { legend: true },
            style: {
                backgroundColor: "#f8f8ec",
                fontSize: "16"
            },
            y: {
                label: "World population living in rural or urban areas",
            },
            x: {
                grid: true,
                tickFormat: "d"
            },
            marks: [
                Plot.lineY(urbanRural, {x: "Year", y: "Urban population", stroke: "Red", strokeWidth: 2.5}),
                Plot.lineY(urbanRural, {x: "Year", y: "Rural population", stroke: "Green", strokeWidth: 2.5}),
                Plot.text(urbanRural, Plot.selectFirst({x: "Year", y: "Urban population", text: ["Urban"], textAnchor: "start", dx: 20, dy: -40})),
                Plot.text(urbanRural, Plot.selectFirst({x: "Year", y: "Rural population", text: ["Rural"], textAnchor: "start", dx: 20, dy: -40})),
                Plot.crosshairY(urbanRural, {y: "Urban population"}),
            ]
        })
    
        // append chart to div element
        const chart2 = document.getElementById("migrationChart2");
        chart2.append(plotSecond);
    
    })
});

Can you share a link to the page?

Sure - https://cozy-crisp-645170.netlify.app/

The plot components are supposed to appear underneath the header " Homesteads to Highrises".

This is what it looks like when I run the project locally -

I see a synchronous error which causes the remainder of the script to fail (I think?):

index-d5ed1135.js:3554 Uncaught TypeError: Cannot read properties of undefined (reading 'rotation')
    at dM (index-d5ed1135.js:3554:205715)
1 Like

Amazing that was it! I fixed that issue so the error caused by the Three.js code was stoping the rest of the other scripts running? I wouldn’t have guessed that.

Thanks so much! :raised_hands:

2 Likes