javascript - 使用 D3.JS 将 HTML 标签添加到轴标签
问题描述
我正在使用 vue 和 d3 制作简单的水平条形图。我想沿垂直轴自定义我的轴标签。现在,我指定在每个标签中放置什么的代码如下所示:
let yAxis = d3.axisLeft()
.scale(this.yScale)
.tickSize(0)
.tickPadding(4)
.tickFormat((d, i) => { return this.data[i].country + " " + this.data[i].value })
我希望我的标签看起来像这样:
国家 | 价值 |
---|---|
国家1 | 10.2 |
国家2 | 200.3 |
国家2 | 3000.4 |
即国家必须左对齐,而值必须右对齐。此外,值必须以粗体显示。问题在于,似乎 .tickFormat 不接受任何 html 标签
解决方案
这是设置轴标签样式的示例:
const svg = d3.select("svg")
.attr("width", 1000)
// Create the scale
const x = d3.scaleLinear()
.domain([0, 100]) // This is what is written on the Axis: from 0 to 100
.range([100, 800]); // This is where the axis is placed: from 100px to 800px
// Draw the axis
svg
.append("g")
.classed('x-axis', true)
.attr("transform", "translate(0,50)") // This controls the vertical position of the Axis
.call(d3.axisBottom(x));
svg.select('.x-axis')
.selectAll('text')
.style('fill', 'red')
.style('font-size', '32px')
.attr('y', 20)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg></svg>
推荐阅读
- php - 仅在域主页上向未签名用户显示特定内容
- java - 字符串数据格式
- python - Azure PageBlob 上传新文件:x-ms-blob-content-length 错误
- angular - 使用角度将产品ID从客户端添加到服务器
- laravel - 重定向到 livewire 组件时的问题
- c++ - 无法使用工具集 v120 (Visual Studio) 转换 std::pair
- javascript - 在表单 php 上显示 JS 警报
- reactjs - React + Mutual TLS (TLS) 与客户端证书到 REST 微服务
- javascript - 如何让所有图标单独旋转而不是一个?
- navigation - 引导药丸如何显示药丸标签而不在引导程序5上滚动