Files

212 lines
5.9 KiB
Markdown
Raw Permalink Normal View History

---
id: 587d7fab367417b2b2512bd8
title: 円要素に属性を追加する
challengeType: 6
forumTopicId: 301471
dashedName: add-attributes-to-the-circle-elements
---
# --description--
前回のチャレンジでは、`dataset` 内の各点に `circle` 要素を作成し、それらを SVG キャンバスに追加しました。 しかし D3 でこれらを正しく表示するには、各 `circle` の位置とサイズに関する詳細情報が必要です。
SVG の `circle` には主要な 3 つの属性があります。 `cx``cy` 属性は座標です。 これらは、SVG キャンバスのどこに図形の *center* を配置するかを指定します。 半径 (`r` 属性) は、`circle` の大きさを指定します。
`rect` `y` 座標と同様に、`circle``cy` 属性は SVG キャンバスの最下部からではなく最上部から測定します。
3 つの属性はすべてコールバック関数を使用して動的に値を設定できます。 留意点として、`data(dataset)` の後にチェーンされたすべてのメソッドは、`dataset` 内のアイテムごとに 1 回実行されます。 コールバック関数の `d` パラメータは、それぞれの点の配列である `dataset` 内の現在のアイテムを参照します。 `d[0]` のような角括弧表記を使用して、配列内の値にアクセスします。
# --instructions--
`cx``cy`、および `r` 属性を `circle` 要素に追加してください。 `cx` の値は、`dataset` 内の各アイテムに対する配列の最初の数でなければなりません。 `cy` の値は配列の 2 番目の数値を基にする必要がありますが、チャートを逆さではなく正しい方向で表示してください。 すべての円の `r` の値を `5` にする必要があります。
# --hints--
10 個の `circle` 要素が必要です。
```js
assert($('circle').length == 10);
```
最初の `circle` 要素には `cx``34``cy``422``r``5` が必要です。
```js
assert(
$('circle').eq(0).attr('cx') == '34' &&
$('circle').eq(0).attr('cy') == '422' &&
$('circle').eq(0).attr('r') == '5'
);
```
2 番目の `circle` 要素には `cx``109``cy``220``r``5` が必要です。
```js
assert(
$('circle').eq(1).attr('cx') == '109' &&
$('circle').eq(1).attr('cy') == '220' &&
$('circle').eq(1).attr('r') == '5'
);
```
3 番目の `circle` 要素には `cx``310``cy``380``r``5` が必要です。
```js
assert(
$('circle').eq(2).attr('cx') == '310' &&
$('circle').eq(2).attr('cy') == '380' &&
$('circle').eq(2).attr('r') == '5'
);
```
4 番目の `circle` 要素には `cx``79``cy``89``r``5` が必要です。
```js
assert(
$('circle').eq(3).attr('cx') == '79' &&
$('circle').eq(3).attr('cy') == '89' &&
$('circle').eq(3).attr('r') == '5'
);
```
5 番目の `circle` 要素には `cx``420``cy``280``r``5` が必要です。
```js
assert(
$('circle').eq(4).attr('cx') == '420' &&
$('circle').eq(4).attr('cy') == '280' &&
$('circle').eq(4).attr('r') == '5'
);
```
6 番目の `circle` 要素には `cx``233``cy``355``r``5` が必要です。
```js
assert(
$('circle').eq(5).attr('cx') == '233' &&
$('circle').eq(5).attr('cy') == '355' &&
$('circle').eq(5).attr('r') == '5'
);
```
7 番目の `circle` 要素には `cx``333``cy``404``r``5` が必要です。
```js
assert(
$('circle').eq(6).attr('cx') == '333' &&
$('circle').eq(6).attr('cy') == '404' &&
$('circle').eq(6).attr('r') == '5'
);
```
8 番目の `circle` 要素には `cx``222``cy``167``r``5` が必要です。
```js
assert(
$('circle').eq(7).attr('cx') == '222' &&
$('circle').eq(7).attr('cy') == '167' &&
$('circle').eq(7).attr('r') == '5'
);
```
9 番目の `circle` 要素には `cx``78``cy``180``r``5` が必要です。
```js
assert(
$('circle').eq(8).attr('cx') == '78' &&
$('circle').eq(8).attr('cy') == '180' &&
$('circle').eq(8).attr('r') == '5'
);
```
10 番目の `circle` 要素には `cx``21``cy``377``r``5` が必要です。
```js
assert(
$('circle').eq(9).attr('cx') == '21' &&
$('circle').eq(9).attr('cy') == '377' &&
$('circle').eq(9).attr('r') == '5'
);
```
# --seed--
## --seed-contents--
```html
<body>
<script>
const dataset = [
[ 34, 78 ],
[ 109, 280 ],
[ 310, 120 ],
[ 79, 411 ],
[ 420, 220 ],
[ 233, 145 ],
[ 333, 96 ],
[ 222, 333 ],
[ 78, 320 ],
[ 21, 123 ]
];
const w = 500;
const h = 500;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
// Add your code below this line
// Add your code above this line
</script>
</body>
```
# --solutions--
```html
<body>
<script>
const dataset = [
[ 34, 78 ],
[ 109, 280 ],
[ 310, 120 ],
[ 79, 411 ],
[ 420, 220 ],
[ 233, 145 ],
[ 333, 96 ],
[ 222, 333 ],
[ 78, 320 ],
[ 21, 123 ]
];
const w = 500;
const h = 500;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", (d) => d[0])
.attr("cy", (d) => h - d[1])
.attr("r", 5)
</script>
</body>
```