2016-07-19 92 views
53

Sono nuovo di D3 e ottenere il seguente errore nel mio script demo -Uncaught TypeError: Impossibile leggere la proprietà 'lineare' di indefinito

FirstD3.jsp: 31 Uncaught TypeError: Impossibile leggere la proprietà 'lineari' di undefined

il mio codice demo è come seguire

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

che cosa sta causando questo errore? e come risolverlo

+8

In d3 4.x è 'd3.scaleLinear()'. –

risposta

148

In D3 v4 non è più denominato d3.scale.linear(). Utilizzare invece d3.scaleLinear().

+3

Sono nuovo a D3.js, d3.scale.ordinal() non è definito in d3 v4? metodo pertinente per il ridimensionamento ordinale –

+4

Grazie, mi ha aiutato anche. Dovrebbero davvero aggiornare i loro Tutorial ... – Megajin

+2

@Megajin informazioni dettagliate sulle modifiche https://github.com/d3/d3/blob/master/CHANGES.md#scales-d3-scale –

0

Grazie per la risposta, ho risolto questo errore per il seguente codice (Sto studiando d3.js):

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

con il seguente errore (all'interno della pagina web):

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