javascript - 自定义 Y 轴标签并在 d3 中打勾

标签 javascript d3.js

我正在与 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>

meandata.y=0.5724999999999999 。如何将此值传递给轴生成器以创建 labeltick为此,除了自动生成的 y-axis勾选和标签。

最佳答案

获取刻度自动生成的刻度数组 (scaleY.ticks()),将平均值添加到该数组并将其传递给 axis.tickValues:

.tickValues(scaleY.ticks().concat(mean))

这是经过更改的代码:

////////////////////////////////////////////////////////////
//////////////////////// 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)`)

const mean = d3.mean(data.map(d => d.y));

bound.append('g').attr('class', 'yAxis1')
  .append('g').attr('class', 'yAxisLeft')
  .call(d3.axisLeft(scaleY)
    .tickValues(scaleY.ticks().concat(mean))
    .tickSizeInner([(-(boundWidth))])
    .tickFormat(d3.format(",.0%")))
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>

<body>
  <svg>
</svg>
  <div id="container" class="svg-container"></div>
  <script src="next.js"></script>
</body>

</html>

关于javascript - 自定义 Y 轴标签并在 d3 中打勾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72625889/

相关文章:

javascript - 对象字面量符号 vs 原型(prototype)速度和内存

javascript - D3 - 按 id 选择

javascript - 无法缩放 d3 techanjs

javascript - 有没有办法在没有 setState 的情况下重新渲染功能性 React.js 组件?

javascript - 使用 promises 将异步函数的结果返回为 'variable'

javascript - hr在移动端div后面

svg - D3 序数标度仅返回极端值。为什么不在范围和域之间进行插值?

javascript - IFrame 在 IE9 和 IE11 上的渲染方式不同

javascript - 如何让d3旭日从3点钟开始?

javascript - 删除节点时的 D3 更新总是删除 SVG DOM 中的最后一个条目