gpt4 book ai didi

javascript - JS d3条形图不显示数据

转载 作者:行者123 更新时间:2023-12-03 02:10:18 25 4
gpt4 key购买 nike

我正在尝试使用 d3 JS 库创建条形图。我试图实现 here 中提供的代码进行了细微的更改,但没有成功。

这是我的完整代码示例:

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bar Chart</title>

<!-- Reference style.css -->
<link rel = "stylesheet" type="text/css" href="style.css">

<!-- Reference minified version of D3 -->
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>

<body>
<div id="barplot" style='width: 600px; height: 500px; padding-left: 5px;'></div>
<script>
var width = 400, height = 350;
var margin = {top: 20, right: 20, left: 40, bottom: 20};

var svg2 = d3.select("#barplot").append("svg")
.append("g")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

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

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

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

createBarPlot();
updateBarPlot();

function createBarPlot(){
svg2.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );

svg2.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Cn0");
}

function updateBarPlot(){
// Create data array of values to visualize
var Cn0 = [{"sat": 2, "val": 15}, {"sat": 4, "val": 20}, {"sat": 10, "val": 11}];

x.domain(Cn0.map(function(d) { return d.sat;}));
y.domain([0, d3.max(Cn0, function(d) { return d.val;})]);

svg2.selectAll("bar")
.data(Cn0)
.enter().append("rect")
.style("fill", "steelblue")
.attr("x", function(d) { return x(d.sat);})
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.val);})
.attr("height", function(d) { return height - y(d.value);});
}

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

当我运行它时,我可以创建条形图的轴,但我在图表上看不到任何数据。我怀疑问题出在 updateBarPlot() 方法中,但我无法弄清楚它出了什么问题。知道如何解决这个问题吗?任何帮助,将不胜感激。谢谢。

最佳答案

就 x 轴的标签和 y 轴的域而言,轴的调用应该在设置各自的域之后完成,即 .call(xAxis) 和设置域后(在 updateBarPlot 函数中),应调用 .call(yAxis),如下所示:

x.domain(Cn0.map(function(d) { return d.sat;}));
y.domain([0, d3.max(Cn0, function(d) { return d.val;})]);

svg2.select('.x.axis')
.call(xAxis);

svg2.select('.y.axis')
.call(yAxis);

此外,我还为轴的路径和刻度添加了一些样式:

<style>

.axis {
font: 10px sans-serif;
}

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

</style>

这是结合上述内容的片段(以及@Gerardo 在评论中提到的拼写错误修复):

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bar Chart</title>

<!-- Reference style.css -->

<!-- Reference minified version of D3 -->
<style>

.axis {
font: 10px sans-serif;
}

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

</style>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>

<body>
<div id="barplot" style='width: 600px; height: 500px; padding-left: 5px;'></div>
<script>
var width = 400, height = 350;
var margin = {top: 20, right: 20, left: 40, bottom: 20};

var svg2 = d3.select("#barplot").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 + ")");

var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);

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

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

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

createBarPlot();
updateBarPlot();

function createBarPlot(){
svg2.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")");

svg2.append("g")
.attr("class", "y axis")
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Cn0");
}

function updateBarPlot(){
// Create data array of values to visualize
var Cn0 = [{"sat": 2, "val": 15}, {"sat": 4, "val": 20}, {"sat": 10, "val": 11}];

x.domain(Cn0.map(function(d) { return d.sat;}));
y.domain([0, d3.max(Cn0, function(d) { return d.val;})]);

svg2.select('.x.axis')
.call(xAxis);

svg2.select('.y.axis')
.call(yAxis);

svg2.selectAll("bar")
.data(Cn0)
.enter().append("rect")
.style("fill", "steelblue")
.attr("x", function(d) { return x(d.sat);})
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.val);})
.attr("height", function(d) { return height - y(d.val);});
}

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

关于javascript - JS d3条形图不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49609845/

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