@@ -357,9 +357,9 @@ const sub = (a, b) => {
357357console .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
364364function compute (f , a , b ) {
365365 return f (a, b);
@@ -398,10 +398,7 @@ circle.attr("cx", 20);
398398circle .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
407404circle .style (" stroke-width" , 2 );
@@ -424,10 +421,7 @@ The DOM elements can be manipulated using. `append` and `remove`
424421
425422``` js
426423const 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
432426d3 .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];
497491d3 .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
544538const 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
555545groups .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
558548const 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
566556Nested 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
581571circles .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
603593d3 .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
613603d3 .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
661651https://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));
694681In 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
702686const 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
712692axis_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
784759circles
@@ -797,24 +772,15 @@ D3 is rather dumb when it comes to mapping data items to DOM elements. It doesn'
797772
798773``` js
799774const 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
805777function 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();
11921158d3 .select <SVGGElement , unknown >(" .axis.x" ).call (axis );
11931159```
11941160
1195-
11961161---
11971162
11981163Thank You
0 commit comments