Published
Edited
Jun 14, 2018
2 forks
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
HeyD3 = {
const width = 200,
height = 200;
const svg = d3.select(DOM.svg(width, height))
.style("width", width)
.style("height", height)
.style("border", "1px solid #000");
svg.append("text")
.style("font-size", "48px")
.attr("fill", "#000")
.attr("x", width/2) // set the y position
.attr("y", height/2) // set the x position
.style("text-anchor","middle") // text-anchor styling (style here means its CSS)
.text("Hey, D3!"); // set the text
return svg.node();
}
Insert cell
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
HeyData = {
const n = 50; // Set length of the array
const data = d3.range(n).map(() => {
return {x: Math.random(), y: Math.random()} // Object of random x and y values
});
return data
}
Insert cell
Insert cell
SVGwithData = {
const width = 500,
height = 500;
const xScale = d3.scaleLinear() // Creates a Linear Scale
.range([0, width]) // Output is 0 to the width (pixels)
.domain([0, 1]); // Input is 0 to 1, which is the range of Math.random(), which we used in making the data
const yScale = d3.scaleLinear()
.range([0, height])
.domain([0, 1]);
const svg = d3.select(DOM.svg(width, height))
.style("width", width)
.style("height", height)
.style("border", "1px solid #000");
svg.selectAll("circle") // Create an "empty selection"
.data(HeyData) // Bind the data
.enter().append("circle") // Create a circle for each data point
.attr("cx", (d) => xScale(d.x)) // Set the X attribute using the xScale and the data
.attr("cy", (d) => yScale(d.y)) // Set the Y attribute using the yScale and the data
.attr("r", 5) // Give it a radius
.style("fill", "#000")
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more