简体   繁体   English

多系列 d3js v5 图表的折线图缩放

[英]Line chart zoom for multiseries d3js v5 chart

I am trying to achieve zooming for d3js line chart.我正在尝试实现 d3js 折线图的缩放。 It is working fine for single series line, but not working for multiseries.它适用于单系列线,但不适用于多系列。 here is the svg zooming function这是 svg 缩放功能

function zoom(svg) {

        var extent = [
            [margin.left, margin.top], 
            [width - margin.right, height - margin.top]
        ];

        var zooming = d3.zoom()
            .scaleExtent([1, 3])
            .translateExtent(extent)
            .extent(extent)
            .on("zoom", zoomed);

        svg.call(zooming);

        function zoomed() {

            x.range([margin.left, width - margin.right]
                .map(d => d3.event.transform.applyX(d)));

            // for single series
            svg.select(".path")
                .attr("d", line);

            svg.select(".x-axis")
                .call(d3.axisBottom(x)
                    .tickSizeOuter(0));
        }
    }

attached working fiddle also, in the demo you can clearly see on mouse scroll, only one line series gets zoomed, other one fixed as it is.还附上了工作小提琴,在演示中,您可以清楚地看到鼠标滚动,只有一个线系列被放大,另一个固定不变。 Don't know how to fix this.不知道如何解决这个问题。 Need help from D3 ninjas.需要 D3 忍者的帮助。

Usually we would use both the datas and the line generator functions in an array: [{line: line, data: data1}, {line: line100, data: data2}] , bound to each SVG line with selectAll() .通常我们会在数组中同时使用[{line: line, data: data1}, {line: line100, data: data2}]和 line 生成器函数: [{line: line, data: data1}, {line: line100, data: data2}] ,使用selectAll()绑定到每个 SVG 行。

But because the data is the same for both lines, we can take a little shortcut and use only the line generators, using the same data each time:但是因为两条线的数据相同,我们可以走一点捷径,只使用线生成器,每次使用相同的数据:

svg.selectAll(".path")
    .data([line, line100])
    .join("path")
    .attr("d", lineGenerator => lineGenerator(data));

This selects all existing path elements, bind a line generator to each of them, and then use the line generators we bound to generate the new paths, using the data join .这将选择所有现有path元素,将线生成器绑定到每个元素,然后使用我们绑定的线生成器生成新路径,使用数据连接

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM