Public
Edited
Feb 14, 2023
Insert cell
Insert cell
Insert cell
d3 = require("d3@7");
Insert cell
d3.version
Insert cell
d3v6 = require("d3@6");
Insert cell
d3v6.version
Insert cell
d3v7 = require("d3@7");
Insert cell
d3v7.version
Insert cell
Insert cell
Insert cell
import { alphabet } from "@observablehq/plot-test-data"
Insert cell
desiredData = alphabet.data;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
FileAttachment("Position@2.svg").image({ width: 620})
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
func1 = d => d*1000
Insert cell
func2 = function (d) {
return d*1000;
}
Insert cell
func1(0.12);
Insert cell
func2(0.12);
Insert cell
Insert cell
Insert cell
height = 250;
Insert cell
width = 600;
Insert cell
firstchart = {

const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox",[0, 0, width, height]);
svg.selectAll('rect')
.data(desiredData)
.join('rect')
.attr('x', (d, i) => i * 25)
.attr('y', d => height - d.frequency*1500)
.attr('width', 20)
.attr('height', d => d.frequency*1500)
.style('fill', 'purple');

// Specific to Observable
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
yScale = d3.scaleLinear()
.domain([0,d3.max(desiredData, d => d.frequency)])
.range([height - margin.bottom, margin.top]);
Insert cell
desiredData

Insert cell
d3.max(desiredData, d => d.frequency);
Insert cell
height - margin.bottom
Insert cell
yScale(0);
Insert cell
yScale(0.127);
Insert cell
yScale(0.1);
Insert cell
Insert cell
yLogScale = d3.scaleLog()
.domain(d3.extent(desiredData, d => d.frequency)) //extent returns an Array[2] as [min, max]
.range([height - margin.bottom, margin.top]);
Insert cell
d3.extent(desiredData, d => d.frequency);
Insert cell
yLogScale(d3.min(desiredData, d => d.frequency));
Insert cell
yLogScale(0.127);
Insert cell
yLogScale(0.1);
Insert cell
yLogScale(0.001);
Insert cell
Insert cell
color = d3.scaleLinear()
.domain([10, 100])
.range(["yellow", "white"]);
Insert cell
Insert cell
color(20);
Insert cell
color(50);
Insert cell
Insert cell
xScale = d3.scaleBand()
.domain(desiredData.map(d=>d.letter))
.range([margin.left, width - margin.right])
.padding(0.1);
Insert cell
desiredData.map(d=>d.letter);
Insert cell
xScale("A");
Insert cell
xScale("F");
Insert cell
xScale.bandwidth();
Insert cell
Insert cell
chartwithscales = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox",[0, 0, width, height]);
svg.selectAll('rect')
.data(desiredData)
.join('rect')
.attr('x', d => xScale(d.letter))
.attr('y', d => yScale(d.frequency))
.attr('width', xScale.bandwidth())
.attr('height', d => yScale(0) - yScale(d.frequency))
.style('fill', 'purple');
// Specific to Observable
return svg.node();
}
Insert cell
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale)
.ticks(width / 80)
.tickSizeOuter(0)
)
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yScale))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 10)
.attr("y", -20)
.attr("text-anchor", "end")
.text("Frequency")
)
Insert cell
barchartwithaxis = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox",[0, 0, width, height]);

svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);

svg.selectAll('rect')
.data(desiredData)
.join('rect')
.attr('x', d => xScale(d.letter))
.attr('y', d => yScale(d.frequency))
.attr('width', xScale.bandwidth())
.attr('height', d => yScale(0) - yScale(d.frequency))
.style('fill', 'purple');
// Specific to Observable
return svg.node();
}
Insert cell
Insert cell
Insert cell
strangePlot = {
const width2 = 600;
const height2 = 250;
const cxScale = d3.scalePoint()
.domain(alphasortedData.map(d=>d.letter))
.range([margin.left, width2 - margin.right]);

const rScale = d3.scaleSqrt()
.domain([0,d3.max(alphasortedData, d => d.frequency)])
.range([0, cxScale.step()/2]);
const xAxis = g => g
.attr("transform", `translate(0,${height2/2 + cxScale.step()/2 })`)
.call(d3.axisBottom(cxScale)
.ticks(width2 / 80)
.tickSizeOuter(0)
)
const svg = d3.create("svg")
.attr("width", width2)
.attr("height", height2)
.attr("viewBox",[0, 0, width2, height2]);
svg.append("g")
.call(xAxis);

svg.selectAll('circle')
.data(alphasortedData)
.join('circle')
.attr('cx', d => cxScale(d.letter))
.attr('cy', height2/2)
.attr('r', d => rScale(d.frequency))
.style('fill', 'purple');
// Specific to Observable
return svg.node();
}
Insert cell
alphasortedData = desiredData.sort((a,b)=>a.letter.localeCompare(b.letter));
Insert cell
Insert cell
import {chart as barchart} with {desiredData as alphabet, margin} from "@d3/bar-chart"
Insert cell
barchart
Insert cell
newData = desiredData.slice(4,19);
Insert cell
import {chart as newbarchart} with {newData as alphabet} from "@d3/bar-chart"
Insert cell
newbarchart
Insert cell
Insert cell
Insert cell
<svg id="main-chart"></svg>
Insert cell
Insert cell
{
//First remove the current content of the main-chart (from previous cell runs).
//Probably, out from Observable this step isn't needed
d3.select('#main-chart').html(null);

//create the plot area
const svg = d3.select('#main-chart')
.attr('width', width) // add attribute width to SVG
.attr('height', height) // add attribute height to SVG
.attr("viewBox",[0, 0, width, height]);

//Add X axis
svg.append("g")
.call(xAxis);

//Add Y axis
svg.append("g")
.call(yAxis);

//Add the bar plot
svg.selectAll('rect')
.data(desiredData)
.join('rect')
.attr('x', d => xScale(d.letter))
.attr('y', d => yScale(d.frequency))
.attr('width', xScale.bandwidth())
.attr('height', d => yScale(0) - yScale(d.frequency))
.style('fill', 'purple');

//Return 1 to indicate everything is ok
return 1;
}
Insert cell
Insert cell
import {Legend} from "@d3/color-legend"
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