CSV 파일에서 막 대형 차트를 만들고 싶습니다. 키 열은 대부분 두세 단어이므로 CSV 파일은 매우 복잡하고 다소 혼란 스럽습니다. 나는 csv를 읽고 ARREST 같은 데이터를 얻을 수 있습니다. 이제는 매년 arrestees를 계산하는 함수가 필요합니다. 그래서 저는 다른 배열이 필요하다고 생각합니다. 이 배열을 사용하여 저는 올해에 x 축에 barchart를 만들고 y는 arrestees의 수를 만들고 싶습니다. 어떤 사람이이 일을 도와 줄 수 있습니까? 나는 JavaScript에 익숙하지 않으며 조금 혼란 스럽다. D3 큰 CSV에서 막 대형 차트 만들기
var arrestdate = [];
console.log(arrestdate);
d3.csv("urbana_crimes.csv", function(error, data) {
data.map(function(m){
arrestdate.push(m["YEAR OF ARREST"]);
})
//console.log(arrestdate);
});
console.log(arrestdate);
count(arrestdate);
function count(data) {
data.sort();
var current = null;
var cnt = 0;
for (var i = 0; i < data.length; i++) {
if (data[i] != current) {
if (cnt > 0) {
document.write(current + ' comes --> ' + cnt + ' times<br>');
}
current = data[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt > 0) {
document.write(current + ' comes --> ' + cnt + ' times');
}
};
감사를 사전 베른하르트
편집에 : 업데이트 코드 :
이것은 내가 지금까지 무엇을 가지고var arrestdate = [];
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
console.log(arrestdate);
d3.csv("urbana_crimes.csv", function(error, data) {
data.map(function(m){
arrestdate.push(m["YEAR OF ARREST"]);
})
var nested = d3.nest()
.key(function (d) { return d['YEAR OF ARREST'] })
.entries(data);
//console.log(nested[0].key);
//console.log(nested[0].values);
// Set X to all your 19 keys, which are your years
x.domain(nested.map(function(d) { return d.key }))
// Set Y between 0 and the maximum length of values, which are your arrests
y.domain([0, d3.max(data, function(d) { return d.values.length })])
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10, "%"))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Frequency");
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("width", x.bandwidth())
.attr("x", function(d) { return x(nested[0].values.length); }) //What to put here?
.attr("y", function(d) { return y(+nested[0].key); }) // What to put here?
.attr("height", function(d) { return height - y(+nested[0].key); });
});
감사합니다. 내 문제는 지금, 내가 웹 페이지를로드 할 때마다 Firefox가 충돌한다는 것입니다. 이 튜토리얼을 따라 barchart http://alignedleft.com/tutorials/d3/making-a-bar-chart를 만들었습니다. 내 질문은 : 내 데이터로도 작동합니까? 아니면 "다른"바 사르트를 만들어야합니까? – potu1304
예, 데이터로 작동합니다. [D3의 제작자 인 Mike Bostock의 아주 좋은 예가 있습니다] (https://bl.ocks.org/mbostock/3885304). – b2m9
좋아, 내가 이것을 시도합니다,하지만 너비 = + svg.attr ("너비")에 대한 "노드가 null 오류"- margin.left - margin.right, 뭐가 잘못 됐어? 그리고 미래에 x와 y 도메인에 다음과 같이 중첩 된 []을 넣어야합니까? x.domain (data.map (function (d) {return nested [0] .values.length;})); ? – potu1304