Tables in JavaScript

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


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

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);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var values = [\n ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'],\n [1200000, 20000, 80000, 2000, 12120000],\n [1300000, 20000, 70000, 2000, 130902000],\n [1300000, 20000, 120000, 2000, 131222000],\n [1400000, 20000, 90000, 2000, 14102000]]\n\nvar data = [{\n type: 'table',\n header: {\n values: [[\"<b>EXPENSES</b>\"], [\"<b>Q1</b>\"],\n\t\t\t\t [\"<b>Q2</b>\"], [\"<b>Q3</b>\"], [\"<b>Q4</b>\"]],\n align: \"center\",\n line: {width: 1, color: 'black'},\n fill: {color: \"grey\"},\n font: {family: \"Arial\", size: 12, color: \"white\"}\n },\n cells: {\n values: values,\n align: \"center\",\n line: {color: \"black\", width: 1},\n font: {family: \"Arial\", size: 11, color: [\"black\"]}\n }\n}]\n\nPlotly.newPlot('myDiv', data);\n"}">
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);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var values = [\n ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'],\n [1200000, 20000, 80000, 2000, 12120000],\n [1300000, 20000, 70000, 2000, 130902000],\n [1300000, 20000, 120000, 2000, 131222000],\n [1400000, 20000, 90000, 2000, 14102000]]\n\nvar data = [{\n type: 'table',\n header: {\n values: [[\"<b>EXPENSES</b>\"], [\"<b>Q1</b>\"],\n\t\t\t\t [\"<b>Q2</b>\"], [\"<b>Q3</b>\"], [\"<b>Q4</b>\"]],\n align: [\"left\", \"center\"],\n line: {width: 1, color: '#506784'},\n fill: {color: '#119DFF'},\n font: {family: \"Arial\", size: 12, color: \"white\"}\n },\n cells: {\n values: values,\n align: [\"left\", \"center\"],\n line: {color: \"#506784\", width: 1},\n\t fill: {color: ['#25FEFD', 'white']},\n font: {family: \"Arial\", size: 11, color: [\"#506784\"]}\n }\n}]\n\nPlotly.newPlot('myDiv', data);\n"}">
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);
});
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv(\"https://raw.githubusercontent.com/plotly/datasets/master/Mining-BTC-180.csv\", function(err, rows){\n\n function unpack(rows, key) {\n return rows.map(function(row) { return row[key]; });\n }\n\n var headerNames = d3.keys(rows[0]);\n\n var headerValues = [];\n var cellValues = [];\n for (i = 0; i < headerNames.length; i++) {\n headerValue = [headerNames[i]];\n headerValues[i] = headerValue;\n cellValue = unpack(rows, headerNames[i]);\n cellValues[i] = cellValue;\n }\n\n // clean date\n for (i = 0; i < cellValues[1].length; i++) {\n var dateValue = cellValues[1][i].split(' ')[0]\n cellValues[1][i] = dateValue\n }\n\n\nvar data = [{\n type: 'table',\n columnwidth: [150,600,1000,900,600,500,1000,1000,1000],\n columnorder: [0,1,2,3,4,5,6,7,8,9],\n header: {\n values: headerValues,\n align: \"center\",\n line: {width: 1, color: 'rgb(50, 50, 50)'},\n fill: {color: ['rgb(235, 100, 230)']},\n font: {family: \"Arial\", size: 8, color: \"white\"}\n },\n cells: {\n values: cellValues,\n align: [\"center\", \"center\"],\n line: {color: \"black\", width: 1},\n fill: {color: ['rgba(228, 222, 249, 0.65)','rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)']},\n font: {family: \"Arial\", size: 9, color: [\"black\"]}\n }\n}]\n\nvar layout = {\n title: {\n text: \"Bitcoin mining stats for 180 days\"\n }\n}\n\nPlotly.newPlot('myDiv', data, layout);\n});\n"}">
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);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var values = [\n ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL<br>EXPENSES</b>'],\n\t[\"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\",\n\t \"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\",\n\t \"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\",\n\t \"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\",\n\t\"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\"]]\n\nvar data = [{\n type: 'table',\n columnorder: [1,2],\n columnwidth: [80,400],\n header: {\n values: [[\"<b>EXPENSES</b><br>as of July 2017\"], [\"<b>DESCRIPTION</b>\"]],\n\t align: [\"left\", \"center\"],\n\t height: 40,\n line: {width: 1, color: '#506784'},\n fill: {color: '#119DFF'},\n font: {family: \"Arial\", size: 12, color: \"white\"}\n },\n cells: {\n values: values,\n align: [\"left\", \"center\"],\n\t height: 30,\n line: {color: \"#506784\", width: 1},\n\t fill: {color: ['#25FEFD', 'white']},\n font: {family: \"Arial\", size: 11, color: [\"#506784\"]}\n }\n}]\n\nPlotly.newPlot('myDiv', data);\n"}">
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);
</script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"var values = [\n ['Salaries', 'Office', 'Merchandise', 'Legal', '<b>TOTAL</b>'],\n [1200000, 20000, 80000, 2000, 12120000],\n [1300000, 20000, 70000, 2000, 130902000],\n [1300000, 20000, 120000, 2000, 131222000],\n [1400000, 20000, 90000, 2000, 14102000]]\n\nvar headerColor = \"grey\";\nvar rowEvenColor = \"lightgrey\";\nvar rowOddColor = \"white\";\n\nvar data = [{\n type: 'table',\n header: {\n values: [[\"<b>EXPENSES</b>\"], [\"<b>Q1</b>\"],\n\t\t\t\t [\"<b>Q2</b>\"], [\"<b>Q3</b>\"], [\"<b>Q4</b>\"]],\n align: \"center\",\n line: {width: 1, color: 'black'},\n fill: {color: headerColor},\n font: {family: \"Arial\", size: 12, color: \"white\"}\n },\n cells: {\n values: values,\n align: \"center\",\n line: {color: \"black\", width: 1},\n fill: {color: [[rowOddColor,rowEvenColor,rowOddColor,\n\t\t\t\t\t\t rowEvenColor,rowOddColor]]},\n font: {family: \"Arial\", size: 11, color: [\"black\"]}\n }\n}]\n\nPlotly.newPlot('myDiv', data);\n"}">

Please see Table Subplots documentation.