在下面的应用程序中,有一些奇怪的谜团:我为什么不能为我的情节使用某些名字?
该应用程序被构建为虚拟应用程序,以更改绘图中的跟踪颜色。1个按钮影响跟踪…
我不明白为什么行为取决于ID,但这是一个解决方法:
jscolor <- c( "function toggleColor(id){", " var color = document.getElementById(id).value;", # get the color of the colourpicker " var ids = id.split('_');", # split the id " var plotAid = ids[2];", #get the id of plotA (plotw or 3) " var plotBid = ids[3];", #get the id of plotB (plot2 or 4) " var index = parseInt(ids[4]) - 1;", #get the trace number to target " var plotA = document.getElementById(plotAid);", #get the plot element " if(plotA.innerHTML !== ''){", " var dataA = plotA.data;", #access the plot data " var markerA = dataA[index].marker;", #access the plot's markers " markerA.color = color;", # set the marker color " Plotly.restyle(plotA, {marker: markerA}, [index]);", #restyle plotA " }", " var plotB = document.getElementById(plotBid);", # repeat steps for plot2 " if(plotB.innerHTML !== ''){", " var dataB = plotB.data;", " var markerB = dataB[index].marker;", " markerB.color = color;", " Plotly.restyle(plotB, {marker: markerB}, [index]);", " }", "};" )
如OP所述,使用此解决方法启动时存在问题。这是另一种解决方法。它只增加了1ms的延迟。
jscolor <- c( "function toggleColor0(id){", " var color = document.getElementById(id).value;", # get the color of the colourpicker " var ids = id.split('_');", # split the id " var plotAid = ids[2];", #get the id of plotA (plotw or 3) " var plotBid = ids[3];", #get the id of plotB (plot2 or 4) " var index = parseInt(ids[4]) - 1;", #get the trace number to target " var plotA = document.getElementById(plotAid);", #get the plot element " var dataA = plotA.data;", #access the plot data " var markerA = dataA[index].marker;", #access the plot's markers " markerA.color = color;", # set the marker color " Plotly.restyle(plotA, {marker: markerA}, [index]);", #restyle plotA " var plotB = document.getElementById(plotBid);", # repeat steps for plot2 " var dataB = plotB.data;", " var markerB = dataB[index].marker;", " markerB.color = color;", " Plotly.restyle(plotB, {marker: markerB}, [index]);", "};", "function toggleColor(id){", " setTimeout(function(){toggleColor0(id);}, 1);", "}" )
实际上这也适用于0ms的延迟:
setTimeout(function(){toggleColor0(id);}, 0);