自定义Y轴标签和D3中的勾
我正在使用d3
来创建和SVG如下
// 1 DATA
const data = [
{ "x": 50, "y": 0.10 },
{ "x": 100, "y": 0.15 },
{ "x": 150, "y": 0.25 },
{ "x": 200, "y": 0.35 },
{ "x": 250, "y": 0.45 },
{ "x": 300, "y": 0.55 },
{ "x": 350, "y": 0.65 },
{ "x": 400, "y": 0.75 },
{ "x": 450, "y": 0.85 },
{ "x": 500, "y": 0.87 },
{ "x": 550, "y": 0.92 },
{ "x": 600, "y": 0.98 }
]
height = 400,
width = 720;
padding = {
top: 70,
bottom: 50,
left: 70,
right: 70
}
const boundHeight = height - padding.top - padding.bottom;
const boundWidth = width - padding.right - padding.left;
// 2 CREATE SCALE
const scaleY = d3.scaleLinear()
.range([boundHeight, 0])
.domain(d3.extent(data, d => d.y))
// 3 SVG
const svgns = 'http://www.w3.org/2000/svg'
const svg = d3.select('svg')
svg
.attr('xmlns', svgns)
.attr('viewBox', `0 0 ${width} ${height}`)
//create bound element
bound = svg.append('g')
.attr('class', 'bound')
.style('transform', `translate(${padding.left}px,${padding.top}px)`)
bound.append('g').attr('class', 'yAxis1')
.append('g').attr('class', 'yAxisLeft')
.call(d3.axisLeft(scaleY).tickSizeInner([(-(boundWidth))])
.tickFormat(d3.format(",.0%")))
console.log(d3.mean(data.map(d => d.y)))
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>
<svg></svg>
<div id="container" class="svg-container"></div>
<script src="next.js"></script>
data.y = 0.5724999999999999
的平均值。除了自动生成的y-axis ,我如何将此值传递给轴发生器以创建
tick 。 tick和标签。标签
tick
I am working with d3
to create and svg as following
// 1 DATA
const data = [
{ "x": 50, "y": 0.10 },
{ "x": 100, "y": 0.15 },
{ "x": 150, "y": 0.25 },
{ "x": 200, "y": 0.35 },
{ "x": 250, "y": 0.45 },
{ "x": 300, "y": 0.55 },
{ "x": 350, "y": 0.65 },
{ "x": 400, "y": 0.75 },
{ "x": 450, "y": 0.85 },
{ "x": 500, "y": 0.87 },
{ "x": 550, "y": 0.92 },
{ "x": 600, "y": 0.98 }
]
height = 400,
width = 720;
padding = {
top: 70,
bottom: 50,
left: 70,
right: 70
}
const boundHeight = height - padding.top - padding.bottom;
const boundWidth = width - padding.right - padding.left;
// 2 CREATE SCALE
const scaleY = d3.scaleLinear()
.range([boundHeight, 0])
.domain(d3.extent(data, d => d.y))
// 3 SVG
const svgns = 'http://www.w3.org/2000/svg'
const svg = d3.select('svg')
svg
.attr('xmlns', svgns)
.attr('viewBox', `0 0 ${width} ${height}`)
//create bound element
bound = svg.append('g')
.attr('class', 'bound')
.style('transform', `translate(${padding.left}px,${padding.top}px)`)
bound.append('g').attr('class', 'yAxis1')
.append('g').attr('class', 'yAxisLeft')
.call(d3.axisLeft(scaleY).tickSizeInner([(-(boundWidth))])
.tickFormat(d3.format(",.0%")))
console.log(d3.mean(data.map(d => d.y)))
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>
<svg></svg>
<div id="container" class="svg-container"></div>
<script src="next.js"></script>
The mean
of data.y=0.5724999999999999
. How can I pass on this value to the axis generator to create a label
and tick
for this, apart from the auto-generated y-axis
tick and labels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取秤的自动生成的刻度阵列(
scaley.ticks()
),将平均值添加到该数组中,然后将其传递到axis.tickvalues
:这是您的代码,带有您的代码变化:
Get the scale's auto-generated ticks array (
scaleY.ticks()
), add the mean value to that array and pass it toaxis.tickValues
:Here is your code with that change: