|
@@ -6,6 +6,18 @@ const scores = [
|
|
{ name: "Emily", score: 88 }
|
|
{ name: "Emily", score: 88 }
|
|
];
|
|
];
|
|
|
|
|
|
|
|
+function scaleBar(selection, scale) {
|
|
|
|
+ selection.style('transform', `scaleX(${scale})`);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function fade(selection, opacity) {
|
|
|
|
+ selection.style('fill-opacity', opacity);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function setFill(selection, color) {
|
|
|
|
+ selection.style('fill', color);
|
|
|
|
+}
|
|
|
|
+
|
|
const bar = d3.select(".chart")
|
|
const bar = d3.select(".chart")
|
|
.append('svg')
|
|
.append('svg')
|
|
.attr('width', 225)
|
|
.attr('width', 225)
|
|
@@ -24,23 +36,24 @@ const bar = d3.select(".chart")
|
|
bar.append('rect')
|
|
bar.append('rect')
|
|
.style("width", d => d.score)
|
|
.style("width", d => d.score)
|
|
.text(d => d.name)
|
|
.text(d => d.name)
|
|
- // classed('bar', true) to set, classed('bar', false) to remove.
|
|
|
|
.attr("class", "bar")
|
|
.attr("class", "bar")
|
|
|
|
|
|
- // Contrived example: '.bar:hover' in CSS would have been sufficient.
|
|
|
|
- // A fat arrow function would have a lexical this instead of the proper one.
|
|
|
|
.on('mouseover', function (d, i, elements) {
|
|
.on('mouseover', function (d, i, elements) {
|
|
d3.select(this)
|
|
d3.select(this)
|
|
- .style('transform', 'scaleX(2)');
|
|
|
|
- // Like 'this', 'elements' are plain DOM elements.
|
|
|
|
|
|
+ .call(scaleBar, 1.5)
|
|
|
|
+ .call(setFill, 'orange');
|
|
|
|
+
|
|
d3.selectAll(elements)
|
|
d3.selectAll(elements)
|
|
.filter(':not(:hover)')
|
|
.filter(':not(:hover)')
|
|
- .style('fill-opacity', 0.2);
|
|
|
|
|
|
+ .call(fade, 0.2);
|
|
})
|
|
})
|
|
.on('mouseout', function (d, i, elements) {
|
|
.on('mouseout', function (d, i, elements) {
|
|
|
|
+ d3.select(this)
|
|
|
|
+ .call(scaleBar, 1) // call returns the (modified) selection.
|
|
|
|
+ .call(setFill, 'palegreen'); // ...which allows chaining.
|
|
|
|
+
|
|
d3.selectAll(elements)
|
|
d3.selectAll(elements)
|
|
- .style('fill-opacity', 1)
|
|
|
|
- .style('transform', 'scaleX(1)');
|
|
|
|
|
|
+ .call(fade, 1);
|
|
})
|
|
})
|
|
|
|
|
|
.on('click', (who) => console.log(`hi ${who.name}`));
|
|
.on('click', (who) => console.log(`hi ${who.name}`));
|