简体   繁体   English

D3使用对象数组从键中获取值

[英]D3 Getting values from keys using array of objects

So I read in a csv file on Country GDP, and I filtered out unwanted columns and countries. 因此,我读了一个关于“国家GDP”的csv文件,并过滤掉了不需要的列和国家。 Now I want to know how to extract the values from this array of objects and use them to create x and y axes for a multi-series line chart. 现在,我想知道如何从该对象数组中提取值,并使用它们为多系列折线图创建x和y轴。 Here is my code 这是我的代码

    function country(element) {
    return element.CountryName == "Brazil" ||
            element.CountryName == "China" ||
            element.CountryName == "Russian Federation" ||
            element.CountryName == "United Kingdom" ||
            element.CountryName == "United States" ||
            element.CountryName == "Germany";
}
d3.csv("GDP.csv", function(d) {
    return {
        CountryName : d["Country Name"],
        Indicator : d["Indicator Name"],
        y2004 : d["2004"],
        y2005 : d["2005"],
        y2006 : d["2006"],
        y2007 : d["2007"],
        y2008 : d["2008"],
        y2009 : d["2009"],
        y2010 : d["2010"],
        y2011 : d["2011"],
        y2012 : d["2012"]
    };
},
    function(error, rows) {
    //document.write(rows);
    dataset = rows;
    var filtered = dataset.filter(country);
    console.log(filtered);

    //Code to create axes and map keys
});

Now I currently have a array of 6 objects, each object having values at the specifically named keys. 现在,我目前有一个由6个对象组成的数组,每个对象在专门命名的键处都有值。 Examples I've seen usually read in key values from the header line of the files, but how would I approach that attempt with this? 我见过的示例通常会从文件的标题行读取键值,但是我将如何尝试这种方法呢?

The X-axis is for the years 2004-2012, which are column names and key values. X轴表示2004-2012年,即列名和键值。 Y-axis is the range of those values at those years. Y轴是当年这些值的范围。 The resulting 6 lines are for each country. 结果6行是针对每个国家的。

Example I'm trying to follow is http://bl.ocks.org/mbostock/3884955 我要遵循的示例是http://bl.ocks.org/mbostock/3884955

Would I need to parse dates? 我需要解析日期吗? I don't think so since I'm just working with year numbers and they are key values, not values of a key. 我不这么认为,因为我只是处理年份数字,它们是关键值,而不是关键值。 Thanks. 谢谢。

I would assemble my data so that you have 4 keys for each point: 我将整理数据,以便每个点有4个键:

{country:"Germany", indicator:"...",  year:2004, value:55}

rather than having keys for each year. 而不是每年都有钥匙。 Just post-process your csv call. 只需对您的csv调用进行后期处理。

As you are doing that, determine the min/max years and the min/max values. 在执行此操作时,请确定最小/最大年份和最小/最大值。

Next you build a year scale(x axis) using a domain of [minYear, maxYear]. 接下来,您将使用[minYear,maxYear]域建立年份标度(x轴)。 Next build a value scale for the y-axis using a domain of [minValue, maxValue]. 接下来,使用[minValue,maxValue]的域为y轴建立一个值刻度。

D3 wants to consume arrays of objects with identical sets of keys. D3希望使用具有相同键集的对象数组。 If you must, have pre-processing stage where you mogrify the data into something that looks like: 如果需要,请进行预处理,在此阶段,您可以将数据迁移为类似以下内容的内容:

[
    { key: value, key2: value },
    { key: value, key2: value },
    { key: value, key2: value },
    { key: value, key2: value },
    { key: value, key2: value }
]

Otherwise you will be having a very terrible time with D3, as most of the functional programming features of the toolkit will not work at all for you. 否则,您将在D3上度过非常糟糕的时光,因为该工具包的大多数功能编程功能对您来说根本无法使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM