2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7fa8367417b2b2512bca
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 更改条形图的显示方式
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 6
|
2020-09-18 00:13:05 +08:00
|
|
|
forumTopicId: 301481
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: change-the-presentation-of-a-bar-chart
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2020-09-18 00:13:05 +08:00
|
|
|
这里有一些格式的改变可以美化上个挑战中创建的条形图:
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
1) 通过在 CSS 中为 `bar` 的类添加 margin 属性,为每一组之间添加空格以直观的将它们分开
|
|
|
|
|
2020-09-18 00:13:05 +08:00
|
|
|
2) 通过给每个值乘以一个数来缩放高度,增加高度以更好地显示值的差异
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
首先,在 `style` 标签中为 `bar` 类添加 2px 的 `margin` 属性。然后,在 `style()` 方法中修改回调函数,使它返回 10 倍原数值的值(在后面加上 "px")。
|
|
|
|
|
|
|
|
**提示**
|
|
|
|
通过给每一个数值点乘以*相同的*常量值仅仅改变比例。这就像放大,它不会改变底层数据的含义。
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
第一个 `div` 的 `height` 应该为 120 个像素,`margin` 应该为 2 个像素。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(0).css('height') == '120px' &&
|
|
|
|
$('div').eq(0).css('margin-right') == '2px'
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
第二个 `div` 的 `height` 应该为 310 个像素,`margin` 应该为 2 个像素。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(1).css('height') == '310px' &&
|
|
|
|
$('div').eq(1).css('margin-right') == '2px'
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
第三个 `div` 的 `height` 应该为 220 个像素,`margin` 应该为 2 个像素。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(2).css('height') == '220px' &&
|
|
|
|
$('div').eq(2).css('margin-right') == '2px'
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
第四个 `div` 的 `height` 应该为 170 个像素,`margin` 应该为 2 个像素。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(3).css('height') == '170px' &&
|
|
|
|
$('div').eq(3).css('margin-right') == '2px'
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第五个 `div` 的 `height` 应该为 250 个像素,`margin` 应该为 2 个像素。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(4).css('height') == '250px' &&
|
|
|
|
$('div').eq(4).css('margin-right') == '2px'
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第六个 `div` 的 `height` 应该为 180 个像素,`margin` 应该为 2 个像素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(5).css('height') == '180px' &&
|
|
|
|
$('div').eq(5).css('margin-right') == '2px'
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第七个 `div` 的 `height` 应该为 290 个像素,`margin` 应该为 2 个像素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(6).css('height') == '290px' &&
|
|
|
|
$('div').eq(6).css('margin-right') == '2px'
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第八个 `div` 的 `height` 应该为 140 个像素,`margin` 应该为 2 个像素。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(7).css('height') == '140px' &&
|
|
|
|
$('div').eq(7).css('margin-right') == '2px'
|
|
|
|
);
|
|
|
|
```
|
2020-09-18 00:13:05 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
第九个 `div` 的 `height` 应该为 90 个像素,`margin` 应该为 2 个像素。
|
2020-09-18 00:13:05 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('div').eq(8).css('height') == '90px' &&
|
|
|
|
$('div').eq(8).css('margin-right') == '2px'
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
.bar {
|
|
|
|
width: 25px;
|
|
|
|
height: 100px;
|
|
|
|
/* Only change code below this line */
|
|
|
|
|
|
|
|
|
|
|
|
/* Only change code above this line */
|
|
|
|
display: inline-block;
|
|
|
|
background-color: blue;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
|
|
|
|
d3.select("body").selectAll("div")
|
|
|
|
.data(dataset)
|
|
|
|
.enter()
|
|
|
|
.append("div")
|
|
|
|
.attr("class", "bar")
|
|
|
|
.style("height", (d) => (d + "px"))
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
.bar {
|
|
|
|
width: 25px;
|
|
|
|
height: 100px;
|
|
|
|
margin: 2px;
|
|
|
|
display: inline-block;
|
|
|
|
background-color: blue;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
|
|
|
|
d3.select("body").selectAll("div")
|
|
|
|
.data(dataset)
|
|
|
|
.enter()
|
|
|
|
.append("div")
|
|
|
|
.attr("class", "bar")
|
|
|
|
.style("height", (d) => (d * 10 + "px"))
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
```
|