Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0125-preview· openaiHugging Face Suzhen/CodeChat

<script type="text/javascript"> var body = d3.select("body");…

  1. User

    [Error] Error: Invalid value for attribute r="NaN"

    (anonymous function) (d3.v7.min.js:2:18259)
    each (d3.v7.min.js:2:25665)
    Global Code (ej4.html:39)
    
  2. Assistant

    The error you're encountering, Invalid value for <circle> attribute r="NaN', occurs because of how you are attempting to set the radius (r) of the circles in the SVG. This is happening due to the misunderstanding of the data structure you have for drawing the circles compared to what you're actually providing.

    Your dataset (dataset) consists of single values ([10,20,30,40,43,67,89]), but in your circle drawing code, you're trying to access them as if they were points with x and y coordinates (d[0] for x, and d[1] for y). This misalignment leads to undefined values when accessing d[1], and subsequently, when trying to compute the radius, JavaScript cannot perform Math.sqrt(h - d[1]) on undefined, which results in NaN.

    Let's fix the relevant parts of your script:

    Correcting Circle Radius Logic

    If your intent is to draw circles using single value data points as diameters or something similar, then you don't need to access d[0] and d[1]. You would adjust the logic to directly use d for whatever purposes you intend, e.g., setting their positions or sizes.

    However, if you mean to use the datasetAleatorio for the circles, which actually contains pairs of values, you need to update the part of your code that binds the dataset to the circles.

    Also, you have to adjust the positions (cx, and cy) and the radius (r) calculation logic accordingly. Assuming you want each circle's center to be its datum's coordinates and its radius to be a constant or a function of its data.

    Example of Fixed Circle Drawing Logic

    Let's correct the logic under the assumption you want to place circles based on the datasetAleatorio:

    // Assuming datasetAleatorio contains points like [[x1, y1], [x2, y2], ...]
    var svg = d3.select("body")
                .append("svg")
                .attr("width", w)
                .attr("height", h);
    
    svg.selectAll("circle")
       .data(datasetAleatorio) // Make sure to use the dataset intended for drawing circles
       .enter()
       .append("circle")
       .attr("cx", function(d) {
            return d[0]; // Assume first value is x-coordinate
       })
       .attr("cy", function(d) {
            return d[1]; // Assume second value is y-coordinate
       })
       .attr("r", 10); // Set a constant radius or use a dynamic value based on `d`
    

    This code assumes that datasetAleatorio contains your desired x and y values for the circle positions. You also need to ensure that you run your generarDataset function before the SVG drawing logic, which appears you do based on the placement of the console.log statement.

    Ensure that any dataset you're using for coordinates complies with the expected data structure for the operations you're performing on them.