그리드 라인이있는 4 사분면 차트를 만드는 중입니다. 그러나 그리드가 제대로 작동하지 않는 것 같습니다. 축을 변환 할 때 양쪽으로 가지 않습니다. 여기도 바이올린을 제공했습니다 .
const f = (x) => {
return Math.sin(x)
}
const data = d3.ticks(-5, 5, 100).map((x) => {
const container = {
x: x,
y: f(x)
}
return container
})
const margins = { top: 20, right: 20, bottom: 30, left: 30 }
const width = 750 - margins.left - margins.right
const height = 750 - margins.top - margins.bottom
const svg = d3.select('#graph')
.append('svg')
.attr('width', width + margins.left + margins.right)
.attr('height', height + margins.top + margins.bottom)
.append('g')
.attr('transform', `translate(${margins.left}, ${margins.top})`)
const x = d3.scaleLinear([-5, 5], [0, width])
const y = d3.scaleLinear([-5, 5], [height, 0])
const xAxis = svg.append('g')
.attr('transform', `translate(0, ${y(0)})`)
.attr('class', 'x-axis')
const yAxis = svg.append('g')
.attr('transform', `translate(${x(0)}, 0)`)
.attr('class', 'y-axis')
xAxis.call(d3.axisBottom(x))
yAxis.call(d3.axisLeft(y))
d3.selectAll('g.x-axis g.tick')
.append('line')
.attr('class', 'gridline')
.attr('x1', 0)
.attr('y1', -height)
.attr('x2', 0)
.attr('y2', 0)
d3.selectAll('g.y-axis g.tick')
.append('line')
.attr('class', 'gridline')
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', width)
.attr('y2', 0)
svg.append('path')
.datum(data)
.attr('fill', 'none')
.attr('stroke', 'steelblue')
.attr('stroke-width', 1.5)
.attr('d', d3.line(d => x(d.x), d => y(d.y)))
#graph {
text-align: center;
}
.gridline{
stroke: black;
shape-rendering: crispEdges;
stroke-opacity: 0.5;
}
<div id="graph"></div>
<script src="https://d3js.org/d3.v6.min.js"></script>
이 방법 을 사용 하여 격자 선을 만듭니다.
그리드가 내가 번역하는쪽으로 확장되기를 원합니다. 감사합니다.
나는 당신이 연결 한 답변의 저자입니다. 그 접근 방식의 문제는 제가 대답에서 명확히 밝히지 않았지만 모든 선이 축의 동일한 위치에 생성된다는 것입니다. 따라서 번역해야합니다.
의 경우 x
축 :
.attr("transform", `translate(0, ${y.range()[0] - y(0)})`)
그리고 y
축의 경우 :
.attr("transform", `translate(${x.range()[0] - x(0)})`, 0)
변경된 코드는 다음과 같습니다.
const f = (x) => {
return Math.sin(x)
}
const data = d3.ticks(-5, 5, 100).map((x) => {
const container = {
x: x,
y: f(x)
}
return container
})
const margins = {
top: 20,
right: 20,
bottom: 30,
left: 30
}
const width = 750 - margins.left - margins.right
const height = 750 - margins.top - margins.bottom
const svg = d3.select('#graph')
.append('svg')
.attr('width', width + margins.left + margins.right)
.attr('height', height + margins.top + margins.bottom)
.append('g')
.attr('transform', `translate(${margins.left}, ${margins.top})`)
const x = d3.scaleLinear([-5, 5], [0, width])
const y = d3.scaleLinear([-5, 5], [height, 0])
const xAxis = svg.append('g')
.attr('transform', `translate(0, ${y(0)})`)
.attr('class', 'x-axis')
const yAxis = svg.append('g')
.attr('transform', `translate(${x(0)}, 0)`)
.attr('class', 'y-axis')
xAxis.call(d3.axisBottom(x))
yAxis.call(d3.axisLeft(y))
d3.selectAll('g.x-axis g.tick')
.append('line')
.attr('class', 'gridline')
.attr('x1', 0)
.attr('y1', -height)
.attr('x2', 0)
.attr('y2', 0)
.attr("transform", `translate(0, ${y.range()[0] - y(0)})`)
d3.selectAll('g.y-axis g.tick')
.append('line')
.attr('class', 'gridline')
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', width)
.attr('y2', 0)
.attr("transform", `translate(${x.range()[0] - x(0)})`, 0)
svg.append('path')
.datum(data)
.attr('fill', 'none')
.attr('stroke', 'steelblue')
.attr('stroke-width', 1.5)
.attr('d', d3.line(d => x(d.x), d => y(d.y)))
#graph {
text-align: center;
}
.gridline {
stroke: black;
shape-rendering: crispEdges;
stroke-opacity: 0.5;
}
<div id="graph"></div>
<script src="https://d3js.org/d3.v6.min.js"></script>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다