gpt4 book ai didi

charts - d3.json将数据传递到d3.svg.arc()

转载 作者:行者123 更新时间:2023-12-04 18:11:15 25 4
gpt4 key购买 nike

我不是JS用户,但我想像这样制作“夜莺图”:http://windhistory.com/station.html?KHKA
我有该代码:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3.v2.js"></script>
<style type="text/css">
.arc{
fill: pink;
stroke: red;
}
</style>
</head>
<body>
<div id="chart" class="chart"></div>
<div id="table"></div>
<script type="text/javascript">
var svg = d3.select("#chart").append("svg").attr("width", 900).attr("height", 600);
var pi = Math.PI;

d3.json(
'data.json',
function(data){
var arc = d3.svg.arc()
.innerRadius(50)
.outerRadius(function(d) {
return (50 + d.value);
})
.startAngle(function(d) { return ((d.time - 1) * 30 * pi / 180); })
.endAngle(function(d) { return (d.time * 30 * pi / 180 ); });

var chartContainer = svg.append("g")
.attr('class', 'some_class')
.attr("transform", "translate(450, 300)");

chartContainer.append("path")
.data(data)
.attr("d", arc)
.attr("class", "arc");
}
);
</script>
</body>
</html>

在jsfinddle上: http://jsfiddle.net/lmasikl/gZ62Z/

我的json:
[
{"label": "January", "value": 150, "time": 1},
{"label": "February", "value": 65, "time": 2},
{"label": "March", "value": 50, "time": 3},
{"label": "April", "value": 75, "time": 4},
{"label": "May", "value": 150, "time": 5},
{"label": "June", "value": 65, "time": 6},
{"label": "July", "value": 50, "time": 7},
{"label": "August", "value": 75, "time": 8},
{"label": "September", "value": 65, "time": 9},
{"label": "October", "value": 50, "time": 10},
{"label": "November", "value": 75, "time": 11},
{"label": "December", "value": 150, "time": 12}
]

但是我的剧本只画了一条弧线。有人可以帮助解决这个问题吗?

最佳答案

您可能需要阅读Thinking With Joins。用于添加数据驱动元素的D3模式是使用selectAll创建一个选择,然后使用data设置数据,然后将该元素附加到.enter()选择中。所以

chartContainer.append("path")
.data(data)
.attr("d", arc)
.attr("class", "arc");

需要是
chartContainer.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", arc)
.attr("class", "arc");

查看更新的 fiddle : http://jsfiddle.net/gZ62Z/1/

关于charts - d3.json将数据传递到d3.svg.arc(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13587376/

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