Public
Edited
Apr 25, 2023
Insert cell
Insert cell
Insert cell
Inputs.button([
["One", () => filter("name","one")], // calls the function filter with specific inputs
["Two", () => filter("name","two")],
["Three", () => filter("name","three")],
["Four", () => filter("name","four")],
["Five", () => filter("name","five")],
[">= 2", () => filterGreater("value",2)], // or calls the function filterGreater with inputs
[">= 3", () => filterGreater("value",3)],
[">= 4", () => filterGreater("value",4)]
])
Insert cell
Insert cell
data = [
{"name": "one", "value": 1},
{"name": "two", "value": 2},
{"name": "three", "value": 3},
{"name": "four", "value": 4},
{"name": "five", "value": 5}
]
Insert cell
circles = viz.select("#layer1").selectAll(".mycircle")
.data(data)
.join("circle")
.classed("mycircle", true)
.attr("cx",d => d.value * 100)
.attr("cy", 150)
.attr("r", d => d.value * 10)
.style("fill","steelblue");
Insert cell
Insert cell
Insert cell
dimAll = () => {
// first a function to dim (change opacity) of all of the circle objects
circles.style("opacity",0.1);
}
Insert cell
filter = (field,val) => {
// create a function to increase opacity only for a single element, via its data.
dimAll(); // first we call dimAll to dim all of them.
circles.filter(d => d[field] === val).style("opacity", 1); // find the ones where field = value and change opacity
}
Insert cell
filterGreater = (field,val) => {
// create a function to increase opacity only for a single element, via its data.
dimAll(); // first we call dimAll to dim all of them.
circles.filter(d => d[field] >= val).style("opacity", 1); // find the ones where field = value and change opacity
}
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