Published unlisted
Edited
Jul 6, 2022
Insert cell
# d3 create separate element based on separate data
Insert cell
Insert cell
{
const width = 1280;
const height = 720;

const svg = d3.select('svg')
.attr('xmlns', 'http://www.w3.org/2000/svg')
.attr('viewBox', `0 0 ${width} ${height}`)

const rect = svg.append('rect')
.attr('x', '0')
.attr('y', '0')
.attr('width', `${width}`)
.attr('height', `${height}`)
.attr('fill', `#EFEFEF`)

//src
const dataHorizontal = [
{ x: 0, y: 0 },
{ x: 1280, y: 0 }
];

const pathVal = d3.line()
.x(d => d.x)
.y(d => d.y)
(dataHorizontal);

const index1 = [1, 2, 3];

const index2 = [4, 5, 6, 7, 8, 9, 10, 11]

const multiplier = 60;

//based on index1

svg.selectAll('path')
.data(index1)
.join('path')
.attr('class', (d) => `ln${d}`)
.attr('d', pathVal)
.attr('stroke', () => { return `hsla(${Math.random() * 360} , ${((Math.random() + Math.random()/2)*100).toFixed(2)}%, 50%, 1)`; })
.style('transform', (d, i) => { return `translateY(${d*multiplier}px)` })
.attr('stroke-width', '2')


//based on index2

svg.selectAll('path')
.data(index2)
.join('path')
.attr('class', (d) => `ln${d}`)
.attr('d', pathVal)
.attr('stroke', () => { return `hsla(${Math.random() * 360} , ${((Math.random() + Math.random()/2)*100).toFixed(2)}%, 50%, 1)`; })
.style('transform', (d, i) => { return `translateY(${d*multiplier}px)` })
.attr('stroke-width', '2')
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more