You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

D3.js简单散点图无输出,控制台报SyntaxError错误求助

Hey there! Let's work through this D3.js issue together—that SyntaxError: expected expression, got '<' is way more common than you might think, and it usually doesn't stem from the code you wrote directly. Let's break down the most likely fixes:

Common Causes & Fixes

  • Incorrect D3.js script inclusion
    This is the #1 culprit for this error. The < in the error message means your browser is trying to parse non-JavaScript content (like an HTML file's opening <!DOCTYPE html> tag) as JS. That happens when your <script> tag points to the wrong resource—for example, linking to your own HTML file instead of the D3 library, or using a broken CDN link.
    Fix this by making sure you include D3 correctly before your custom code. Use the official CDN for reliability:

    <script src="https://d3js.org/d3.v7.min.js"></script>
    
  • Truncated code in your example
    You cut off the scaleX definition at .ra...—while this isn't causing the current syntax error, once you fix the script inclusion, you'll need to complete this line to avoid new issues. For example, a proper linear scale might look like:

    var scaleX = d3.scaleLinear()
      .domain([1, 3]) // Matches the min/max x values in your data
      .range([20, 230]); // Maps to your SVG width with padding
    
  • Local file loading quirks
    If you're opening your HTML file directly via file:// (double-clicking it), browser security restrictions can sometimes cause unexpected resource loading issues. If the script fix doesn't work, try serving the file via a local web server (like using python -m http.server in your project folder) to rule this out.

Full Working Example

Here's a complete, runnable version of your code to test:

<!DOCTYPE html>
<html>
<head>
    <title>D3 Circle Test</title>
    <script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
    <script>
        var xyr = [ {x:1,y:1,r:1}, {x:2,y:2,r:2}, {x:3,y:3,r:3} ]; 
        var body = d3.select("body"); 
        var svg = body.append("svg").attr("width",250).attr("height",250); 
        
        // Complete scale definition
        var scaleX = d3.scaleLinear()
            .domain([1, 3])
            .range([20, 230]);
        
        // Add circles to the SVG
        svg.selectAll("circle")
            .data(xyr)
            .enter()
            .append("circle")
            .attr("cx", d => scaleX(d.x))
            .attr("cy", d => d.y * 50)
            .attr("r", d => d.r * 10)
            .attr("fill", "steelblue");
    </script>
</body>
</html>

Content of the question comes from stack exchange, question author Subhan Asadli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:36:18