我有这个散点图,不适用于d3 v3。我在控制台中没有出现任何错误,但是没有按原样显示轴。
这是js文件:
var data = [
{
"xaxis": 0.2,
"yaxis": 0.8,
"color": 0
},
{
"xaxis": 0.3,
"yaxis": 0.7,
"color": 1
},
]
// set the dimensions and margins of the graph
var margin = {top: 10, right: 30, bottom: 30, left: 60},
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
//Read the data
function draw(data) {
// Add X axis
var x = d3.scale.linear()
.domain([0, 1])
.range([ 0, width ]);
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis(y));
// text label for the x axis
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height + margin.top + 20) + ")")
.style("text-anchor", "middle")
.text("Date");
// Add Y axis
var y = d3.scale.linear()
.domain([0, 1])
.range([ height, 0]);
svg.append("g")
.call(d3.svg.axis(y));
// text label for the y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Value");
// Color scale: give me a specie name, I return a color
var color = d3.scale.ordinal()
.domain(["0", "1", "2" ])
.range([ "#440154ff", "#21908dff", "#fde725ff"])
// Add dots
svg.append('g')
.selectAll("dot")
.data(data)
.enter()
.append("circle")
.attr("cx", function (d) { return x(d.xaxis); } )
.attr("cy", function (d) { return y(d.yaxis); } )
.attr("r", 5)
.style("fill", function (d) { return color(d.color) } )
}
draw(data);
和html:
<!DOCTYPE html>
<meta charset="utf-8">
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v3.js"></script>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
我已经从v4修改了此散点图以适用于v3,但似乎缺少了一些东西,我无法找到它。任何帮助将不胜感激。
答案 0 :(得分:2)
这是我见过几次有关降级版本的问题之一。您为什么要这样做尚不清楚。不过,问题很明显,在D3 v3中,您不会像以前那样将比例传递给轴生成器:
d3.svg.axis(y)
必须为:
d3.svg.axis()
.scale(y)//pass the scale here
.orient("left")//or "right", depending on what position you want