简体   繁体   English

d3.js重新排列数据列以进行绘图

[英]d3.js Rearranging columns of data for plot

I've been trying for a while but I am unsure of what keyword I need to search for. 我已经尝试了一段时间,但是不确定要搜索哪个关键字。 Anyways, I am interested in changing how the data in d3 is loaded so I can rearrange columns in plots. 无论如何,我有兴趣更改d3中数据的加载方式,以便重新布置图中的列。 In R terms, what I am looking for would be about the equivalent of changing factor levels to be able to rearrange columns in a bar plot. 用R术语来说,我要寻找的是等价于变化的因子水平,以便能够重新排列条形图中的列。

For example, in the link to a block below, I want to manually set which order the parallel coordinates are. 例如,在下面的块链接中,我想手动设置平行坐标的顺序。 Currently, it shows economy, cylinders, to year left to right. 当前,它从左到右显示年份(气缸)的经济性。 I want to do something like year, weight, displacement, etc. 我想做一些像年,体重,排量等的事情。

https://bl.ocks.org/jasondavies/1341281 https://bl.ocks.org/jasondavies/1341281

The order of the columns is determined by d3.keys(cars[0]) in this snippet: 列的顺序由以下代码段中的d3.keys(cars[0])确定:

x.domain(dimensions = d3.keys(cars[0]).filter(function(d) {
    return d != "name" && (y[d] = d3.scale.linear()
        .domain(d3.extent(cars, function(p) {
            return +p[d];
        }))
        .range([height, 0]));
}));

For instance, here is the same code, but using... 例如,这是相同的代码,但是使用...

d3.keys(cars[0]).sort()

... to sort the array alphabetically: https://bl.ocks.org/anonymous/9fb7329fed0e2ea539bab5bc5a47a16c ...按字母顺序对数组进行排序: https : //bl.ocks.org/anonymous/9fb7329fed0e2ea539bab5bc5a47a16c

So, if you want a specific order, you can simply pass the desired array: 因此,如果您想要特定的订单,则可以简单地传递所需的数组:

x.domain(dimensions = ["year", , "weight (lb)", "name", "displacement (cc)", "economy (mpg)", "cylinders", "power (hp)", "0-60 mph (s)"].filter(function(d) {
    return d != "name" && (y[d] = d3.scale.linear()
        .domain(d3.extent(cars, function(p) {
            return +p[d];
        }))
        .range([height, 0]));
}));

Here is the blocks: https://bl.ocks.org/anonymous/4f1798a5e658df7986f70effa030497c 这是块: https : //bl.ocks.org/anonymous/4f1798a5e658df7986f70effa030497c

To accomplish this, you can take the csv file (ie cars.csv) and edit in excel spreadsheet. 为此,您可以获取csv文件(即cars.csv)并在excel电子表格中进行编辑。 There you can transpose the column as you wish and copy back to your d3js function. 在那里,您可以根据需要转置该列,然后复制回d3js函数。 Look at the example below (Done with excel): 查看下面的示例(使用excel完成):

name,year,weight (lb),displacement (cc),economy (mpg),cylinders,power (hp),0-60 mph (s)

AMC Ambassador Brougham,73,3821,360,13,8,175,11 AMC Ambassador DPL,70,3850,390,15,8,190,8.5 AMC Ambassador SST,72,3672,304,17,8,150,11.5 AMC Concord DL 6,79,3265,232,20.2,6,90,18.2 AMC布洛厄姆大使73,3821,360,13,8,175,11 AMC DPL大使,70,3850,390,15,8,190,8.5 AMC SST大使,72,3672,304,17,8,150,11.5 AMC Concord DL 6, 79,3265,232,20.2,6,90,18.2

Etc... I didn't want it to extend too much... 等等...我不想让它扩展太多...

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

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