我正在尝试使用D3(v7)在我的网页上显示一个基本的动态条形图。这是在Django项目内完成的,我的图表从views.py文件中获取数据。但是,该图表仅在页面上部分显示。 x和y轴线以及轴标签已显示。以下是我的错误消息:
Error: <rect> attribute height: Expected length, "NaN".
这是我的HTML模板的相关部分:
<div id="chart-container"></div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script type="text/javascript">
const data = JSON.parse('{{ assign_levels|safe }}');
const margin = {top: 10, right: 30, bottom: 140, left: 60};
const width = 400 - margin.left - margin.right;
const height = 500 - margin.top - margin.bottom;
const svg = d3.select("#chart-container")
.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})`);
const x = d3.scaleBand()
.range([0, width])
.domain(data.map(d => d.level_assign))
.padding(0.3);
const y = d3.scaleLinear()
.range([height, 0])
.domain([0, d3.max(data, d => d.count)]);
svg.append("g")
.call(d3.axisLeft(y));
svg.selectAll(".bar")
.data(data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", d => x(d.level_assign))
.attr("width", x.bandwidth())
.attr("y", height)
.attr("height", 0)
.attr("fill", "#5F9EA0")
.transition()
.duration(1000)
.delay((d, i) => i * 100)
.attr("y", d => y(d.count))
.attr("height", d => height - y(d.count));
svg.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "rotate(-45)")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em");
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("Study Count");
svg.append("text")
.attr("x", (width + margin.left + margin.right) / 2)
.attr("y", margin.top / 2)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("Level of Assignment");
</script>
这是我输入到图表中的代码:
[["Class", 36], ["School - cluster", 6], ["Individual", 20], ["N", 1], ["School - multi-site", 9], ["Not provided/ not available", 4], ["Region or district", 1]]
我不明白为什么错误信息告诉我高度接收到了一个"NaN",因为我可以看到高度已经以绝对值的方式设置为500。