在d3图表上拖动时仅更新最后一个Y轴

问题描述 投票:2回答:1

我的图表具有多个具有不同域的Y轴。

当我在图表上拖动时,仅更新了最后一个y轴。

enter image description here

我如下添加了每个y轴;

addYAxis(data, tag) { // tag is for index for each y-axis e.g: 0, 1, 2
  const yScale = d3.scale.linear()
    .domain([0, this.innerHeight])
    .range([this.innerHeight, 0]);
  const yAxis = d3.svg.axis()
    .scale(yScale)
    .orient(tag ? 'right' : 'left')
    .tickSize(tag ? this.innerWidth + 50 * (tag - 1) : -this.innerWidth);
  const yAxisElement = this.g.append('g')
    .attr('class', 'y axis')
    .call(yAxis);
  this.yAxisList.push({yScale, yAxis, yAxisElement});
}

这里是每个轴的缩放列表。

this.zoom.push(d3.behavior.zoom()
    .x(this.xScale)
    .y(this.yAxisList[tag].yScale)  // when I replace [tag] with [0], then only first axis is being updated.
    .scaleExtent([.5, 10])
    .scale(this.currentZoom)
    .translate(this.currentPan)
    .on('zoom', () => this.zoomed(tag))
    .on('zoomend', () => {
        setTimeout(() => { this.zooming = false; }, 10);
    }));

this.g.call(this.zoom[tag])  // when I replace [tag] with [0], then only first axis is being updated.
    .on('dblclick.zoom', null);

并如下更新它们;

updateYAxis() {
  this.yAxisList.forEach(({yAxisElement, yAxis}) => yAxisElement.call(yAxis));
}

此图表的结构:enter image description here拖动图表时如何更新所有Y轴?

提前感谢。

javascript d3.js
1个回答
2
投票

这里,

.on('zoom', () => this.zoomed(tag))

您仅更新了当前缩放,但还应该使用当前缩放值来更新其他缩放对象。

.on('zoom', () => {
    this.zoom.forEach((zoom, t) => {
        zoom.scale(this.zoom[tag].scale());
        zoom.translate(this.zoom[tag].translate());
        this.zoomed(t);
    });
})
© www.soinside.com 2019 - 2024. All rights reserved.