플롯 I d3에 새로 추가되었으며 자바 스크립트에도 새로 도입되었습니다. 대부분의 경험은 파이썬과 같습니다. 나는 최근에 도서에있는 자습서를 갔다 대화 형 데이터 시각화 : 웹에 대한 Scott Murray의 막대 차트 작성. 튜토리얼을 완료하는 데는 문제가 없었지만 책에서 취하는 접근법은 전역 변수를 사용하는 대부분 절차 적으로 보인다. 비슷한 차트를 만들고 싶지만 변수를 캡슐화하고 코드를 재사용하여 코드를 반복하지 않아도 하나의 스크립트에서 여러 차트를 생성 할 수있게하려면 객체 지향 접근 방식을 사용하십시오. 사람이 작동하지 않는 이유를 알고 않습니다d3에서 객체 지향 접근 방식을 사용하여 가로 막 대형 차트를 생성하려고 시도했습니다.
function d3Chart(id, data, width, height, padding) {
this.id = id
this.data = data
this.width = width
this.height = height
this.padding = padding
this.svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("class", "chart")
.attr("id". this.id);
this.yscale = d3.scale.linear()
.domain([0, d3.max(data)])
.range([h - padding, padding])
this.xscale = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeRoundBands([padding,w-padding], 0.05)
};
d3Chart.prototype.rect = function() {
this.rect = this.svg.selectAll("rect")
.data(this.data)
.enter()
.append("rect");
}
d3Chart.prototype.plot = function() {
this.rect.attr("x", function(d, i) {
return this.xscale(i);
})
.attr("y", function (d) {
return this.yscale(d);
})
.attr("width", this.xscale.rangeBand())
.attr("height", function(d) {
return this.height - this.padding - this.yscale(d);
})
.attr("fill", function(d) {
return "rgb(" + (100 - d) + ", 0, " + (d*10) + ")";
});
}
var chart = new d3Chart("chart1", [5, 15, 10, 30, 20, 45, 15, 10, 5], 600, 400, 40);
chart.rect()
chart.plot()
나는 내가 잘못 아주 간단 뭔가가있을거야, 또는이 심지어 경우 : 이것은 내가 운이없이 지금까지 시도한 것입니다 올바른 접근 방식을 취할 것인가? 어떤 도움이라도 대단히 감사 할 것입니다. 감사.
드미트리 소 슈니 코브 (Dmitry Soshnikov)는 읽을만한 가치가있는 Javascript의 메커니즘에 대해 매우 짧고 감미로운 설명 (http://dmitrysoshnikov.com/ecmascript/javascript-the-core/)을 썼습니다. 대부분의 OOP 필요성은 변수가 설정되고 클로저 캡슐화 된 함수 내에서 함수를 반환하여 해결할 수 있습니다. 기본 객체를 확장해야하는 경우 [Object.create] (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create) 및 Object.defineProperty 메소드를 살펴보십시오. –