Published
Edited
Jul 17, 2020
2 forks
Importers
Insert cell
md`# Histogram For Integers`
Insert cell
histogramInt(rawData)
Insert cell
histogramInt = (data, options={}) => {
const {
bar_color = 'steelblue',
highlight_color = "orange",
highlight_value = null,
chart_width = width,
chart_height = 300,
margin = {top: 20, right: 20, bottom: 40, left: 40},
y_title = "frequency",
x_title = "value",
start = 0,
tickCount = 10
} = options;
// bin the integer data
const maxValue = data.reduce(function(a, b) {
return Math.max(a, b)
});
const binned = [];
var f = {};
data.forEach(function(i) { f[i] = (f[i]||0) + 1;});
for (let i = start; i < maxValue+1; i++)
{
let v = 0;
if (f[i] !== undefined) v = f[i];
binned.push({ value: i, count: v } )
}
const x = d3.scaleBand()
.domain(d3.range(binned.length))
.range([margin.left, chart_width - margin.right])
.padding(0.1)
const y = d3.scaleLinear()
.domain([0, d3.max(binned, d => d.count)]).nice()
.range([chart_height - margin.bottom, margin.top])
const xAxis = g => g
.attr("transform", `translate(0,${chart_height - margin.bottom})`)
.call(d3.axisBottom(x).tickFormat(i => binned[i].value).tickSizeOuter(0))
.call(g => g.append("text")
.attr("x", (chart_width / 2) - margin.right)
.attr("y", 25)
.attr("fill", "currentColor" )
.attr("font-weight", "bold")
.attr("text-anchor", "center")
.text(x_title));
const maxCount = Math.max.apply(Math, binned.map(function(o) { return o.count; }))
const yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(Math.min(tickCount, maxCount))
.tickFormat(function(e){
if(Math.floor(e) != e)
{
return;
}

return e;
}))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", -margin.left)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text(y_title))

const svg = d3.create("svg")
.attr("viewBox", [0, 0, chart_width, chart_height]);

svg.append("g")
//.attr("fill", bar_color)
.selectAll("rect")
.data(binned)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => y(d.count))
.attr("height", d => y(0) - y(d.count))
.attr("width", x.bandwidth())
.attr("fill", function(d){if (highlight_value !== null && d.value >= highlight_value) return highlight_color; else return bar_color; });
//.style("fill", function(d){ if(d.x0 > 2) {return "orange"} else {return bar_color}});

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

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

return svg.node();

}
Insert cell
binnedData2 = {
const maxValue = rawData.reduce(function(a, b) {
return Math.max(a, b)
});
const array = [];
var f = {};
rawData.forEach(function(i) { f[i] = (f[i]||0) + 1;});
for (let i = 0; i < maxValue+1; i++)
{
let v = 0;
if (f[i] !== undefined) v = f[i];
array.push({ value: i, count: v } )
}
return array;
}
Insert cell
rawData = [5,4,1,2,2,3,3,3,4,4,5]
Insert cell
d3 = require("d3@5")
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