Tables in JavaScript

How to make a D3.js-based tables in javascript.


New to Plotly?

Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

var values = [
      ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'],
      [1200000, 20000, 80000, 2000, 12120000],
      [1300000, 20000, 70000, 2000, 130902000],
      [1300000, 20000, 120000, 2000, 131222000],
      [1400000, 20000, 90000, 2000, 14102000]]

var data = [{
  type: 'table',
  header: {
    values: [["<b>EXPENSES</b>"], ["<b>Q1</b>"],
				 ["<b>Q2</b>"], ["<b>Q3</b>"], ["<b>Q4</b>"]],
    align: "center",
    line: {width: 1, color: 'black'},
    fill: {color: "grey"},
    font: {family: "Arial", size: 12, color: "white"}
  },
  cells: {
    values: values,
    align: "center",
    line: {color: "black", width: 1},
    font: {family: "Arial", size: 11, color: ["black"]}
  }
}]

Plotly.newPlot('myDiv', data);
Click to copy
SalariesOfficeMerchandiseLegalTOTALEXPENSES12000002000080000200012120000Q1130000020000700002000130902000Q21300000200001200002000131222000Q314000002000090000200014102000Q4
var values = [
      ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'],
      [1200000, 20000, 80000, 2000, 12120000],
      [1300000, 20000, 70000, 2000, 130902000],
      [1300000, 20000, 120000, 2000, 131222000],
      [1400000, 20000, 90000, 2000, 14102000]]

var data = [{
  type: 'table',
  header: {
    values: [["<b>EXPENSES</b>"], ["<b>Q1</b>"],
				 ["<b>Q2</b>"], ["<b>Q3</b>"], ["<b>Q4</b>"]],
    align: ["left", "center"],
    line: {width: 1, color: '#506784'},
    fill: {color: '#119DFF'},
    font: {family: "Arial", size: 12, color: "white"}
  },
  cells: {
    values: values,
    align: ["left", "center"],
    line: {color: "#506784", width: 1},
	 fill: {color: ['#25FEFD', 'white']},
    font: {family: "Arial", size: 11, color: ["#506784"]}
  }
}]

Plotly.newPlot('myDiv', data);
Click to copy
SalariesOfficeMerchandiseLegalTOTALEXPENSES12000002000080000200012120000Q1130000020000700002000130902000Q21300000200001200002000131222000Q314000002000090000200014102000Q4
d3.csv("https://raw.githubusercontent.com/plotly/datasets/master/Mining-BTC-180.csv", function(err, rows){

  function unpack(rows, key) {
  return rows.map(function(row) { return row[key]; });
  }

  var headerNames = d3.keys(rows[0]);

  var headerValues = [];
  var cellValues = [];
  for (i = 0; i < headerNames.length; i++) {
    headerValue = [headerNames[i]];
    headerValues[i] = headerValue;
    cellValue = unpack(rows, headerNames[i]);
    cellValues[i] = cellValue;
  }

  // clean date
  for (i = 0; i < cellValues[1].length; i++) {
  var dateValue = cellValues[1][i].split(' ')[0]
  cellValues[1][i] = dateValue
  }


var data = [{
  type: 'table',
  columnwidth: [150,600,1000,900,600,500,1000,1000,1000],
  columnorder: [0,1,2,3,4,5,6,7,8,9],
  header: {
    values: headerValues,
    align: "center",
    line: {width: 1, color: 'rgb(50, 50, 50)'},
    fill: {color: ['rgb(235, 100, 230)']},
    font: {family: "Arial", size: 8, color: "white"}
  },
  cells: {
    values: cellValues,
    align: ["center", "center"],
    line: {color: "black", width: 1},
    fill: {color: ['rgba(228, 222, 249, 0.65)','rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)']},
    font: {family: "Arial", size: 9, color: ["black"]}
  }
}]

var layout = {
  title: {
    text: "Bitcoin mining stats for 180 days"
  }
}

Plotly.newPlot('myDiv', data, layout);
});
Click to copy
0123456789101112131415161718192021222324252017-04-292017-04-302017-05-012017-05-022017-05-032017-05-042017-05-052017-05-062017-05-072017-05-082017-05-092017-05-102017-05-112017-05-122017-05-132017-05-142017-05-152017-05-162017-05-172017-05-182017-05-192017-05-202017-05-212017-05-222017-05-232017-05-24Date341319281489294786333161295149354737267193363022316011365096332879311391294743317698329266369098329229233977317527288904319502352805326057327868367710338642Number-transactions44889163918072389212440997043425069435917933472274359179433323246705494021862411977337579014286791439813645651544732172367439243146273952755456515445929904091937406410049767384562010Output-volume(BTC)31191792720216287827831495532760373350074627791703610789350562040621733715394366504134680773731718388582140180284090703335633242412643986175474688048623174331490449910556689465524488Market-price44889163918072389212440997043425069435917933472274359179433323246705494021862411977337579014286791439813645651544732172367439243146273952755456515445929904091937406410049767384562010Hash-rate910101010101110111212121212121113151414151414141617Cost-per-trans-USD31191792720216287827831495532760373350074627791703610789350562040621733715394366504134680773731718388582140180284090703335633242412643986175474688048623174331490449910556689465524488Mining-revenue-USD256199228273247307261297277316303303280322301297327343486392443388353407468473Transaction-fees-BTC
Bitcoin mining stats for 180 days
var values = [
      ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL<br>EXPENSES</b>'],
	["Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad",
	 "Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad",
	 "Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad",
	 "Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad",
	"Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iusto timeam, an prima laboramus vim. Id usu aeterno adversarium, summo mollis timeam vel ad"]]

