Uncaught TypeError: Cannot read property 'linear' of undefined

d3.js

d3.js Problem Overview


I'm new to D3 and getting the following error in my demo script -

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

My demo code is as follow

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

what is causing this error? and how to solve it

d3.js Solutions


Solution 1 - d3.js

In D3 v4 it is no longer named d3.scale.linear(). Use d3.scaleLinear() instead.

Solution 2 - d3.js

Thanks for the answer, I've solved this error for the following code (I'm studing D3.js) :

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

With the following error (inside the web page) :

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAugustusView Question on Stackoverflow
Solution 1 - d3.jsBabish ShresthaView Answer on Stackoverflow
Solution 2 - d3.jsColonna MaurizioView Answer on Stackoverflow