Published
Edited
May 19, 2020
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
html`<svg>
<rect x="10" y="10" width="50" height="100" fill="#c01"/>
<rect x="70" y="10" width="50" height="50" fill="#c01"/>
<rect x="130" y="10" width="50" height="20" fill="#c01"/>
</svg>
`
Insert cell
html`
<svg width=200 height=200>
<circle cx="90" cy="25" r="20" fill="orange" />
<circle cx="90" cy="45" r="40" fill="blue" opacity="0.5"/>
<circle cx="90" cy="85" r="80" fill="green" opacity="0.3"/>
</svg>
`
Insert cell
html`
<svg width=200 height=50>
<line x1="0" y1="45" x2="100%" y2="10" stroke="salmon" stroke-width="5"/>
</svg>
`
Insert cell
html`<svg width=200 height=200>
<g transform="translate(100, 100)">
<line x1="-6" x2="6" y1="0" y2="0" style="stroke: black"/>
<line y1="-6" y2="6" x1="0" x2="0" style="stroke: black"/>
<circle cx="-50" cy="-50" r="20" fill="#61d4b3" />
<circle cx="40" cy="50" r="20" fill="#fdd365" />
<g transform="scale(1.5)">
<circle cx="25" cy="-30" r="20" fill="#fb8d62" />
</g>
</g>
</svg>
`
Insert cell

