简体   繁体   English

D3折线图问题

[英]D3 line graph issue

I have d3 line graph that is constantly updated with new set of data. 我有d3折线图,它会不断用新的数据集进行更新。 The issue is my line graph is drawn above the some rectangular blocks. 问题是我的折线图绘制在一些矩形块上方。 On page load my line graph is always in the front of the rect but after the page is being refreshed the line graph is going behind the rectangular block. 在页面加载时,我的折线图总是在矩形的前面,但是刷新页面后,折线图在矩形块后面。 Can any of you help me fix this problem ? 你们任何人都可以帮助我解决此问题吗?

My code is set up like this 我的代码是这样设置的

function drawRect(SVG, cData, type) {
            let selector = '.ca';
            let className = 'c a';
            let tcHeight = verticalSize + MIN_CELL_PADDING;
            let getTranslateString = function (index) {
            let yVal = columnHeight - ((index + 1) * tcHeight);
            return `translate(${xVal}, ${yVal})`;
            let rects = d3.select(columnSVG)
                .selectAll(selector)
                .data(cData.filter((d) => {
                    return d;
                }));
            rects.enter()
                .append('g')
                .attr('class', className)
                .attr('transform', (d, ix) => {
                return getTranslateString(ix);
                })
                .each(function () {
                    d3.select(this)
                        .append('rect')
                        .attr('width', cellSize)
                        .attr('height', verticalSize)
                        .attr('rx', 4)
                        .attr('ry', 4)
                        .attr('time', (d) => {
                            return cData.date;
                        })
                        .attr('fill', (d) => {
                            return changeColor(d);
                        });
                });

            rects.transition()
                .attr('transform', (d, ix) => {
                    return getTranslateString(ix);
                });

            rects.each(function (d) {
                let node = d3.select(this);
                node.selectAll('rects').transition()
                    .attr('width', cellSize)
                    .attr('height', verticalSize)
                    .attr('rx', 4)
                    .attr('ry', 4)
        }

    function drawOline(aData, dData, time) {
                let aLine = d3.svg.line()
                    .defined((d) => {
                        return !isNaN(d.Ptile);
                    })
                    .x((d) => {
                        return ptime(moment(d.day).utc());
                    })
                    .y((d) => {
                        return aY(d.Ptile);
                    });

                let dLine = d3.svg.line()
                    .defined((d) => {
                        return !isNaN(d.Ptile);
                    })
                    .x((d) => {
                        return ptime(moment(d.day).utc());
                    })
                    .y((d) => {
                        return dY(d.Ptile);
                    });

                if (aData && aData.length > 0) {
                    if (g.select('.aline')[0][0] == null) {
                        g.append('g')
                            .append('path')
                            .datum(aData)
                            .attr('class', 'line aline')
                            .attr('fill-opacity', 1.0)
                            .attr('d', aline);
                    } else {
                        g.select('.aline')
                            .datum(aData)
                            .transition()
                            .attr('fill-opacity', 1.0)
                            .attr('d', aline);
                    }
                } else {
                    g.select('.aline')
                        .transition()
                        .attr('fill-opacity', 1.0)
                        .attr('d', aline);
                }

                if (dData && dData.length > 0) {
                    if (g.select('.dline')[0][0] == null) {
                        g.append('g')
                            .append('path')
                            .datum(dData)
                            .attr('class', 'line dline')
                            .attr('fill-opacity', 1.0)
                            .attr('d', dline);
                    } else {
                        g.select('.dline')
                            .datum(dData)
                            .transition()
                            .attr('fill-opacity', 1.0)
                            .attr('d', dline);
                    }
                } else {
                    g.select('.dline')
                        .transition()
                        .attr('fill-opacity', 1.0)
                        .attr('d', dline);
                }
            }

The visual occlusion (hiding) of some SVG objects by others (eg lines by rects, or vice versa) is very dependent on their drawing order. 某些SVG对象被其他对象(例如,线条按矩形,反之亦然)的视觉遮挡(隐藏)非常取决于它们的绘制顺序。 Unlike HTML/CSS, SVG does not have a true z-index or "what's on top?" 与HTML / CSS不同,SVG没有真正的z-index或“最重要的是什么?” indicator. 指示符。

The trick is often to draw the items you want to see on top last. 诀窍通常是在最上面绘制要查看的项目。 That's not always convenient, however. 但是,这并不总是很方便。 For example, you may not way to redraw lines every time you redraw the blocks. 例如,您可能无法在每次重画块时都重画线。

A way to preserve the visual ordering of objects, even when they're redrawn, is to put them into <g> groups. 即使重新绘制对象,也可以保留对象的视觉顺序,即将它们放在<g>组中。 The ordering of the groups need not change, even if the items are updated. 即使项目已更新,组的顺序也无需更改。 For example: 例如:

var rectsG = svg.append('g').attr('class', 'rects');
var linesG = svg.append('g').attr('class', 'lines');

Then instead of drawing into the global svg element, direct your appends to individual groups. 然后,不要将全局对象添加到单个svg元素中。 They will act as layers: 它们将充当图层:

linesG.append('line')
      ...more here...
rectsG.append('rect')
      ...more here...

Because the groups are ordered in the document top to bottom, it really doesn't matter what order you draw or redraw their constituent elements. 由于这些组在文档中是从上到下排序的,因此绘制或重新绘制其组成元素的顺序实际上并不重要。 The ordering of the <g> containers is what will determine visual occlusion. <g>容器的顺序将决定视觉遮挡。

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

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