Files

119 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

---
id: 587d7faa367417b2b2512bd4
title: D3 の要素にホバーエフェクトを追加する
challengeType: 6
forumTopicId: 301469
dashedName: add-a-hover-effect-to-a-d3-element
---
# --description--
ユーザーがバーにマウスカーソルを合わせた時にバーがハイライトされるように、効果を追加することができます。 これまでのところ、長方形のスタイリングには D3 と SVG の組み込みメソッドが適用されていますが、CSS も使用できます。
`attr()` メソッドを使用して、CSS クラスを SVG 要素に設定します。 その後、新しいクラスの `:hover` 疑似クラスによって、あらゆるホバーエフェクトのスタイルルールが保持されます。
# --instructions--
`attr()`メソッドを使用して `bar` クラスをすべての `rect` 要素に追加してください。 これによって、バーにマウスカーソルを合わせたときにバーの `fill` の 色が茶色に変わります。
# --hints--
`rect` 要素には `bar` のクラスが必要です。
```js
assert($('rect').attr('class').trim().split(/\s+/g).includes('bar'));
```
# --seed--
## --seed-contents--
```html
<style>
.bar:hover {
fill: brown;
}
</style>
<body>
<script>
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
const w = 500;
const h = 100;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - 3 * d)
.attr("width", 25)
.attr("height", (d, i) => 3 * d)
.attr("fill", "navy")
// Add your code below this line
// Add your code above this line
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text((d) => d)
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - (3 * d) - 3);
</script>
</body>
```
# --solutions--
```html
<style>
.bar:hover {
fill: brown;
}
</style>
<body>
<script>
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
const w = 500;
const h = 100;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - 3 * d)
.attr("width", 25)
.attr("height", (d, i) => 3 * d)
.attr("fill", "navy")
// Add your code below this line
.attr('class', 'bar')
// Add your code above this line
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text((d) => d)
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - (3 * d) - 3);
</script>
</body>
```