Published
Edited
Dec 3, 2020
Insert cell
Insert cell
Insert cell
driverId = 30;
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
plotCareerResults = (parent, data)=>{
const blockHeight = 6;
const blockWidth = 3;
const stacked = data.results.reduce((acc, current)=>{
if(acc.accumulator[current.position]==undefined){
acc.accumulator[current.position] = 0;
}else{
acc.accumulator[current.position] += 1;
}
current.stackHeight = acc.accumulator[current.position];
acc.stacked.push(current)
return acc
}, {
stacked:[],
accumulator:{}
}).stacked;
const group = parent.append('g')
.attr('transform',`translate(${data.margin.left},${data.margin.top+7})`)
group.append('line')
.attr('x1', data.margin.left)
.attr('y1', data.mean*blockHeight)
.attr('x2', data.width - data.margin.right)
.attr('y2', data.mean*blockHeight)
.attr('stroke','rgb(0,0,0)')
group.append('line')
.attr('x1', data.margin.left)
.attr('y1', data.circuitStats.mean*blockHeight)
.attr('x2', data.width - data.margin.right)
.attr('y2', data.circuitStats.mean*blockHeight)
.attr('stroke', 'rgb(255,0,0)')
group.selectAll('rect')
.data(stacked)
.enter()
.append('rect')
.attr('x', d => d.stackHeight*(blockWidth+1))
.attr('y', d => d.position*(blockHeight +1))
.attr('width', blockWidth)
.attr('height', blockHeight)
.attr('fill', d => {
if(d.circuitId == data.circuit){ return 'rgb(255, 0, 0)'; }
return 'rgb(200, 200, 200)';
});
}
Insert cell
Insert cell
chart = {
const width = 1000;
const height = 2450;
const margin = {bottom:20, top:20, left:0, right:0};
const subPlotHeight = 200;
const subPlotWidth = 300;
const subPlotRow = Math.floor(width/subPlotWidth);
const svg = d3.select(DOM.svg(width, height));
svg.selectAll('g.segment')
.data(Object.keys(segmentedStats))
.enter()
.append('g')
.attr('class','segment')
.attr('transform', (d,i)=>`translate(${i%subPlotRow * subPlotWidth}, ${Math.floor(i/subPlotRow) * subPlotHeight})`)
.each(function(d, i){
const parent = d3.select(this);
const circuitName = circuitLookup[d].name;
parent.append('text')
.attr('dy', margin.top)
.text(circuitName);

parent.call(plotCareerResults, {
results: driverCareer.results,
mean: overallSummary.mean,
median: overallSummary.median,
deviation: overallSummary.deviation,
width: subPlotWidth,
height: subPlotHeight - (margin.bottom + margin.top),
circuit: d,
circuitStats: segmentedStats[d],
margin
})
//add the general career plot
//add the distribution for this course
});
return svg.node(); //to add the chart to the page the block should return its node
}
Insert cell
Insert cell
import {
races,
drivers,
results,
circuits,
qualifying,
circuitLookup,
driverLookup,
raceLookup }
from '@tomgp/f1-data'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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