Published
Edited
May 11, 2022
Importers
1 star
Insert cell
Insert cell
Insert cell
chart = {
const height = 600
const margin = ({top: 25, right: 20, bottom: 35, left: 40})
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

// X scale
const x = d3.scaleLinear()
.domain(d3.extent(trials, d => d.xValue)).nice(12)
.range([margin.left, width - margin.right])

// Y scale
const y = d3.scaleLinear()
.domain(d3.extent(trials, d => d.yValue)).nice(12)
.range([height - margin.bottom, margin.top]);

// X axis
const xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(12))
.call(g => g.select(".domain").remove())

// Y axis
const yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y)
.ticks(12))
.call(g => g.select(".domain").remove())

// Grid
const grid = g => g
.attr("stroke", "#eee")
.attr("stroke-dasharray",3)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));

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

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

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

// Trial points
const points = svg.append("g")
.attr("stroke", "none")
.attr("fill", "#12A6AD")
points.selectAll("circle")
.data(trials)
.join("circle")
.attr("cx", d => x(d.xValue))
.attr("cy", d => y(d.yValue))
.attr("r", 0)
.attr('cursor', 'pointer')

points.selectAll("circle")
.transition()
.duration(d => x(d.xValue) * 1.5)
.attr("r", 4)

// Correlation line math
const linearRegression = ss.linearRegression(trials.map(d => [d.xValue, d.yValue]))
console.log(linearRegression)
// We can pass that object into a helper function to get a function that given x, returns y!
const linearRegressionLine = (x) => linearRegression.m * x + linearRegression.b;
const xCoordinates = d3.extent(trials, d => d.xValue)
const regressionPoints = xCoordinates.map(d => ({xValue: d, yValue: linearRegressionLine(d)}));

// Correlation line path
svg.append('path')
.datum(regressionPoints)
.attr('stroke', 'maroon')
.attr('stroke-width', 1.5)
.attr('stroke-dasharray', '3,5')
.attr('d', d3.line()
.x(d => x(d.xValue))
.y(d => y(d.yValue)));

points.selectAll("circle").on("mouseover", function(event, d){
console.log({event, d})
svg.append("text")
.attr("class", "correlation-tooltip")
.attr("x", x(d.xValue) + 20)
.attr("y", y(d.yValue) - 20)
// .style("visibility", "visible")
.text(`${d.xValue}, ${d.yValue}`);

d3.select(this)
.transition()
.duration(50)
.attr("r", 12);


}).on("mouseout", function(){
d3.select(this)
.transition()
.duration(50)
.attr("r", 4);

d3.select('.correlation-tooltip').remove()
});

return svg.node();
}
Insert cell
Insert cell
ss = require('simple-statistics')
Insert cell
d3 = require("d3@7")
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