var data = [{
  type: 'table',
  columnorder: [1,2],
  columnwidth: [80,400],
  header: {
    values: [["<b>EXPENSES</b><br>as of July 2017"], ["<b>DESCRIPTION</b>"]],
	 align: ["left", "center"],
	 height: 40,
    line: {width: 1, color: '#506784'},
    fill: {color: '#119DFF'},
    font: {family: "Arial", size: 12, color: "white"}
  },
  cells: {
    values: values,
    align: ["left", "center"],
	 height: 30,
    line: {color: "#506784", width: 1},
	 fill: {color: ['#25FEFD', 'white']},
    font: {family: "Arial", size: 11, color: ["#506784"]}
  }
}]

Plotly.newPlot('myDiv', data);
Click to copy
SalariesOfficeMerchandiseLegalTOTALEXPENSESEXPENSESas of July 2017Lorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iustotimeam, an prima laboramus vim. Id usu aeterno adversarium, summomollis timeam vel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iustotimeam, an prima laboramus vim. Id usu aeterno adversarium, summomollis timeam vel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iustotimeam, an prima laboramus vim. Id usu aeterno adversarium, summomollis timeam vel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iustotimeam, an prima laboramus vim. Id usu aeterno adversarium, summomollis timeam vel adLorem ipsum dolor sit amet, tollit discere inermis pri ut. Eos ea iustotimeam, an prima laboramus vim. Id usu aeterno adversarium, summomollis timeam vel adDESCRIPTION
var values = [
      ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'],
      [1200000, 20000, 80000, 2000, 12120000],
      [1300000, 20000, 70000, 2000, 130902000],
      [1300000, 20000, 120000, 2000, 131222000],
      [1400000, 20000, 90000, 2000, 14102000]]

var headerColor = "grey";
var rowEvenColor = "lightgrey";
var rowOddColor = "white";

var data = [{
  type: 'table',
  header: {
    values: [["<b>EXPENSES</b>"], ["<b>Q1</b>"],
				 ["<b>Q2</b>"], ["<b>Q3</b>"], ["<b>Q4</b>"]],
    align: "center",
    line: {width: 1, color: 'black'},
    fill: {color: headerColor},
    font: {family: "Arial", size: 12, color: "white"}
  },
  cells: {
    values: values,
    align: "center",
    line: {color: "black", width: 1},
    fill: {color: [[rowOddColor,rowEvenColor,rowOddColor,
						  rowEvenColor,rowOddColor]]},
    font: {family: "Arial", size: 11, color: ["black"]}
  }
}]

Plotly.newPlot('myDiv', data);
Click to copy
SalariesOfficeMerchandiseLegalTOTALEXPENSES12000002000080000200012120000Q1130000020000700002000130902000Q21300000200001200002000131222000Q314000002000090000200014102000Q4

Please see Table Subplots documentation.