簡體   English   中英

d3.nest有多列

[英]d3.nest with multiple columns

我有一個具有以下結構的csv,並且我想在d3中創建多條折線圖:

date,value1,value2,value3
1924-01-01,433.57,16.21,122.09
1925-01-01,247.68,18.55,115.38

為此,我需要獲得以下結構:

date,category,n
1924-01-01,value1,433.57
1924-01-01,value2,16.21
1924-01-01,value3,122.09
1925-01-01,value1,247.68
1925-01-01,value2,18.55
1925-01-01,value3,115.38

我嘗試了不同的方法,但沒有成功。 例如:

d3.csv("data.csv",function(data) {

var values = ['value1','value2','value3']

var nestedData=d3.nest()
  .key(function(d) {return d.values;})
  .sortKeys(d3.ascending)
  .entries(data)

我究竟做錯了什么?

先感謝您。

沒有d3.nest()此示例執行的操作與您正在尋找的功能完全相同。 那里的數據結構與您的數據結構相同(除了它是制表符而且沒有逗號分隔):

date    New York    San Francisco   Austin
20111001    63.4    62.7    72.2
20111002    58.0    59.9    67.7
20111003    53.3    59.1    69.4

這是示例中進行數據轉換的代碼(添加了注釋以供解釋):

// get the names of all the different series (everything that's not called "date")
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));

// parse the dates
data.forEach(function(d) {
  d.date = parseDate(d.date);
});

// restructure the data
var cities = color.domain().map(function(name) {
  // for each series, create an object with its name and its values
  return {
    name: name,
    // extract the value for the series we're processing at the moment from the data
    values: data.map(function(d) {
      return {date: d.date, temperature: +d[name]};
    })
  };
});

要使用此代碼,您唯一需要更改的就是日期格式。 您可能還想更改變量名稱,以使您清楚要處理的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM