안녕하세요 이해하려고 노력 중이 야 이 예제 와 이 다른 예를 누적 영역에 눈금 선을 추가 할 수 있습니다.
tiksArray = [1900, 1950, 2000, 2010];
keys = ["Amanda", "Ashley", "Betty", "Deborah"];
// Add X axis
const dX = d3.extent(nbrOfBabies, d => d.year);
const x = d3.scaleLinear()
.domain(dX)
.range([0, this.width]);
svg.append("g")
.attr("class", "grid")
.attr("transform", `translate(0,${this.height})`)
.call(this.make_x_gridlines(x)
.tickSize(-this.height)
.tickFormat(null)
);
// gridlines in x axis function
make_x_gridlines(x) {
const bttm = d3.axisBottom(x)
.tickValues(this.tiksArray);
return bttm;
}
이 그래프를 얻고 있습니다. 나는 그 틱의 X 값, 즉 1900 , 1950 , 2000을 알고 싶습니다. 각 열에 대한 범례를 추가 할 수 있도록 각 줄이 시작되는 위치를 알고 싶습니다.
도움을 주셔서 감사합니다.
D3에서 축 생성기는 틱 위치를 지정하기 위해 전달 된 스케일 을 사용합니다 . 따라서 필요한 tiksArray
것은 동일한 척도 (귀하의 경우 x
)에 해당 값을 전달하는 것 입니다.
다음은 매우 기본적인 데모입니다.
const ticksArray = [1900, 1950, 2000, 2010];
const svg = d3.select("svg")
const x = d3.scaleLinear()
.domain([1880, 2020])
.range([50, 450]);
svg.append("g")
.attr("transform", `translate(0,${100})`)
.call(d3.axisBottom(x)
.tickValues(ticksArray)
.tickSizeInner(-100)
);
ticksArray.forEach(d => {
console.log("The SVG position of the tick " + d + " is: " + x(d))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="200"></svg>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다