Published
Edited
Aug 5, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart3 = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, 320]);

svg
.append("g")
.attr("fill", 'steelBlue')
.selectAll("rect")
.data(bins)
.join("rect")
.attr("x", d => x(d.x0) + 1)
.attr("width", d => Math.max(0, x(d.x1) - x(d.x0) - 1))
.attr("y", d => y(d.length))
.attr("height", d => y(0) - y(d.length));

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

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

svg
.append('text')
.text('Hours in Range')
.attr('transform', 'translate(40,40)');

return svg.node();
}
Insert cell
Insert cell
Insert cell
bins = d3.bin().thresholds(24)(hours)
Insert cell
x = d3
.scaleLinear()
.domain([0, 24])
.range([margin.left, width - margin.right])
Insert cell
y = d3
.scaleLinear()
.domain([0, d3.max(bins, d => d.length)])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cleanedDays = Array.from(grouped)
.map(d => {
let stdDev = getStandardDeviation(
d[1].map(d => {
return d.value;
})
);
return { day: d[0], values: d[1], stddev: stdDev };
})
.sort((a, b) => {
return a.stddev - b.stddev;
})
.filter(d => {
return d.values.length >= 100;
})
Insert cell
hours = withRanges.map(d => d.inRangeHours)
Insert cell
withRanges = Array.from(grouped)
.map(d => {
let hours = d[1].map(v => {
return v.timestamp.getHours();
});
let hoursWithSet = new Set(hours);
let flatHours = [...hoursWithSet];

let outOfRangeHours = d[1]
.filter(d => {
return d.value >= maxGlucose; //110 is max range value
})
.map(d => {
return d.timestamp.getHours();
});

let dataArrWithSet = new Set(outOfRangeHours);
let resultArr = [...dataArrWithSet];

let inRangeCount = 0;
let cumulative = d3.range(24).map(v => {
if (resultArr.includes(v) === false) {
inRangeCount += 1;
}
return { hour: v, inRangeCount: inRangeCount };
});

return {
day: d[0],
values: d[1],
outOfRangeHours: resultArr,
inRangeHours: flatHours.length - resultArr.length,
cumulative: cumulative,
totalHoursWithData: flatHours.length
};
})
.filter(d => {
return d.totalHoursWithData >= 12;
})
Insert cell
maxGlucose = 110
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
line = d3
.line()
.defined(d => !isNaN(d.inRangeCount))
.x(d => xScale(d.hour))
.y(d => yScale(d.inRangeCount))
.curve(d3.curveMonotoneX)
Insert cell
Insert cell
yScale.domain()
Insert cell
yAxisGenerator = g =>
g
.attr("transform", `translate(${width - margin.right - margin.left / 2},0)`)
.call(
d3
.axisRight(yScale)
.ticks(4)
.tickSize(-width + margin.left + margin.right + 5)
)
.call(g => g.select(".domain").remove())
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(
d3
.axisBottom(x)
.ticks(24)
.tickSizeOuter(0)
)
.call(g =>
g
.append("text")
.attr("x", width - margin.right)
.attr("y", -4)
.attr("fill", "currentColor")
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.text(data.x)
)
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(height / 40))
.call(g => g.select(".domain").remove())
.call(g =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text(data.y)
)
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