【问题标题】:D3 how to properly get the key value INSIDE of a json objectD3如何正确获取json对象的键值INSIDE
【发布时间】:2017-08-12 16:46:07
【问题描述】:

我正在尝试根据返回的 json 对象的键值生成列标题。但是它以[0,1] 而不是[key[1],key[2]] 的形式返回。

这是我的 json 数据,我正在尝试使用 D3 来获取该对象的键(例如“Label”、“Count”)作为我的列标题,而不是静态插入它。

[
  {
  "Label": "External-Partner-Induced",
  "Count": 9
  },
{
 "Label": "Null",
 "Count": 1
},
{
 "Label": "FCTS-Induced",
 "Count": 66
},
{
 "Label": "EI-Partner-Induced",
 "Count": 78
 }
]

这是我的 d3 代码:

d3.json('dataQualityIssuesCategory.json', function (error,data) {

    function tabulate(data, columns) {
    var table = d3.select('body').append('table')
    var thead = table.append('thead')
    var tbody = table.append('tbody');

    // append the header row
    thead.append('tr')
      .selectAll('th')
      .data(columns).enter()
      .append('th')
        .text(function (column) { return column; });

    // create a row for each object in the data
    var rows = tbody.selectAll('tr')
      .data(data)
      .enter()
      .append('tr');


   // create a cell in each row for each column
    var cells = rows.selectAll('td')
      .data(function (row) {
        return columns.map(function (column) {
          return {column: column, value: row[column]};
        });
      })
      .enter()
      .append('td')
        .text(function (d) { return d.value; });
     return table;
     }

    // render the table
    tabulate(data, [d3.keys(data)[0], d3.keys(data)[1]]); // 2 column table
});

制表函数是我试图获取列标题的关键字段的地方,但上面的代码似乎获取的是整个对象而不是值 INSIDE。示例:[0,1] 作为列标题而不是 [Label, Count]

【问题讨论】:

    标签: javascript json d3.js


    【解决方案1】:

    请注意data 是一个对象数组,而不是一个对象。因此,要获取对象的键,您应该在数组中的一个对象上应用 d3.keys 函数。像这样——

    tabulate(data, d3.keys(data[0])); //data[0] is an object, applying d3.keys(data[0]) will return [Label, Count]
    

    var data = [{
        "Label": "External-Partner-Induced",
        "Count": 9
      },
      {
        "Label": "Null",
        "Count": 1
      },
      {
        "Label": "FCTS-Induced",
        "Count": 66
      },
      {
        "Label": "EI-Partner-Induced",
        "Count": 78
      }
    ];
    
    function tabulate(data, columns) {
      var table = d3.select('body').append('table')
      var thead = table.append('thead')
      var tbody = table.append('tbody');
    
      // append the header row
      thead.append('tr')
        .selectAll('th')
        .data(columns).enter()
        .append('th')
        .text(function(column) {
          return column;
        });
    
      // create a row for each object in the data
      var rows = tbody.selectAll('tr')
        .data(data)
        .enter()
        .append('tr');
    
    
      // create a cell in each row for each column
      var cells = rows.selectAll('td')
        .data(function(row) {
          return columns.map(function(column) {
            return {
              column: column,
              value: row[column]
            };
          });
        })
        .enter()
        .append('td')
        .text(function(d) {
          return d.value;
        });
      return table;
    }
    // render the table
    tabulate(data, d3.keys(data[0]));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多