D3.js - line graph is not displayed

二次信任 提交于 2019-12-24 19:23:33

问题


I have modified some code available on the internet to draw a line chart based on the data contained in the array "data".

  <!doctype html>
  <html>
  <head>
    <title></title>
  </head>
  <body>
      <style>
            .chart-line {
                fill: none;
                stroke : #0ff;
                shape-rendering: crispEdges;
            }
        </style>
        <script src="https://d3js.org/d3.v4.min.js"> </script>
        <script>

           var svg = d3.select("svg"),
                      margin = { top: 20, right: 20, bottom: 30, left: 50 },
                      width = +svg.attr("width") - margin.left - margin.right,
                      height = +svg.attr("height") - margin.top - margin.bottom,
                      g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");

           var parseTime = d3.timeParse("%d-%b-%y");

           var x = d3.scaleTime()
               .rangeRound([0, width]);

           var y = d3.scaleLinear()
               .rangeRound([height, 0]);

           var line = d3.line()
               .x(function (d) { return x(d.date); })
               .y(function (d) { return y(d.close); });


           var data = [
                         { letter: "A", frequency: .08167 },
                         { letter: "B", frequency: .01492 },
                         { letter: "C", frequency: .02780 },
                         { letter: "D", frequency: .04253 },
                         { letter: "E", frequency: .12702 },
                         { letter: "F", frequency: .02288 },
                         { letter: "G", frequency: .02022 },
                         { letter: "H", frequency: .06094 },
                         { letter: "I", frequency: .06973 },
                         { letter: "J", frequency: .00153 },
                         { letter: "K", frequency: .00747 },
                         { letter: "L", frequency: .04025 },
                         { letter: "M", frequency: .02517 },
                         { letter: "N", frequency: .06749 },
                         { letter: "O", frequency: .07507 },
                         { letter: "P", frequency: .01929 },
                         { letter: "Q", frequency: .00098 },
                         { letter: "R", frequency: .05987 },
                         { letter: "S", frequency: .06333 },
                         { letter: "T", frequency: .09056 },
                         { letter: "U", frequency: .02758 },
                         { letter: "V", frequency: .01037 },
                         { letter: "W", frequency: .02465 },
                         { letter: "X", frequency: .00150 },
                         { letter: "Y", frequency: .01971 },
                         { letter: "Z", frequency: .00074 }
           ];


           x.domain(data.map(function (d) { return d.letter; }));
           y.domain([0, d3.max(data, function (d) { return d.frequency; 
         })]);
           g.append("g")
                        .attr("transform", "translate(0," + height + ")")
                        .call(d3.axisBottom(x))
                        .select(".domain")
                        .remove();

           g.append("g")
               .call(d3.axisLeft(y))
               .append("text")
               .attr("fill", "#000")
               .attr("transform", "rotate(-90)")
               .attr("y", 6)
               .attr("dy", "0.71em")
               .attr("text-anchor", "end")
               .text("Price ($)");

           g.append("path")
             .datum(data)
             .attr("fill", "none")
             .attr("stroke", "steelblue")
             .attr("stroke-linejoin", "round")
             .attr("stroke-linecap", "round")
             .attr("stroke-width", 1.5)
             .attr("d", line);

        </script>

But surprisingly, it does not draw the chart. I mean, when I execute the application, there is nothing displayed. I am new to d3.js so referring to sources available on the internet.

What I want to draw is something like in the following.

https://bl.ocks.org/mbostock/3883245

But the data should be taken from the array "data" , not from a file.

Can someoone pls help to figure out what the issue is ?


回答1:


 <!DOCTYPE html>
  <html>
   <head>
<meta charset="UTF-8">
<title>title</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
.bar {
  fill: steelblue;
}
.bar:hover {
  fill: brown;
}
.axis--x path {
  display: none;
}
.line {
  fill: none;
  stroke: steelblue;
  stroke-width: 3px;
}
.circle {
  fill: steelblue;
}
 </style>
   </head>
  <body>
<svg width="960" height="500"></svg>

  <script>
      var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;

var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

 var data = [
                     { letter: "A", frequency: .08167 },
                     { letter: "B", frequency: .01492 },
                     { letter: "C", frequency: .02780 },
                     { letter: "D", frequency: .04253 },
                     { letter: "E", frequency: .12702 },
                     { letter: "F", frequency: .02288 },
                     { letter: "G", frequency: .02022 },
                     { letter: "H", frequency: .06094 },
                     { letter: "I", frequency: .06973 },
                     { letter: "J", frequency: .00153 },
                     { letter: "K", frequency: .00747 },
                     { letter: "L", frequency: .04025 },
                     { letter: "M", frequency: .02517 },
                     { letter: "N", frequency: .06749 },
                     { letter: "O", frequency: .07507 },
                     { letter: "P", frequency: .01929 },
                     { letter: "Q", frequency: .00098 },
                     { letter: "R", frequency: .05987 },
                     { letter: "S", frequency: .06333 },
                     { letter: "T", frequency: .09056 },
                     { letter: "U", frequency: .02758 },
                     { letter: "V", frequency: .01037 },
                     { letter: "W", frequency: .02465 },
                     { letter: "X", frequency: .00150 },
                     { letter: "Y", frequency: .01971 },
                     { letter: "Z", frequency: .00074 }
       ];
   var line = d3.line()
 .x(function(d) { return x(d.letter); })
  .y(function(d) { return y(d.frequency); })

   x.domain(data.map(function(d) { return d.letter; }));

  y.domain([0, d3.max(data, function(d) { return d.frequency; })]);

 g.append("g")
  .attr("class", "axis axis--x")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.axisBottom(x));

 g.append("g")
  .attr("class", "axis axis--y")
  .call(d3.axisLeft(y).ticks(10, "%"))
 .append("text")
  .attr("transform", "rotate(-90)")
  .attr("y", 6)
  .attr("dy", "0.71em")
  .attr("text-anchor", "end")
  .text("Frequency");
 g.append("path")
 .datum(data)
 .attr("class", "line")
 .attr("d", line);

 g.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("class", "circle")
.attr("cx", function(d) { return x(d.letter); })
.attr("cy", function(d) { return y(d.frequency); })
.attr("r", 4);

  </script>
 </body>
 </html>

Hope it helps you.



来源:https://stackoverflow.com/questions/45181647/d3-js-line-graph-is-not-displayed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!