D3.js - 不显示折线图

时间:2017-07-19 05:29:01

标签: d3.js linechart

我修改了互联网上的一些代码,根据数组中包含的数据绘制折线图"数据"。

  <!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>

但令人惊讶的是,它没有绘制图表。我的意思是,当我执行应用程序时,没有显示任何内容。我是d3.js的新手,所以请参考互联网上提供的资料。

我想要绘制的内容如下所示。

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

但是数据应该来自数组&#34;数据&#34; ,而不是文件。

有人可以帮忙弄清问题是什么吗?

2 个答案:

答案 0 :(得分:0)

 <!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>

希望它对你有所帮助。

答案 1 :(得分:-1)

你的html中没有

svg元素,所以d3.select('svg')返回空选择。请将<svg width="960" height="500"></svg>放入HTML文件中。