Skip to content

Commit d4e37d0

Browse files
committed
update dependencies and format code
1 parent 191a78f commit d4e37d0

25 files changed

Lines changed: 338 additions & 435 deletions

README.md

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ const sub = (a, b) => {
357357
console.log(add(10, 30), sub(10, 5));
358358

359359
// functional style programming
360-
arr.forEach(d => (x += d));
361-
const arr2 = arr.map(d => d * 10);
362-
const arrf = arr.filter(d => d < 3);
360+
arr.forEach((d) => (x += d));
361+
const arr2 = arr.map((d) => d * 10);
362+
const arrf = arr.filter((d) => d < 3);
363363
// function are first-level objects
364364
function compute(f, a, b) {
365365
return f(a, b);
@@ -398,10 +398,7 @@ circle.attr("cx", 20);
398398
circle.attr("cy", 23);
399399

400400
// alternative syntax via chaining
401-
circle
402-
.attr("r", 10)
403-
.attr("cx", 20)
404-
.attr("cy", 23);
401+
circle.attr("r", 10).attr("cx", 20).attr("cy", 23);
405402

406403
// set css styles
407404
circle.style("stroke-width", 2);
@@ -424,10 +421,7 @@ The DOM elements can be manipulated using. `append` and `remove`
424421

425422
```js
426423
const body = d3.select("body");
427-
body
428-
.append("svg")
429-
.attr("width", 800)
430-
.attr("height", 600);
424+
body.append("svg").attr("width", 800).attr("height", 600);
431425

432426
d3.select("svg").remove();
433427
```
@@ -464,15 +458,15 @@ const circles = d3
464458
.selectAll("circle")
465459
.data(data)
466460
.join(
467-
enter => {
461+
(enter) => {
468462
// append an element matching the selector and set constant attributes
469463
const circles_enter = enter.append("circle");
470464
circles_enter.attr("r", 10);
471465
return circles_enter;
472466
},
473467
// update existing elements
474-
update => update,
475-
exit => {
468+
(update) => update,
469+
(exit) => {
476470
// exit phase
477471
return exit.remove();
478472
}
@@ -497,7 +491,7 @@ const data = [1, 2, 3];
497491
d3.select("svg")
498492
.selectAll("circle")
499493
.data(data)
500-
.join(enter => enter.append("circle").attr("r", 10))
494+
.join((enter) => enter.append("circle").attr("r", 10))
501495
.attr("cx", (d, i) => d * 10)
502496
.attr("cy", (d, i) => i * 50);
503497
```
@@ -543,24 +537,20 @@ Nested data join [![Open in CodePen][codepen]](https://codepen.io/sgratzl/pen/yL
543537
// hierarchical data
544538
const data = [
545539
{ name: "a", arr: [1, 2, 3] },
546-
{ name: "b", arr: [3, 2, 4] }
540+
{ name: "b", arr: [3, 2, 4] },
547541
];
548542

549-
const groups = d3
550-
.select("svg")
551-
.selectAll("g")
552-
.data(data)
553-
.join("g");
543+
const groups = d3.select("svg").selectAll("g").data(data).join("g");
554544

555545
groups.attr("transform", (d, i) => `translate(${i * 20 + 10},10)`);
556546

557547
// select all circles within each group and bind the inner array per data item
558548
const circles = groups
559549
.selectAll("circle")
560-
.data(d => d.arr)
550+
.data((d) => d.arr)
561551
.join("circle");
562552

563-
circles.attr("r", d => d * 2).attr("cy", (d, i) => i * 20);
553+
circles.attr("r", (d) => d * 2).attr("cy", (d, i) => i * 20);
564554
```
565555

566556
Nested selection [![Open in CodePen][codepen]](https://codepen.io/sgratzl/pen/VwwZZjY):
@@ -571,7 +561,7 @@ const circles = d3
571561
.select("svg")
572562
.selectAll("circle")
573563
.data(data)
574-
.join(enter => {
564+
.join((enter) => {
575565
const circles_enter = enter.append("circle").attr("r", 10);
576566
// need to be separate since .append returns the appended element
577567
circles_enter.append("title");
@@ -580,7 +570,7 @@ const circles = d3
580570

581571
circles.attr("cx", (d, i) => d * 10).attr("cy", (d, i) => i * 50);
582572

583-
circles.select("title").text(d => d);
573+
circles.select("title").text((d) => d);
584574
```
585575

586576
---
@@ -601,22 +591,22 @@ In the current version we have static hard-coded data in our files. D3 provides
601591

602592
```js
603593
d3.json("file_to_load.json")
604-
.then(data => {
594+
.then((data) => {
605595
// do something with the data
606596
})
607-
.catch(error => {
597+
.catch((error) => {
608598
console.error("Error loading the data");
609599
});
610600
```
611601

612602
```js
613603
d3.csv("file_to_load.csv")
614-
.then(data => {
604+
.then((data) => {
615605
// array of objects
616606
console.log(data.length);
617607
// do something with the data
618608
})
619-
.catch(error => {
609+
.catch((error) => {
620610
console.error("Error loading the data");
621611
});
622612
```
@@ -661,10 +651,7 @@ With version 5 D3 extracted the color schemes to it on repository located at
661651
https://github.com/d3/d3-scale-chromatic. Including both D3 standard schemes (e.g. `d3.schemeCategory10`) but also the ones from [ColorBrewer](http://colorbrewer2.org/) (e.g. `d3.schemeSet3`). These can be used as `range` for an ordinal scale.
662652

663653
```js
664-
const cscale = d3
665-
.scaleOrdinal()
666-
.domain(["a", "b", "c"])
667-
.range(d3.schemeCategory10);
654+
const cscale = d3.scaleOrdinal().domain(["a", "b", "c"]).range(d3.schemeCategory10);
668655
```
669656

670657
```js
@@ -694,19 +681,12 @@ circles.attr('cx', (d) => scale(d));
694681
In addition, it is quite common adding a axis for your charts. D3 provides a utility function for this case : `d3.axisBottom()`, `d3.axisLeft()`, `d3.axisRight()`, `d3.axisTop()`. It uses a scale as input and the necessary SVG elements for you.
695682

696683
```js
697-
const scale = d3
698-
.scaleLinear()
699-
.domain([0, 5])
700-
.range([0, 200]);
684+
const scale = d3.scaleLinear().domain([0, 5]).range([0, 200]);
701685

702686
const axis = d3.axisBottom().scale(scale);
703687

704688
// create a container to put the axis
705-
const axis_container = d3
706-
.select("svg")
707-
.append("g")
708-
.attr("class", "axis")
709-
.attr("transform", "translate(0,200)");
689+
const axis_container = d3.select("svg").append("g").attr("class", "axis").attr("transform", "translate(0,200)");
710690

711691
// call axis to create the SVG elements for you
712692
axis_container.call(axis);
@@ -734,13 +714,13 @@ const circles = d3
734714
.select("svg")
735715
.selectAll("circle")
736716
.data(data)
737-
.join(enter =>
717+
.join((enter) =>
738718
enter
739719
.append("circle")
740720
.attr("r", 10)
741721
.attr("cy", 40)
742722
.attr("cx", (d, i) => 30 + i * 30)
743-
.on("click", function(d, i) {
723+
.on("click", function (d, i) {
744724
console.log(`clicked on: ${d} (${i})`);
745725
const circle = d3.select(this); // can't use arrow scoping
746726
circle.style("stroke", "orange");
@@ -773,12 +753,7 @@ const circles = d3
773753
.selectAll("circle")
774754
.data(data)
775755
.join(
776-
enter =>
777-
enter
778-
.append("circle")
779-
.attr("r", 10)
780-
.attr("cx", 100)
781-
.attr("cy", 100) // useful default values for animation
756+
(enter) => enter.append("circle").attr("r", 10).attr("cx", 100).attr("cy", 100) // useful default values for animation
782757
);
783758

784759
circles
@@ -797,24 +772,15 @@ D3 is rather dumb when it comes to mapping data items to DOM elements. It doesn'
797772

798773
```js
799774
const cscale = d3.scaleOrdinal(d3.schemeCategory10).domain(["a", "b", "c", "d"]);
800-
const xscale = d3
801-
.scaleBand()
802-
.domain(["a", "b", "c", "d"])
803-
.range([10, 200]);
775+
const xscale = d3.scaleBand().domain(["a", "b", "c", "d"]).range([10, 200]);
804776

805777
function update(data) {
806778
const s = d3.select("svg");
807779
// key argument return a unique key/id per data-item (string)
808780
const circles = s
809781
.selectAll("circle")
810-
.data(data, d => d)
811-
.join(enter =>
812-
enter
813-
.append("circle")
814-
.attr("r", 10)
815-
.attr("cx", xscale)
816-
.style("fill", cscale)
817-
);
782+
.data(data, (d) => d)
783+
.join((enter) => enter.append("circle").attr("r", 10).attr("cx", xscale).style("fill", cscale));
818784

819785
// a will be bound to the first DOM element
820786
circles.transition().attr("cy", (d, i) => 10 + i * 20);
@@ -1192,7 +1158,6 @@ const axis = d3.axisLeft();
11921158
d3.select<SVGGElement, unknown>(".axis.x").call(axis);
11931159
```
11941160

1195-
11961161
---
11971162

11981163
Thank You

examples/barchart02_title.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,20 @@ <h1>Student's First Barchart</h1>
5757
.join(
5858
// ENTER
5959
// new elements
60-
enter => enter.append("rect").attr("x", 0),
60+
(enter) => enter.append("rect").attr("x", 0),
6161
// UPDATE
6262
// update existing elements
63-
update => update,
63+
(update) => update,
6464
// EXIT
6565
// elements that aren't associated with data
66-
exit => exit.remove()
66+
(exit) => exit.remove()
6767
);
6868

6969
// ENTER + UPDATE
7070
// both old and new elements
7171
rect
7272
.attr("height", bar_height)
73-
.attr("width", d => d * 7)
73+
.attr("width", (d) => d * 7)
7474
.attr("y", (d, i) => i * (bar_height + 5));
7575
</script>
7676
</body>

examples/barchart03_json.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,27 @@ <h1>Student's First Barchart</h1>
5959
.join(
6060
// ENTER
6161
// new elements
62-
enter => {
62+
(enter) => {
6363
const rect_enter = enter.append("rect").attr("x", 0);
6464
rect_enter.append("title");
6565
return rect_enter;
6666
},
6767
// UPDATE
6868
// update existing elements
69-
update => update,
69+
(update) => update,
7070
// EXIT
7171
// elements that aren't associated with data
72-
exit => exit.remove()
72+
(exit) => exit.remove()
7373
);
7474

7575
// ENTER + UPDATE
7676
// both old and new elements
7777
rect
7878
.attr("height", bar_height)
79-
.attr("width", d => d * 7)
79+
.attr("width", (d) => d * 7)
8080
.attr("y", (d, i) => i * (bar_height + 5));
8181

82-
rect.select("title").text(d => `value: ${d}`);
82+
rect.select("title").text((d) => `value: ${d}`);
8383
</script>
8484
</body>
8585
</html>

examples/barchart04_scale.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h1>Student's First Barchart</h1>
5050
// x a linear scale with range: [0, width] and domain from 0 max temperature
5151
// y a band ordinal scale range: [0, height] and domain the different cities
5252

53-
d3.json("weather.json").then(json => {
53+
d3.json("weather.json").then((json) => {
5454
data = json;
5555

5656
update(data);
@@ -66,27 +66,27 @@ <h1>Student's First Barchart</h1>
6666
.join(
6767
// ENTER
6868
// new elements
69-
enter => {
69+
(enter) => {
7070
const rect_enter = enter.append("rect").attr("x", 0);
7171
rect_enter.append("title");
7272
return rect_enter;
7373
},
7474
// UPDATE
7575
// update existing elements
76-
update => update,
76+
(update) => update,
7777
// EXIT
7878
// elements that aren't associated with data
79-
exit => exit.remove()
79+
(exit) => exit.remove()
8080
);
8181

8282
// ENTER + UPDATE
8383
// both old and new elements
8484
rect
8585
.attr("height", bar_height)
86-
.attr("width", d => d.temperature * 7)
86+
.attr("width", (d) => d.temperature * 7)
8787
.attr("y", (d, i) => i * (bar_height + 5));
8888

89-
rect.select("title").text(d => d.location.city);
89+
rect.select("title").text((d) => d.location.city);
9090
}
9191
</script>
9292
</body>

0 commit comments

Comments
 (0)