gpt4 book ai didi

javascript - 填充未在 d3.js 中定义

转载 作者:行者123 更新时间:2023-11-29 23:56:55 25 4
gpt4 key购买 nike

我试图在 x 轴上获取月份中的某天(即 1、2、3、4 ...等),在 Y 轴上获取 0-24 小时的时间段。我无法获得轴线我不知道为什么。有人能告诉我为什么吗?在控制台窗口中,它表示未定义填充。

    <!DOCTYPE html>
<meta charset="utf-8">
<style>

/*set the axis line color, dot stroke, font size, and font position*/
body {
font: 13px helvetica;
}

.name{
position: relative;
top: 90px;
text-align: left;
font-weight: bold;
}

.title {
position: relative;
text-align: left;
font-size: 25px;
}

.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.dot {
stroke: #000;
}

#filter {
position: absolute;
}

#mark {
padding-left: 150px;
position: inherit;
}

#xAXs {
position: relative;
left: 290px;
bottom: 30px;
}

#yAXs {
position: relative;
bottom: 30px;
left: 315px;

}

#label {
position: absolute;
top: 599px;
bottom: 125px;
left: 300px;
right: 0px;
}

#label2 {
position: absolute;
top: 599px;
bottom: 125px;
left: 430px;
right: 0px;
}

</style>

<body>


<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;


var x = d3.scale.linear()
.range([0, width]);

var y = d3.scale.linear()
.range([height, 0]);


var color = d3.scale.category10();

var axisNames = {
Hour: 'Hour',
Day: 'Day',
};





// define the x scale (horizontal)
var mindate = new Date(2012,0,1),
maxdate = new Date(2012,0,31);

var xScale = d3.time.scale()
.domain([mindate, maxdate]) // values between for month of january
.range([padding, width - padding * 2]); // map these the the chart width = total width minus padding at both sides

var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left");

var svg = d3.select("body").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 + ")");


d3.csv("file1.csv", function(error, data) {
data.forEach(function(d) {
d.Day = +d.Day;
d.Hour = +d.Hour;
});

x.domain(d3.extent(data, function(d) { return d.Day; })).nice();
y.domain(d3.extent(data, function(d) { return d.Hour; })).nice();

svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Day");

svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Hour")

var circles = svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.Hour); })
.attr("cy", function(d) { return y(d.day); })
.style("fill", function(d) { return color(d.name); });


var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });


legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color);


legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });



d3.selectAll("[name=v]").on("change", function() {
var selected = this.value;
display = this.checked ? "inline" : "none";


svg.selectAll(".dot")
.filter(function(d) {return selected == d.name;})
.attr("display", display);
});



d3.selectAll("[name=sepal]").on("change", function(d) {
radius = this.value;

svg.selectAll(".dot")
console.log(radius);
circles.attr("r", function(d) { return d[radius]; });
});



d3.select("[name=xAX]").on("change", function(){
xAxy = this.value;
console.log(xAxy)
x.domain(d3.extent(data, function(d) { return d[xAxy]; })).nice();

svg.select(".x.axis").transition().call(xAxis);

svg.selectAll(".dot").transition().attr("cx", function(d) {
return x(d[xAxy]);
});
svg.selectAll(".x.axis").selectAll("text.label").text(axisNames[xAxy] + " (cm)");
});

d3.select("[name=yAX]").on("change", function(){
yAxy = this.value;
console.log(yAxy)
y.domain(d3.extent(data, function(d) { return d[yAxy]; })).nice();
svg.select(".y.axis").transition().call(yAxis);
svg.selectAll(".dot").transition().attr("cy", function(d) {
return y(d[yAxy]);
});
svg.selectAll(".y.axis").selectAll("text.label").text(axisNames[yAxy] + " (cm)");
});

});

</script>
<br><br>
<br>
</body>

最佳答案

您设置一个时间尺度,其中范围使用 paddingwidth:

var xScale = d3.time.scale()
.domain([mindate, maxdate])
.range([padding, width - padding * 2]);

尽管您之前定义了 width:

width = 960 - margin.left - margin.right;

您没有在代码中的任何地方定义 padding

因此,在定义时间尺度之前,只需给它任何你想要的值:

var padding = 42; //tweak this value

关于javascript - 填充未在 d3.js 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41360469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com