d3.max()没有返回数组中的最大值? [英] d3.max() not returning the max value in an array?

查看:392
本文介绍了d3.max()没有返回数组中的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在教自己d3,似乎无法弄清楚我遇到的这个问题。我正在动态地为 d3.max()方法提供一个数组数组(我尝试使用 parseInt() / parseFloat()以及不解析它们)通过HTML < select> 元素。我正在使用控制台检查值,因为我的可视化变得歪斜。但是,有时它会返回最大值,有时它不会返回。

I've been teaching myself d3 overnight and can't seem to figure out this one issue I am having. I am dynamically feeding the d3.max() method an array of numbers (I have tried parsing them with parseInt()/parseFloat() as well as not parsing them) through an HTML <select> element. I am using the console to check the values as my visualizations are becoming skewed. However, sometimes it does return the max value, and sometimes it just doesn't.

var localityElectricityConsumption = localities[localityName].totalElectricityConsumption;
            maxConsumption = d3.max(localityElectricityConsumption);
            r = d3.scale.linear().domain([0, maxConsumption]).range([120, 0]);

            console.log("array being fed to d3.max: " + localityElectricityConsumption);
            console.log("what d3.max() is returning: " + d3.max(localityElectricityConsumption));
            console.log("what parseInt(d3.max()) is returning: " + d3.max(localityElectricityConsumption));

您可以看到实时可视化并检查控制台这里。尝试使用最顶层的图表从北美地区切换到墨西哥地区。 d3.max()返回北美的最大值,但不返回墨西哥,因此不会正确显示墨西哥的数据。我正式难过。任何帮助将不胜感激。

You can see the live visualization and check the console here. Try using the top-most graph to switch from the North American locality to the Mexico locality. d3.max() returns the max value for North America, but not Mexico, and therefore doesn't graph the data correctly for Mexico. I am officially stumped. Any help would be appreciated.

编辑:

以下是用于创建<$ c的代码$ c>地方[localityName] .totalElectricityConsumption

 d3.csv("data/" + dataset, function (data) {

            for (var i = 0; i < data.length; i++) {

                var record = data[i];

                if (dataset === "total_electricity_consumption.csv") {
                    currentDataset = listOfDatasets[0].name;
                    record.totalElectricityConsumption = [];

                    // loop through all years, from 1980 to 2012
                    for (var year = 1980; year <= 2012; year++) {
                        var value = record[year];

                        // deal with missing data points
                        if (value === '--') {
                            value = 0;
                        }
                        else if (value === 'NA') {
                            value = 0;
                        }
                        else if (value === '(s)') {
                            value = 0;
                        }
                        else if (value === 'W') {
                            value = 0;
                        }

                        // add record of current total electricity consumption
                        record.totalElectricityConsumption.push(value);
                    }
                }

// create a list of country names
                localities[record.Locality] = record;
                listOfLocalities.push(record.Locality);
                }
}

对不起,格式化并不完美,因为我不得不采取一些东西可以使代码更易于Stack Overflow读取。

Sorry, formatting is not perfect as I had to take some stuff out to make the code more readable for Stack Overflow.

推荐答案

正如所料, d3.max ()确实返回了最大值...但是,它返回字符串中的最大值,而不是数字中的最大值。

As expected, d3.max() is indeed returning the maximum value... however, it is returning the maximum value among strings, not among numbers.

解释是,默认情况下, d3.csv()会将所有值加载为字符串,即使它们是数字。因此,当你这样做时:

The explanation is that, by default, d3.csv() will load all the values as strings, even if they are numbers. Therefore, when you do:

var value = record[year];

value 是一个字符串,而不是一个数字。在JavaScript中,字符串数组的最大值与数组数组的最大值不同。例如,使用数字:

value is a string, not a number. And in JavaScript the maximum for an array of strings is different from the maximum for an array of numbers. For instance, using numbers:

var myArray = [1, 65, 9, 32, 42];
console.log(d3.max(myArray));

<script src="https://d3js.org/d3.v4.min.js"></script>

这是预期值。但是看看如果我们使用字符串会发生什么:

That's the expected value. But look what happens if we use strings:

var myArray = ["1", "65", "9", "32", "42"];
console.log(d3.max(myArray));

<script src="https://d3js.org/d3.v4.min.js"></script>

解决方案:将您的值更改为数字:

Solution: change your values to numbers:

var value = +record[year];

这篇关于d3.max()没有返回数组中的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