D3.js JSON / ARRAY에서 구름 크기 태그?
-
12-12-2019 - |
문제
이 코드를 수정하고 있습니다 : https://github.com/jasondavies/d3-cloud와)>
<script>
d3.layout.cloud().size([300, 300])
.words([
"Hello", "world", "normally", "you", "want", "more", "words",
"than", "this"].map(function(d) {
return {text: d, size: 10 + Math.random() * 90};
}))
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
function draw(words) {
d3.select("body").append("svg")
.attr("width", 300)
.attr("height", 300)
.append("g")
.attr("transform", "translate(150,150)")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
</script>
.
별도의 JSON 데이터에서 단어와 크기 데이터를 얻고 싶습니다. 나는 두 가지 변수가있다
jWord = ["abc","def","ghi,"jkl"];
jCount = ["2", "5", "3", "8"];
.
JWORD에는 태그 클라우드에 표시하려는 단어가 있습니다. JCount는 해당 단어 (동일한 순서)의 크기입니다.
에 전환하는 방법을 확실하지 않습니다. .words(jWord.map(function(d) {
return {text: d, size: 10 + Math.random() * 90};
}))
.
jWord_Count = ["abc":2, "def":5, "ghi":3, "jkl":8 ];
.
이 형식이 도움이되는 경우 해결책
d3.zip : d3.zip(jWord, jCount)
는 첫 번째 요소가 텍스트 인 병합 된 배열을 반환합니다.제 1 워드 [jWord[0], jCount[0]]
의 크기의 크기, 두 번째 요소는 제 2 단어이고,예 :
.words(d3.zip(jWord, jCount).map(function(d) {
return {text: d[0], size: d[1]};
}))
.
이펙트에서 D3.zip은 열 지향 데이터를 행 지향 데이터로 전환합니다.행 지향적 인 양식으로 데이터를 표시하여 다음과 같이 시작할 수도 있습니다.
var words = [
{text: "abc", size: 2},
{text: "def", size: 5},
{text: "ghi", size: 3},
{text: "jkl", size: 8}
];
.
마지막으로 유형을 조심하십시오.카운트는 숫자가 아닌 문자열 ("2"
)으로 표시됩니다.따라서 2
를 사용하여 숫자로 구성 할 수 있습니다.
제휴하지 않습니다 StackOverflow