Public
Edited
Jun 8, 2023
8 forks
1 star
Insert cell
Insert cell
Insert cell
html`
<svg width="200" height="200">
<circle cx="100"
cy="100"
r="80"
style="fill:none; stroke:pink;stroke-width:2"
/>
</svg>
`
Insert cell
Insert cell
html`
<svg width="300" height="100">
<rect x="0" y="0" width="300" height="100" style="fill:none; stroke:black"/>
<g transform="translate(10,5)">
<line x1="0" y1="90" x2="0" y2="0" style="stroke:red; stroke-width:3"/>
<line x1="125" y1="90" x2="125" y2="50" style="stroke:red; stroke-width:1"/>
<line x1="160" y1="90" x2="160" y2="15" style="stroke:red; stroke-width:5"/>
</g>
</svg>
`
Insert cell
Insert cell
d3 = require('d3@5')
Insert cell
Insert cell
container = DOM.element('div');
Insert cell
Insert cell
Insert cell
myData =[
{year:1970, total:4, importance:4},
{year:1974, total:2, importance:1},
{year:1975, total:7, importance:4},
{year:1977, total:9, importance:8}
]
Insert cell
Insert cell
xScale = d3.scaleLinear()
.domain([
Math.min(...myData.map((d) => d.year)),
Math.max(...myData.map((d) => d.year))
])
.range([5, 295])
Insert cell
xTest = xScale(1976)
Insert cell
Insert cell
yScale = d3.scaleLinear()
.domain([
0,
Math.max(...myData.map((d) => d.total))
])
.range([95, 5])
Insert cell
yTest = yScale(1)
Insert cell
Insert cell
{
// remove the previously drawn lines (as this will be executed more than once)
svg.selectAll('line').remove()
svg.selectAll('circle').remove()
svg.selectAll('circle')
.data(myData)
.enter()
.append('circle') // insert a new line for each myData element
.attr('cx', (d) => xScale(d.year))
.attr('cy', (d) => yScale(d.total))
.attr('r', (d) => d.importance/2)
.attr('fill', 'red')

svg.selectAll('line')
.data(myData)
.enter()
.append('line') // insert a new line for each myData element
.attr('x1', (d) => xScale(d.year))
.attr('x2', (d) => xScale(d.year)) // x1 & x2 have the same values
.attr('y1', (d) => yScale(0)) // line starts on the 0 axis
.attr('y2', (d) => yScale(d.total))
.attr('stroke', 'blue')
.attr('stroke-width', (d) => 1)
}
Insert cell
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