(
container: Drawable,
// tslint:disable-next-line:no-any
data: TableData, opts: {fontSize?: number} = {})
| 46 | * @doc {heading: 'Charts', namespace: 'render'} |
| 47 | */ |
| 48 | export function table( |
| 49 | container: Drawable, |
| 50 | // tslint:disable-next-line:no-any |
| 51 | data: TableData, opts: {fontSize?: number} = {}) { |
| 52 | if (data && data.headers == null) { |
| 53 | throw new Error('Data to render must have a "headers" property'); |
| 54 | } |
| 55 | |
| 56 | if (data && data.values == null) { |
| 57 | throw new Error('Data to render must have a "values" property'); |
| 58 | } |
| 59 | |
| 60 | const drawArea = getDrawArea(container); |
| 61 | |
| 62 | const options = Object.assign({}, defaultOpts, opts); |
| 63 | |
| 64 | let table = d3Select(drawArea).select('table.tf-table'); |
| 65 | |
| 66 | const tableStyle = css({ |
| 67 | fontSize: options.fontSize || '.875rem', |
| 68 | width: '100%', |
| 69 | maxWidth: '64rem', |
| 70 | marginRight: 'auto', |
| 71 | marginLeft: 'auto', |
| 72 | }); |
| 73 | |
| 74 | // If a table is not already present on this element add one |
| 75 | if (table.size() === 0) { |
| 76 | table = d3Select(drawArea).append('table'); |
| 77 | |
| 78 | table.attr('class', ` ${tableStyle} tf-table`); |
| 79 | |
| 80 | table.append('thead').append('tr'); |
| 81 | table.append('tbody'); |
| 82 | } |
| 83 | |
| 84 | if (table.size() !== 1) { |
| 85 | throw new Error('Error inserting table'); |
| 86 | } |
| 87 | |
| 88 | // |
| 89 | // Add the reader row |
| 90 | // |
| 91 | const headerRowStyle = css({ |
| 92 | fontWeight: '600', |
| 93 | borderBottomStyle: 'solid', |
| 94 | borderBottomWidth: '1px', |
| 95 | borderColor: 'rgba( 0, 0, 0, .2 )', |
| 96 | textAlign: 'left', |
| 97 | paddingBottom: '1rem', |
| 98 | paddingRight: '1rem', |
| 99 | backgroundColor: '#fff', |
| 100 | }); |
| 101 | const headers = |
| 102 | table.select('thead').select('tr').selectAll('th').data(data.headers); |
| 103 | const headersEnter = |
| 104 | headers.enter().append('th').attr('class', `${headerRowStyle}`); |
| 105 | headers.merge(headersEnter).html(d => d); |
no test coverage detected
searching dependent graphs…