testWithLetters = {
const width = 500;
const height = 300;
const margin = {
top: 50,
bottom: 50,
left: 50,
right: 50
};
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("font-size", 10)
.attr("font-family", "sans-serif")
.attr("text-anchor", "middle")
.style("background", "pink");
const t = svg.transition().duration(750);
const dataset = [
[{ name: 'a' }, { name: 'b' }, { name: 'c' }],
[{ name: 'd' }, { name: 'e' }, { name: 'f' }],
[{ name: 'g' }, { name: 'h' }]
]
console.log('initial dataset', dataset);
svg.append('text')
.text('click to change the dataset')
.attr('text-anchor', "start")
.attr('x', 50)
.attr('y', 50)
.on("click", function changeDataset() {
dataset.push([
{ name: 'i'}, { name: "j"}
])
console.log('new dataset', dataset);
});
const xScale = d3
.scaleLinear()
.domain([0, 100])
.range([margin.left, width - margin.right]);
const yScale = d3
.scaleLinear()
.domain([0, 100])
.range([height - margin.bottom, margin.top]);
let gColumns = svg.selectAll('g')
.data(dataset, (d,i) => i)
.join(
enter => enter.append('g')
.attr('id', (d, i) => `column-step-${i}`)
.attr('data-i', (d,i) => i)
.attr('transform', (d, i) => `translate(${i * 50 + 100},0)`),
update => update
.attr('class', 'update')
.attr('transform', (d, i) => `translate(${i * 50 + 100},0)`),
exit => exit.remove()
);
let textLetters = gColumns.selectAll('text')
.data(d => d, d => d)
.join(
enter => enter.append('text')
.text(d => d.name)
.attr('data-name', d => d.name)
.attr('class', 'enter')
.attr('x', 0)
.attr('y', yScale(300))
.style('font-size', '20px')
.call(enter => enter.transition(t)
.delay(function(d, i) {
let parentNodeInfo = d3.select(this.parentNode).attr("data-i");
return parentNodeInfo * 200 + i * 50;
})
.attr('y', (d, i) => yScale(i * 20))
),
update => update
.call(update => update.transition(t)
.delay(function(d, i) {
let parentNodeInfo = d3.select(this.parentNode).attr("data-i");
return parentNodeInfo * 200 + i * 50;
})
.attr('y', (d, i) => yScale(i * 20))
),
);
return svg.node()
}