【发布时间】:2020-04-12 09:04:35
【问题描述】:
我正在使用出色的插件 d3 注释,但我很难弄清楚为什么我的注释沿 x 轴放置在错误的区域。我正在使用时间刻度来确定我的轴的范围,并且我对注释使用相同的范围,但不知何故它不起作用。我遵循了文档 (https://bl.ocks.org/susielu/23dc3082669ee026c552b85081d90976) 中的相同流程。
X 尺度:
const xScale = d3
.scaleTime()
.domain(d3.extent(data, xAccessor))
.range([0, dimensions.boundedWidth])
.nice();
注释:
const annotations = [
{
note: {
label: 'National emergency is declared',
align: 'center',
wrap: 100,
},
subject: {
y1: dimensions.margin.top + 40,
y2: dimensions.height - dimensions.margin.bottom,
},
y: dimensions.height - dimensions.boundedHeight,
data: { x: dateParser('2020-3-13') }, // position the x based on an x scale
},
{
note: {
label: `Florida's stay at home rule is declared`,
align: 'right',
wrap: 120,
},
subject: {
y1: dimensions.margin.top + 40,
y2: dimensions.height - dimensions.margin.bottom,
},
y: dimensions.height - (dimensions.boundedHeight - 10),
data: { x: dateParser('2020-4-1') },
},
];
const type = d3.annotationCustomType(d3.annotationXYThreshold, {
note: {
lineType: 'none',
orientation: 'top',
},
});
// 12 - render the annotations
const makeAnnotations = d3
.annotation()
.type(type)
.accessors({
x(d) {
return xScale(d.x);
},
y(d) {
return yScale(d.y);
},
})
.annotations(annotations)
.textWrap(30);
wrapper
.append('g')
.attr('class', 'annotation-group')
.call(makeAnnotations);
【问题讨论】:
标签: javascript d3.js data-visualization