Published
Edited
Feb 29, 2020
Importers
Insert cell
Insert cell
working_memory = {
const svg = d3.create('svg')
.attr('viewBox', [0, 0, width, height]);
const wrapper = svg.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
wrapper.append('g')
.call(xAxis);
const yAxis = wrapper.append('g')
.attr('class', 'y-axis')
.call(yAxisGenerator);
const grids = wrapper.append('g')
.call(grid);
var rects = wrapper.append('g')
.attr('class', 'bars')
.selectAll('rect')
.data(mydata[0].data)
.join('rect')
.attr('x', d => x(d.group))
.attr('y', d => y(d.mean))
.attr('fill', (d, i) => colorScale[i])
.attr('fill-opacity', 0.99)
.attr('stroke', '#3D4852')
.attr('height', d => y(y.domain()[0]) - y(d.mean))
.attr('width', x.bandwidth())
.on('click', changeYScale)
.style("cursor", "pointer");
// Draw confidence intervals
wrapper.append('g')
.attr('class', 'confInt')
.selectAll('line')
.data(mydata[0].data)
.join('line')
.attr('y1', d => y(d.cl_lower))
.attr('y2', d => y(d.cl_upper))
.attr('x1', d => x(d.group)+ x.bandwidth() / 2)
.attr('x2', d => x(d.group) + x.bandwidth() / 2)
.attr('stroke', 'black');
// Draw top whiskers
wrapper.append('g')
.attr('class', 'topWhiskers')
.selectAll('line')
.data(mydata[0].data)
.join('line')
.attr('y1', d => y(d.cl_upper))
.attr('y2', d => y(d.cl_upper))
.attr('x1', d => x(d.group) + (x.bandwidth()/2) - (x.bandwidth() / 20))
.attr('x2', d => x(d.group) + (x.bandwidth()/2) + (x.bandwidth() / 20))
.attr('stroke', 'black');
// Draw bottom whiskers
wrapper.append('g')
.attr('class', 'bottomWhiskers')
.selectAll('line')
.data(mydata[0].data)
.join('line')
.attr('y1', d => y(d.cl_lower))
.attr('y2', d => y(d.cl_lower))
.attr('x1', d => x(d.group) + (x.bandwidth()/2) - (x.bandwidth() / 20))
.attr('x2', d => x(d.group) + (x.bandwidth()/2) + (x.bandwidth() / 20))
.attr('stroke', 'black');
function changeYScale() {
let yDomain = y.domain();
let tickValues = d3.range(28, 36, 1);
if (y.domain()[0] == 0) {
y.domain([28, 35]);
tickValues = d3.range(28, 36, 1);
} else {
y.domain([0, 35]);
tickValues = d3.range(0, 36, 5);
}
// Update yAxis
yAxis.transition(t)
.call(d3.axisLeft(y)
.ticks(tickValues)
.tickSize(3)
.tickValues(tickValues)
.tickFormat(d3.format(',.0f')))
.call(g => g.selectAll('.tick text')
.attr('font-size', '0.8rem'));
// Update Bars
svg.select('.bars')
.selectAll('rect')
.transition(t)
.attr('y', d => y(d.mean))
.attr('height', d => y(y.domain()[0]) - y(d.mean));
// Update confidence interval
svg.select('.confInt')
.selectAll('line')
.transition(t)
.attr('y1', d => y(d.cl_lower))
.attr('y2', d => y(d.cl_upper))
.attr('stroke', 'black');
// Update whiskers top
svg.select('.topWhiskers')
.selectAll('line')
.transition(t)
.attr('y1', d => y(d.cl_upper))
.attr('y2', d => y(d.cl_upper));
svg.select('.bottomWhiskers')
.selectAll('line')
.transition(t)
.attr('y1', d => y(d.cl_lower))
.attr('y2', d => y(d.cl_lower));

}
return svg.node();
}
Insert cell
html`
<style>
.bars rect:hover {
fill-opacity: 0.92;
transition: fill-opacity 0.2s;
}
</style>
`
Insert cell
t = d3.transition()
.duration(700)
.ease(d3.easeLinear);
Insert cell
Insert cell
colorScale = ['#BCDEFA', '#6CB2EB', '#3490DC']
Insert cell
grid = g => g
.attr('stroke', '#B8C2CC')
.attr('stroke-opacity', 0.5)
.call(g => g.append('g')
.selectAll('line')
.data(d3.range(28, 36, 1))
.join('line')
.attr("x1", x(0))
.attr("x2", (width - margin.left - margin.right))
.attr("y1", d => y(d))
.attr("y2", d => y(d)))
.call(g => g.select('line:last-child').remove());
Insert cell
xAxis = g => g
.attr('transform', `translate(0, ${height - margin.top - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll('.tick line').remove())
.call(g => g.selectAll('.tick text')
.attr('font-size', '0.8rem'));
Insert cell
yAxisGenerator = g => g
.call(d3.axisLeft(y)
.ticks(d3.range(28, 36, 1))
.tickSize(3)
.tickValues(d3.range(28, 36, 1))
.tickFormat(d3.format(',.0f')))
.call(g => g.select('.tick:last-of-type text').clone()
.attr('x', 10)
.attr('y', 0)
.attr('font-size', '0.5rem')
.attr('text-anchor', 'start')
.text("↑ Arbeitsgedächtnis (OSpan Score)"))
.call(g => g.selectAll('.tick text')
.attr('font-size', '0.8rem'));
Insert cell
x = d3.scaleBand()
.domain(mydata[0].data.map((d) => d.group))
.range([0, width - margin.left - margin.right])
.padding(0.2);
Insert cell
y = d3.scaleLinear()
.domain([28, 35])
.range([height - margin.bottom - margin.top, 0])
Insert cell
height = 400;
Insert cell
margin = ({top: 30, left: 30, bottom: 70, right: 30})
Insert cell
Insert cell
mydata = [{av: "Working Memory Capacity",
y: "OSpan Score",
x: "Phone Location",
data: [{group: "Schreibtisch", mean: 30.5, cl_lower: 29.7, cl_upper: 31.4},
{group: "Tasche", mean: 31.2, cl_lower: 30.2, cl_upper: 32.1},
{group: "Anderer Raum", mean: 33.9, cl_lower: 33, cl_upper: 34.9}]},
{av: "Fluid Intelligence",
y: "Correctly Solved Matries (out of 10)",
x: "Phone Location",
data: [{group: "Schreibtisch", mean: 7.8, cl_lower: 7.7, cl_upper: 7.96},
{group: "Tasche", mean: 8.24, cl_lower: 8.1, cl_upper: 8.37},
{group: "Anderer Raum", mean: 8.36, cl_lower: 8.2, cl_upper: 8.5}]}]
Insert cell
d3 = require("d3@5")
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