【问题标题】:Add a single row with multiple columns to html table in d3在 d3 中的 html 表中添加单行多列
【发布时间】:2020-09-24 15:21:48
【问题描述】:

我正在使用以下代码将表格添加到我的 d3 图表中。

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

      // append the header row
      thead.append('td')
        .selectAll('th')
        .data(columns).enter()
        .append('th')
        .text((column) => column);

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

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

        return table;
    }

我需要在表格中添加一个垂直标题,并将 json 对象中的每个键读取到单行中的多个列中。如何在 d3 中实现这一点?请帮忙。

【问题讨论】:

  • 你能告诉我们你的data对象吗?
  • 下面是我的 json 数据模型:export interface ZRTData { A: string; B:号码; C:号码; D_percentage:数字;我需要在 d3 图表下添加带有标题 D_percentage 和 JSON 值的单行。我需要添加一个垂直标题和单行,其中包含来自 json 的具有不同 D_percentage 值的多列。

标签: javascript html css angular d3.js


【解决方案1】:

这是一个很好的例子,使用与您相同的函数tabulateBasic D3 Table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多