Coffeescript:未正确声明for循环中的条件变量

时间:2016-05-09 17:55:50

标签: javascript variables graph coffeescript conditional

我正在使用coffeescript来渲染绘图。我以前发过一个关于设置条件的问题;我相信现在已经解决了。但是,用于其中一个条件的变量curr_visibility正在引起我认为的问题,因为它没有正确定义。图表图基本上是这样的; a 0(不可见)或1(可见)被分配给图形上的每个点(这些点用于绘制一条线,该线基本上是来自使用DEM图像的地图的地形轮廓)。我附上了一个截图,说明了我的错误(LV = lastVisibilty和CV = curr_visibility)。变量curr_visibility在for循环中。我需要确保它在每次迭代后都会更新,但我不确定它是否已正确设置以在fillColor中工作:if条件语句。代码以两个空集开始 - line = []datasets = [] Plot graph showing the bug. The area between LV and CV should be red for No visibility

prev_visibility = data[0].visibility
        for elem, index in data
            curr_visibility = elem.visibility

            point = [
                index
                elem.geometry[2]
            ]

            line.push point
            unless prev_visibility is curr_visibility
                datasets.push line
                line = [point]
                prev_visibility = curr_visibility

        datasets.push line

        line = []
        lastVisibility = data[0].visibility

        newfillColor = if lastVisibilty == 0 && curr_visibility == 0
                "#C90E30"
            else if lastVisibilty == 0 && curr_visibility == 1
                "#439C32"
            else if lastVisibilty == 1 && curr_visibility == 0
                "#C90E30"
            else
                "#439C32"

        for set in datasets
            line.push 
                data: set,
                lines:
                    show: true
                    fill: true
                    opacity: 0.7
                    fillColor: newfillColor

            lastVisibility = 1 - lastVisibility    

1 个答案:

答案 0 :(得分:1)

好的,在同事的帮助下,我能够解决这个问题。首先,在上面的代码中,删除了变量prev_visibility的每个实例。确定没有必要。其次,我们确定需要利用索引方法来关联一个新变量next_visibility,它将每个迭代的点的当前可见性值与下一个点的当前可见性值进行比较(我希望我能正确地解释这一点) 。为此,我们添加了以下内容:

line.push point
if (index + 1) < data.length
    next_visibility = data[index + 1].visibility
else
    next_visibility = curr_visibility

unless next_visibility is curr_visibility
    datasets.push line
    line = [point]

最后,我删除了所有newFillColor内容,然后又恢复为fillColor: if lastVisibility is 0 then "#C90E30" else "439C32"