d3 = require("d3@5", "d3-geo@^1.12")
Insert cell
Insert cell
Insert cell
{
const width = 500 //width of svg
const height = 250 //height of svg

const svg = d3.select(DOM.svg(width, height))
const ArrayofData = Array(25).fill().map(() => Math.round(Math.random() * 250))
svg.selectAll('rect')
.data(ArrayofData) //connect ArrayofData with DOM <rect/> elements
.enter() // returns the portion of the data which is new ("entering") and not yet bound to DOM elements
.append('rect') // for each data value, append a <rect/> to selected svg
.attr('x', (d, itemsIndexInArray) => itemsIndexInArray * 20)// For each, set x attr
.attr('y', d => height - d) // for each, set y attr
.attr('width', 15) // for each, set width attribute
.attr('height', arrayItemTypicallyNamed_d => arrayItemTypicallyNamed_d)
.attr('fill', 'red')
// Once we append the visualization elments to it, we return the DOM element for Observable to display above.
return svg.node()
}
Insert cell
Insert cell
{
// function to zip arrays
const zip = (arr1, arr2) => arr1.map((k, i) => [k, arr2[i]])
// generate two arrays of random data
const ArrayofX = Array(25).fill().map(() => Math.round(Math.random() * 500))
const ArrayofY = Array(25).fill().map(() => Math.round(Math.random() * 250))
const dataset = zip(ArrayofX,ArrayofY)
const width = 500
const height = 250
const svg = d3.select(DOM.svg(width, height))
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return d[0];
})
.attr("cy", function(d) {
return d[1];
})
.attr("r", 3)
.attr('fill', 'purple')
return svg.node()
}
Insert cell
Insert cell
Insert cell
{
// define an svg with given width and height
const width = 500
const height = 30
const svg = d3.select(DOM.svg(width, height))
// we want a margin to give us whitespace around the axis
const margin = { left: 30, top: 10, right: 10, bottom: 10 }
// Use d3 scaleLinear to split the width (- margins) = 460 and scale the 0-50 axis length we'd like to within
// these 460 width limits
const xScale = d3.scaleLinear()
.domain([0, 500])
.range([margin.left, width - margin.right])
// append it to the svg
svg.append('g').call(d3.axisBottom(xScale))

return svg.node()
}
Insert cell
Insert cell
{
const height = 250
const svg = d3.select(DOM.svg(60, height)) // Defining svg
const margin = { left: 30, top: 10, right: 10, bottom: 10 }
const yScale = d3.scaleLinear()
.domain([0, 250])
.range([height - margin.bottom, margin.top])
svg.append('g').call(d3.axisLeft(yScale))// Append it to svg
.attr('transform', `translate(${margin.left},0)`)
return svg.node()
}
Insert cell
{
// function to zip arrays
const zip = (arr1, arr2) => arr1.map((k, i) => [k, arr2[i]])
// generate two arrays of random data
const ArrayofX = Array(25).fill().map(() => Math.round(Math.random() * 500))
const ArrayofY = Array(25).fill().map(() => Math.round(Math.random() * 300))
const dataset = zip(ArrayofX,ArrayofY)
const width = 500
const height = 300
const padding = 20
const svg = d3.select(DOM.svg(width, height))
const margin = { left: 40, top: 20, right: 10, bottom: 40 }
const xScale = d3.scaleLinear()
.domain(d3.extent(ArrayofX))
.range([margin.left, width - margin.right])
svg.append('g')
.call(d3.axisBottom(xScale))
.attr('transform', `translate(0,${height - margin.bottom})`)
const yScale = d3.scaleLinear()
.range([height - margin.bottom, margin.top])
.domain(d3.extent(ArrayofY))
svg.append('g')
.call(d3.axisLeft(yScale))
.attr('transform', `translate(${margin.left},0)`)
svg.selectAll('circle')
.data(dataset)
.enter()
.append('circle')
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr('r', 3)
.attr('fill', 'purple')
return svg.node()

}
Insert cell
Insert cell
{
// function to zip arrays
const zip = (arr1, arr2) => arr1.map((k, i) => [k, arr2[i]])
// generate two arrays of random data
const ArrayofX = Array(25).fill().map(() => Math.round(Math.random() * 500))
const ArrayofY = Array(25).fill().map(() => Math.round(Math.random() * 300))
const dataset = zip(ArrayofX,ArrayofY)
const width = 500
const height = 300
const padding = 20
const svg = d3.select(DOM.svg(width, height))
const margin = { left: 40, top: 20, right: 10, bottom: 40 }
const xScale = d3.scaleLinear()
.domain(d3.extent(ArrayofX))
.range([margin.left, width - margin.right])
svg.append('g')
.call(d3.axisBottom(xScale))
.attr('transform', `translate(0,${height - margin.bottom})`)
const yScale = d3.scaleLinear()
.range([height - margin.bottom, margin.top])
.domain(d3.extent(ArrayofY))
svg.append('g')
.call(d3.axisLeft(yScale))
.attr('transform', `translate(${margin.left},0)`)
svg.selectAll('circle')
.data(dataset)
.enter()
.append('circle')
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", function(d) {
return (Math.cbrt(d[1]));
})
.attr('fill', 'purple')
svg.append("text")
.attr("text-anchor", "middle")
.attr("transform", "translate("+ (width/2) +","+(height-padding/2)+")")
.attr('font-size', 12)
.attr('fill', 'steelblue')
.text("Random X Value")
svg.append("text")
.attr("text-anchor", "middle")
.attr("transform", "translate("+ (padding/2) +","+(height/2)+")rotate(-90)")
.attr('font-size', 12)
.attr('fill', 'steelblue')
.text("Random Y Value")
svg.append("text")
.attr("text-anchor", "middle")
.attr("transform", "translate("+ (width/2) +","+(padding/2)+")")
.attr('font-size', 14)
.text("Random Scatter Plot")
return svg.node()

}
Insert cell
Insert cell
Insert cell
Insert cell
geo_json = FileAttachment("ccgs_april_2016@1.json").json()
Insert cell
Insert cell
ccg_data = d3.csvParse(await FileAttachment("baby_birth_weight.csv").text(), d3.autoType)
Insert cell
{
const w = 600
const h = 600
const svg = d3.select(DOM.svg(w, h))
// set up the geometry
const projection = d3.geoConicConformal()
.center([1.9, 52.4])
.clipAngle(180)
.scale(4500)
.translate([w /1.25, h /1.9])
.precision(0.1);
const path = d3.geoPath().projection(projection);
var colour = d3.scaleThreshold()
.domain([0,1, 2, 3, 4])
.range(['#f2f0f7','#cbc9e2','#9e9ac8','#756bb1','#54278f']);
var matchset = [];
var reportset = [];
var opacityset = [];
var numDataPoints = ccg_data.length;
var mean = 0.029;
var opacity_value = 0;
for (var i = 0; i < numDataPoints; i++) {
if (ccg_data[i].Denominator==0) {
var indicator = 0;
}
else {
var indicator = ccg_data[i].Numerator/ccg_data[i].Denominator;
}
console.log(indicator)
if (indicator > 0.05) {
opacity_value = 4;
}
else if (indicator > 0.03) {
opacity_value = 3;
}
else if (indicator > 0.02) {
opacity_value = 2;
}
else if (indicator > 0.01) {
opacity_value = 1;
}
else {
opacity_value = 0;
}
var match = ccg_data[i].ONS_code;
matchset.push(ccg_data[i].ONS_code);
reportset.push(ccg_data[i].ONS_code);
opacityset.push(opacity_value);
}
i = 0;
for (var i = 0; i < (geo_json.features).length; i++) {
var ons = geo_json.features[i].properties.ccg16cd;
var index = matchset.indexOf(ons);
svg.append("path")
.datum(geo_json.features[i].geometry)
.attr("class", "border")
.attr("d", path)
.style("fill", function(d) { return colour(opacityset[index]);})
.attr("class", "border count-" + opacityset[index])
}
return svg.node();
}
Insert cell
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