为什么无法显示一个d3图表?

huangapple go评论65阅读模式
英文:

Why am I unable to display a d3 plot?

问题

I am unable to display a D3 plot on a web-page.

I am able to display the plot when I select <body>. However, I cannot display a plot when I select a, say, <div>.

The following is my setting:

为什么无法显示一个d3图表?

plot.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <title>Protein Structure Analyzer</title>
        <script type="text/javascript" src="../static/jquery/jquery-3.6.3.js"></script>
        <script type="text/javascript" src="../static/d3/d3.js"></script>
        <script type="text/javascript" src="../static/d3/d3.min.js"></script>
    </head>
    <body>
    <script type="text/javascript">
    // set the dimensions and margins of the graph
    var margin = {top: 10, right: 40, bottom: 30, left: 30},
        width = 450 - margin.left - margin.right,
        height = 400 - margin.top - margin.bottom;

    // append the svg object to the body of the page
    var svG = d3.select("#plot-div")
      .append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
      .append("g")
        .attr("transform",
              "translate(" + margin.left + "," + margin.top + ")");
    
    // Create data
    var data = [ {x:10, y:20}, {x:40, y:90}, {x:80, y:50} ]
    
    // X scale and Axis
    var x = d3.scaleLinear()
        .domain([0, 100])         // This is the min and the max of the data: 0 to 100 if percentages
        .range([0, width]);       // This is the corresponding value I want in Pixel
    svG
      .append('g')
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x));
    
    // X scale and Axis
    var y = d3.scaleLinear()
        .domain([0, 100])         // This is the min and the max of the data: 0 to 100 if percentages
        .range([height, 0]);       // This is the corresponding value I want in Pixel
    svG
      .append('g')
      .call(d3.axisLeft(y));
    
    // Add 3 dots for 0, 50 and 100%
    svG
      .selectAll("whatever")
      .data(data)
      .enter()
      .append("circle")
        .attr("cx", function(d){ return x(d.x) })
        .attr("cy", function(d){ return y(d.y) })
        .attr("r", 7)
    </script>
    <div id="plot-div"></div>
    </body>
</html>

What am I doing incorrectly?

英文:

I am unable to display a D3 plot on a web-page.

I am able to display the plot when I select &lt;body&gt;. However, I cannot display a plot when I select a, say, &lt;div&gt;.

The following is my setting:

为什么无法显示一个d3图表?

plot.html

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Protein Structure Analyzer&lt;/title&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;../static/jquery/jquery-3.6.3.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;../static/d3/d3.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;../static/d3/d3.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
// set the dimensions and margins of the graph
var margin = {top: 10, right: 40, bottom: 30, left: 30},
width = 450 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svG = d3.select(&quot;#plot-div&quot;)
.append(&quot;svg&quot;)
.attr(&quot;width&quot;, width + margin.left + margin.right)
.attr(&quot;height&quot;, height + margin.top + margin.bottom)
.append(&quot;g&quot;)
.attr(&quot;transform&quot;,
&quot;translate(&quot; + margin.left + &quot;,&quot; + margin.top + &quot;)&quot;);
// Create data
var data = [ {x:10, y:20}, {x:40, y:90}, {x:80, y:50} ]
// X scale and Axis
var x = d3.scaleLinear()
.domain([0, 100])         // This is the min and the max of the data: 0 to 100 if percentages
.range([0, width]);       // This is the corresponding value I want in Pixel
svG
.append(&#39;g&#39;)
.attr(&quot;transform&quot;, &quot;translate(0,&quot; + height + &quot;)&quot;)
.call(d3.axisBottom(x));
// X scale and Axis
var y = d3.scaleLinear()
.domain([0, 100])         // This is the min and the max of the data: 0 to 100 if percentages
.range([height, 0]);       // This is the corresponding value I want in Pixel
svG
.append(&#39;g&#39;)
.call(d3.axisLeft(y));
// Add 3 dots for 0, 50 and 100%
svG
.selectAll(&quot;whatever&quot;)
.data(data)
.enter()
.append(&quot;circle&quot;)
.attr(&quot;cx&quot;, function(d){ return x(d.x) })
.attr(&quot;cy&quot;, function(d){ return y(d.y) })
.attr(&quot;r&quot;, 7)
&lt;/script&gt;
&lt;div id=&quot;plot-div&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

What am I doing incorrectly?

答案1

得分: 1

以下是翻译好的部分,代码部分不翻译:

"The DIV needs to be in the document before the script runs so just move the DIV to the top of the document:"

"DIV 必须在脚本运行之前出现在文档中,所以将 DIV 移动到文档的顶部:"

"<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>"
"<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>"
"<div id="plot-div"></div>"

请注意,这是原始HTML代码中的一部分,已经保留原始的HTML实体编码。

英文:

The DIV needs to be in the document before the script runs so just move the DIV to the top of the document:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// set the dimensions and margins of the graph
var margin = {top: 10, right: 40, bottom: 30, left: 30},
width = 450 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svG = d3.select(&quot;#plot-div&quot;)
.append(&quot;svg&quot;)
.attr(&quot;width&quot;, width + margin.left + margin.right)
.attr(&quot;height&quot;, height + margin.top + margin.bottom)
.append(&quot;g&quot;)
.attr(&quot;transform&quot;,
&quot;translate(&quot; + margin.left + &quot;,&quot; + margin.top + &quot;)&quot;);
// Create data
var data = [ {x:10, y:20}, {x:40, y:90}, {x:80, y:50} ]
// X scale and Axis
var x = d3.scaleLinear()
.domain([0, 100])         // This is the min and the max of the data: 0 to 100 if percentages
.range([0, width]);       // This is the corresponding value I want in Pixel
svG
.append(&#39;g&#39;)
.attr(&quot;transform&quot;, &quot;translate(0,&quot; + height + &quot;)&quot;)
.call(d3.axisBottom(x));
// X scale and Axis
var y = d3.scaleLinear()
.domain([0, 100])         // This is the min and the max of the data: 0 to 100 if percentages
.range([height, 0]);       // This is the corresponding value I want in Pixel
svG
.append(&#39;g&#39;)
.call(d3.axisLeft(y));
// Add 3 dots for 0, 50 and 100%
svG
.selectAll(&quot;whatever&quot;)
.data(data)
.enter()
.append(&quot;circle&quot;)
.attr(&quot;cx&quot;, function(d){ return x(d.x) })
.attr(&quot;cy&quot;, function(d){ return y(d.y) })
.attr(&quot;r&quot;, 7)

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.3.min.js&quot; integrity=&quot;sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;https://d3js.org/d3.v7.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;plot-div&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 02:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定