【问题标题】:Uncaught TypeError: Cannot read property 'linear' of undefined未捕获的类型错误:无法读取未定义的属性“线性”
【发布时间】:2016-11-21 20:46:28
【问题描述】:

我是 D3 新手,在我的演示脚本中遇到以下错误 -

FirstD3.jsp:31 Uncaught TypeError: Cannot read property 'linear' of undefined

我的演示代码如下

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<title>Linear Scales</title>
<style type="text/css">

</style>
</head>
<body>
<script type="text/javascript">
var dataset = [
           [ 5,     20 ],
           [ 460,   90 ],
           [ 250,   50 ],
           [ 100,   33 ],
           [ 330,   95 ],
           [ 410,   12 ],
           [ 468,   44 ],
           [ 25,    67 ],
           [ 85,    21 ],
           [ 220,   88 ]
       ];
var w = 500;
var h = 100;


var xScale = d3.scale.linear()
        .domain([0, d3.max(dataset, function(d) { return d[0]; })])
        .range([0, w]);

var yScale = d3.scale.linear()
        .domain([0, d3.max(dataset, function(d) { return d[1]; })])
        .range([0, h]);

var svg = d3.select("body")
      .append("svg")
      .attr("height", h)
      .attr("width", w);

      svg.selectAll("circle")
         .data(dataset)
         .enter()
         .append("circle")
         .attr("cx", function (d) {
             return xScale(d[0]);
         })
         .attr("cy", function(d) {
             return yScale(d[1]);
         })
         .attr("r", function (d) {
             return Math.sqrt(h-(d[1]));
         });

      svg.selectAll("text")
         .data(dataset)
         .enter()
         .append("text")
         .text(function (d) {
             return d[0]+","+d[1];
         })
         .attr("x", function (d) {
             return xScale(d[0]);
         })
         .attr("y", function (d) {
             return yScale(d[1]);
         })
         .attr("font-size", "11px")
         .attr("fill", "red");



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

是什么导致了这个错误?以及如何解决它

【问题讨论】:

  • 在 d3 4.x 中是 d3.scaleLinear()

标签: d3.js


【解决方案1】:

在 D3 v4 中不再命名为 d3.scale.linear()。请改用d3.scaleLinear()

【讨论】:

  • 我是 D3.js 的新手,d3.scale.ordinal() 在 d3 v4 中未定义?序数缩放的相关方法
  • @Megajin 有关更改的详细信息github.com/d3/d3/blob/master/CHANGES.md#scales-d3-scale
  • 如果您在使用d3js.org/d3.v4.js 时遇到错误,请尝试d3js.org/d3.v3.js
  • 注意 scaleLinear() 中的大写“L”
【解决方案2】:

感谢您的回答,我已经解决了以下代码的这个错误(我正在研究 D3.js):

var colorScale = d3.scale.linear().domain([0, 100]).range(["#add8e6", "blue"]);

出现以下错误(在网页内):

Uncaught TypeError: Cannot read property 'linear' of undefined at index.html:22

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 2021-12-22
    • 2017-07-26
    • 2015-01-06
    • 1970-01-01
    • 2019-02-26
    相关资源
    最近更新 更多