Consider the following (with many thanks to Creating a table linked to a csv file):
var dataAsCsv = `Col1,Col2
Type1,"123,456"
Type2,"789,012"
Type3,"34,567"`;
var data = d3.csvParse(dataAsCsv);
var columns = ['Col1', 'Col2'];
var table = d3.select("#currentMonthTable").append("table"),
thead = table.append("thead"),
tbody = table.append("tbody");
// append the header row
thead.append("tr")
.selectAll("th")
.data(columns)
.enter()
.append("th")
.text(function(column) { return column; });
var rows = tbody.selectAll("tr")
// create a row for each object in the data
.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; });
<script src="https://d3js.org/d3.v4.js"></script>
<body>
<div id="currentMonthTable"></div>
</body>
I would like to right-justify all cells with numbers in them and keep the justification of all other cells the same. I.e.: the 123,456
, 789,012
, and 34,567
should all be right-justified, but all other justification should remain the same.
What is the easiest way to do this in D3? Note that I would like to keep this to D3 so as to maintain being able to use the csv
data.
Intuitively, I believe the correct solution probably entails giving all cells with numbers a CSS class and then setting text-align:right;
, but I'm not sure how to specifically target these cells using D3.