【问题标题】:Changing constant to variable in a comparison producing unexpected results在比较中将常量更改为变量会产生意想不到的结果
【发布时间】:2020-01-20 18:38:37
【问题描述】:

我一直在愚弄这个,我无法理解它。

这为我提供了我正在寻找的输出(彩色条取决于值)

我有以下代码:

t2actual: 
function(d) {
    if (d.value >= 44.5 && d.value <= 45.5) {
        return '#218340';
    } else if (d.value >= 44.0 && d.value <= 44.4 || d.value >= 45.6 && d.value <= 50) {
        return '#f7b731';
    } else {
        return '#a62337';
    }
}

参见迭代 #1 w/断点(为什么 d.value 未定义,但它有效?):

这并没有给我我正在寻找的输出(彩色条取决于值)

当我把它改成这个时,每次迭代我都会在 else 中结束:

t2actual: 
function(d) {
    arrayIndex++
    var setPoint = columns_TurbineConditions[0][arrayIndex];

    if ((setPoint - 0.5) <= d.value && d.value <= (setPoint + 0.5)) {
        return '#218340';
    } else if ((setPoint - 1) <= d.value && d.value <= (setPoint + 1)) {
        return '#f7b731';
    } else {
        return '#a62337';
    }
}

参见迭代 #1 w/断点(为什么 d.value 仍然未定义,但它不起作用?):

访问JS fiddle 获取完整代码。

编辑: 我考虑了 cmets 中建议的运算符优先级,以下对我不起作用:

if (((setPoint - 0.5) <= d.value) && (d.value <= (setPoint + 0.5))) {

【问题讨论】:

  • 运算符优先级浮现在脑海中......但一般来说,寻找重现错误的最小条件。
  • 您似乎正在显示调试器图像,两次,您似乎都在else 上。有什么不同?为什么d.value 未定义,而它显然不应该是?
  • @ASDFGerte 这是我的问题的一部分。一种情况有效,但另一种情况无效,但 C3.js 在两种情况下都返回“未定义”值。见上文。
  • 我不相信您的第一个函数实际上显示了它应该显示的内容。 d.value 在大多数情况下是未定义的。请参阅此fiddle。检查控制台。

标签: javascript d3.js c3.js


【解决方案1】:

经过一些研究,但在示例中:https://c3js.org/samples/data_color.html

基本上你需要在图表的data 属性上设置一个color 方法。我还清理了一些格式并重构了为t2actual 生成颜色的方式。从t2setpoint 获取值所需的索引位于数据对象d 中。但是,您需要向其添加 1 以跳过位于数组开头的标识符。

var columns_TurbineConditions = [
        ['t2setpoint', 45.1, 45, 45.4, 45, 45.2, 45, 45, 45, 45, 48.1, 45, 45],
        ['drybulb', 82.3, 82.3, 82.3, 82.3, 82.3, 82.3, 82.3, 82.3, 82.3, 82.3, 82.3, 82.3],
        ['t2actual', 46, 45, 45, 46, 47, 46, 45, 45, 45, 44, 45, 46]
    ];
        
var chart = c3.generate({
    bindto: '#charts_TurbineConditions',
    data: {
        columns: columns_TurbineConditions,
        axes: {
            t2setpoint: 'y',
            drybulb: 'y',
            t2actual: 'y2'
        },
        types: {
            't2setpoint': "spline",
            'drybulb': "spline",
            't2actual': "bar"
        },
        groups: [
            ['t2actual']
        ],
        colors: {
            t2setpoint: '#77777a',
            drybulb: '#4d4d4f',
            t2actual: '#ffffff' // set the legend color here
        },
        color: function(color, d) { 
            // d will be 'id' when called for legends
            if(typeof d !== 'object') {
                return color;
            }
            
            var setPoint = columns_TurbineConditions[0][d.index + 1];

            if (setPoint - 0.5 <= d.value && d.value <= setPoint + 0.5) {
                return '#218340';
            } 

            if (setPoint - 1 <= d.value && d.value <= setPoint + 1) {
                return '#f7b731';
            } 

            return '#a62337';     
        },
        names: {
            't2setpoint': 'T2 Setpoint (°F)',
            'drybulb': 'Dry Bulb (°F)',
            't2actual': 'T2 Actual (°F)'
        }
    },
    axis: {
        x: {
            type: 'category',
            label: {
                text: '',
                position: 'outer-center'
            },
            tick: {
                rotate: -75,
                multiline: false
            },
            height: 70,
            categories: ['Turbine 1', 'Turbine 2', 'Turbine 3', 'Turbine 4', 'Turbine 5', 'Turbine 6', 'Turbine 7', 'Turbine 8', 'Turbine 9', 'Turbine 10', 'Turbine 11', 'Turbine 12']
        },
        y: {
            min: 30,
            max: 100,
            label: {
                text: 'Dry Bulb',
                position: 'outer-middle'
            }
        },
        y2: {
            min: 30,
            max: 100,
            show: true,
            label: {
                text: 'T2 Actual',
                position: 'outer-middle'
            }
        }
    },
    bar: {
        width: 50
    },
    legend: {
        show: true,
    },
    padding: {
        bottom: 0,
        top: 0,
    },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.10/c3.min.css">

<div id="charts_TurbineConditions"></div>

【讨论】:

  • 太棒了!非常感谢您的帮助。
猜你喜欢
  • 2022-01-02
  • 2011-07-06
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
相关资源
最近更新 更多