gpt4 book ai didi

javascript - d3 - v5 文本渲染比 v3.5 慢?

转载 作者:行者123 更新时间:2023-11-28 16:46:02 27 4
gpt4 key购买 nike

我已将 d3 树从 v3.5 迁移到 v5。现在我发现使用Patrick Brockmann’s search and highlight functionality在树上执行搜索时v5版本有相当大的滞后。 。

使用下拉菜单 Select2 进行搜索时会出现延迟。所有叶节点扩展速度不够快,导致 v5 版本中出现几秒钟的大量黑色文本。 v3.5 版本中不会发生这种情况。

enter image description here

有什么想法可以优化它以获得与树的 v3.5 版本相同的性能吗?两个版本的JSON数据是相同的。

d3 v3.5 fiddle

d3 v5 fiddle

搜索功能中的代码(v3.5 和 v5 版本相同):

            function select2DataCollectName( d ) {
if ( d.children )
d.children.forEach( select2DataCollectName );
else if ( d._children )
d._children.forEach( select2DataCollectName );
if ( !d.children && d.data.type == 'learning_event' ) select2Data.push( d.data.name );
}

//===============================================
function searchTree( d ) {
if ( d.children )
d.children.forEach( searchTree );
else if ( d._children )
d._children.forEach( searchTree );
var searchFieldValue = eval( searchField );
if ( searchFieldValue && searchFieldValue.toLowerCase().match( searchText.toLowerCase() ) ) {
// Walk parent chain
var ancestors = [];
var parent = d;
while ( parent !== null && typeof ( parent ) !== "undefined" ) {
ancestors.push( parent );
//console.log(parent);
parent.class = "found";
parent = parent.parent;
}
}
}

//===============================================
function clearAll( d ) {
d.class = "";
if ( d.children )
d.children.forEach( clearAll );
else if ( d._children )
d._children.forEach( clearAll );
}
//===============================================
function collapse( d ) {

if ( d.children ) {
d._children = d.children;
//set the parent object in all the children
d._children.forEach( function ( d1 ) {
d1.parent = d;
collapse( d1 );
} );
d.children = null;
}
}
//===============================================
function collapseAllNotFound( d ) {
if ( d.children ) {
if ( d.class !== "found" ) {
d._children = d.children;
d._children.forEach( collapseAllNotFound );
d.children = null;
} else
d.children.forEach( collapseAllNotFound );
}
}
//===============================================
function expandAll( d ) {
if ( d._children ) {
d.children = d._children;
d.children.forEach( expandAll );
d._children = null;
} else if ( d.children )
d.children.forEach( expandAll );
}

//===============================================
// Toggle children on click.
function toggle( d ) {
if ( d.children ) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
clearAll( root );
update( d );
$( "#search" ).select2( "val", "" );
}

//===============================================
$( "#search" ).on( "select2-selecting", function ( e ) {
clearAll( root );
expandAll( root );
update( root );
searchField = "d.data.name";
searchText = e.object.text;
searchTree( root );
root.children.forEach( collapseAllNotFound );
update( root );
} )


var colourScale = d3.scaleOrdinal()
.domain(["MD", "Year 1", "Year 2", "Year 3", "Year 4"])
.range(["#abacab", "#53e28c", "#4b80fa", "#eab014", "#bd57fe"]);

// Set the dimensions and margins of the diagram
var margin = {top: 20, right: 90, bottom: 30, left: 0},
width = 2000 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" +
margin.left + "," + margin.top + ")");



var i = 0,
duration = 750,
root;

// declares a tree layout and assigns the size
var treemap = d3.tree().size([height, width]);

// Assigns parent, children, height, depth
root = d3.hierarchy(treeData, function(d) {
return d.children;
});
root.x0 = height / 2;
root.y0 = 0;

// Collapse after the second level
root.children.forEach(collapse);

update(root);


select2Data = [];
select2DataCollectName( root );
select2DataObject = [];
select2Data.sort( function ( a, b ) {
if ( a > b ) return 1; // sort
if ( a < b ) return -1;
return 0;
} )
.filter( function ( item, i, ar ) {
return ar.indexOf( item ) === i;
} ) // remove duplicate items
.filter( function ( item, i, ar ) {
select2DataObject.push( {
"id": i,
"text": item
} );
} );
$( "#search" ).select2( {
placeholder: "Select a Learning Event...",
data: select2DataObject,
containerCssClass: "search"
} );

最佳答案

您的标题是标题诱饵,因为两个版本之间的性能没有任何差异。听起来像是您正在报告回归。 :)

正如 @RobertHarvey 所指出的,不同之处在于文本进入时不会从透明状态淡入。看起来您在将代码从 D3v3 移植到 D3v5 时错过了 fill-opacity

    nodeEnter.append('text')
// ...
.style( "fill-opacity", 1e-6 )

    var nut = nodeUpdate.transition()
// ...
nut.select( "text" )
.style( "fill-opacity", 1 );

Fork of your d3v5 fiddle .

关于javascript - d3 - v5 文本渲染比 v3.5 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60536881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com