我正在使用带有candlestick chart
的Reactjs来创建JSON.parse(JSON.stringify())
。
我正在尝试创建一个计算简单移动平均值的函数,并将数据存储到每个迭代的数组中。与手动方法相比,循环给我一个输出。输出来自输入数组的最后一个元素。这让我觉得这是由于一些引用问题并尝试使用Object.assign()
和renderList=[]
formulaList=[]
accessorList=[]
但仍然无效。我想知道为什么手动模式有效,迭代方法没有,以及如何进行。
外部变量
for(var i=0;i<windowSizeList.length;i++){
var _sma = sma()
.id(i)
.options({ windowSize: windowSizeList[i] })
.merge((d, c) => { d['sma_'+i] = c })
.accessor(d => d['sma_'+i])
.stroke(colorList[i])
var _sma_=Object.assign(_sma)
this.formulaList.push(_sma_)
this.accessorList.push(_sma_.accessor())
this.renderList.push(<LineSeries yAccessor={_sma_.accessor()}
stroke={_sma_.stroke()}/>)
this.renderList.push(<CurrentCoordinate yAccessor={_sma_.accessor()} fill={_sma_.stroke()} />)
}
这是迭代模式
//------------0
var _sma0 = sma()
.id(0)
.options({ windowSize: windowSizeList[0] })
.merge((d, c) => { d['sma_'+0] = c })
.accessor(d => d['sma_'+0])
.stroke(colorList[0])
var _sma_=_sma0
this.formulaList.push(_sma_)
this.accessorList.push(_sma_.accessor())
this.renderList.push(<LineSeries yAccessor={_sma_.accessor()} stroke={_sma_.stroke()}/>)
this.renderList.push(<CurrentCoordinate yAccessor={_sma_.accessor()} fill={_sma_.stroke()} />)
//------------1
var _sma0 = sma()
.id(1)
.options({ windowSize: windowSizeList[1] })
.merge((d, c) => { d['sma_'+1] = c })
.accessor(d => d['sma_'+1])
.stroke(colorList[1])
var _sma_=_sma0
this.formulaList.push(_sma_)
this.accessorList.push(_sma_.accessor())
this.renderList.push(<LineSeries yAccessor={_sma_.accessor()} stroke={_sma_.stroke()}/>)
this.renderList.push(<CurrentCoordinate yAccessor={_sma_.accessor()} fill={_sma_.stroke()} />)
//------------2
var _sma0 = sma()
.id(2)
.options({ windowSize: windowSizeList[2] })
.merge((d, c) => { d['sma_'+2] = c })
.accessor(d => d['sma_'+2])
.stroke(colorList[2])
var _sma_=_sma0
this.formulaList.push(_sma_)
this.accessorList.push(_sma_.accessor())
this.renderList.push(<LineSeries yAccessor={_sma_.accessor()} stroke={_sma_.stroke()}/>)
this.renderList.push(<CurrentCoordinate yAccessor={_sma_.accessor()} fill={_sma_.stroke()} />)
我得到的输出
这是手动模式
MULTILINE