@@ -4,7 +4,13 @@ import maxBy from 'lodash.maxby';
4
4
import max from 'lodash.max' ;
5
5
import orderBy from 'lodash.orderby' ;
6
6
import { Delaunay } from 'd3-delaunay' ;
7
- import { scaleOrdinal , scaleLinear , scaleThreshold , scaleSqrt } from 'd3-scale' ;
7
+ import {
8
+ scaleOrdinal ,
9
+ scaleLinear ,
10
+ scaleThreshold ,
11
+ scaleSqrt ,
12
+ scaleLog ,
13
+ } from 'd3-scale' ;
8
14
import minBy from 'lodash.minby' ;
9
15
import UNDPColorModule from 'undp-viz-colors' ;
10
16
import flattenDeep from 'lodash.flattendeep' ;
@@ -49,6 +55,8 @@ export function Graph(props: Props) {
49
55
selectedIncomeGroups,
50
56
selectedCountryGroup,
51
57
keepAxisSame,
58
+ xScaleType,
59
+ yScaleType,
52
60
} = useContext ( Context ) as CtxDataType ;
53
61
const [ selectedColor , setSelectedColor ] = useState < string | undefined > (
54
62
undefined ,
@@ -342,17 +350,28 @@ export function Graph(props: Props) {
342
350
: refYVal
343
351
: ( minBy ( dataFormatted , d => d . yVal ) ?. yVal as number )
344
352
: 0 ;
345
-
346
- const xScale = scaleLinear ( )
347
- . domain ( [ xMinValue > 0 ? 0 : xMinValue , xMaxValue ] )
348
- . range ( [ 0 , graphWidth ] )
349
- . nice ( ) ;
350
- const yScale = scaleLinear ( )
351
- . domain ( [ yMinValue > 0 ? 0 : yMinValue , yMaxValue ] )
352
- . range ( [ graphHeight , 0 ] )
353
- . nice ( ) ;
354
- const xTicks = xScale . ticks ( 5 ) ;
355
- const yTicks = yScale . ticks ( 5 ) ;
353
+ const xScaleLogAllowed = ! (
354
+ xScaleType === 'linear' ||
355
+ fullArray . filter ( d => ( d . x as number ) <= 0 ) . length > 0
356
+ ) ;
357
+ const yScaleLogAllowed = ! (
358
+ yScaleType === 'linear' ||
359
+ fullArray . filter ( d => ( d . y as number ) <= 0 ) . length > 0
360
+ ) ;
361
+ const xScale = ! xScaleLogAllowed
362
+ ? scaleLinear ( )
363
+ . domain ( [ xMinValue > 0 ? 0 : xMinValue , xMaxValue ] )
364
+ . range ( [ 0 , graphWidth ] )
365
+ . nice ( )
366
+ : scaleLog ( ) . domain ( [ xMinValue , xMaxValue ] ) . range ( [ 0 , graphWidth ] ) . nice ( ) ;
367
+ const yScale = ! yScaleLogAllowed
368
+ ? scaleLinear ( )
369
+ . domain ( [ yMinValue > 0 ? 0 : yMinValue , yMaxValue ] )
370
+ . range ( [ graphHeight , 0 ] )
371
+ . nice ( )
372
+ : scaleLog ( ) . domain ( [ yMinValue , yMaxValue ] ) . range ( [ graphHeight , 0 ] ) . nice ( ) ;
373
+ const xTicks = ! xScaleLogAllowed ? xScale . ticks ( 5 ) : xScale . ticks ( 3 ) ;
374
+ const yTicks = ! yScaleLogAllowed ? yScale . ticks ( 5 ) : yScale . ticks ( 3 ) ;
356
375
const voronoiDiagram = Delaunay . from (
357
376
dataFormatted ,
358
377
d => xScale ( d . xVal as number ) ,
@@ -552,6 +571,7 @@ export function Graph(props: Props) {
552
571
stroke = '#AAA'
553
572
strokeWidth = { 1 }
554
573
strokeDasharray = '4,8'
574
+ opacity = { yScaleLogAllowed && i === 0 ? 0 : 1 }
555
575
/>
556
576
< text
557
577
x = { 0 }
@@ -569,21 +589,21 @@ export function Graph(props: Props) {
569
589
< line
570
590
x1 = { 0 }
571
591
x2 = { graphWidth }
572
- y1 = { yScale ( 0 ) }
573
- y2 = { yScale ( 0 ) }
592
+ y1 = { yScaleLogAllowed ? graphHeight : yScale ( 0 ) }
593
+ y2 = { yScaleLogAllowed ? graphHeight : yScale ( 0 ) }
574
594
stroke = { UNDPColorModule . graphGray }
575
595
strokeWidth = { 1 }
576
596
/>
577
597
< text
578
598
x = { 0 }
579
- y = { yScale ( 0 ) }
599
+ y = { yScaleLogAllowed ? graphHeight : yScale ( 0 ) }
580
600
fill = { UNDPColorModule . graphGray }
581
601
textAnchor = 'end'
582
602
fontSize = { 12 }
583
603
dy = { 4 }
584
604
dx = { - 3 }
585
605
>
586
- 0
606
+ { yScaleLogAllowed ? '' : 0 }
587
607
</ text >
588
608
< text
589
609
transform = { `translate(-50, ${ graphHeight / 2 } ) rotate(-90)` }
@@ -602,6 +622,7 @@ export function Graph(props: Props) {
602
622
TRUNCATE_MAX_TEXT_LENGTH ,
603
623
) } ...`
604
624
: yIndicatorMetaData . IndicatorLabel }
625
+ { yScaleLogAllowed ? ' (Log scale)' : '' }
605
626
</ text >
606
627
</ g >
607
628
< g >
@@ -615,14 +636,15 @@ export function Graph(props: Props) {
615
636
stroke = '#AAA'
616
637
strokeWidth = { 1 }
617
638
strokeDasharray = '4,8'
639
+ opacity = { xScaleLogAllowed && i === 0 ? 0 : 1 }
618
640
/>
619
641
< text
620
642
x = { xScale ( d ) }
621
643
y = { graphHeight }
622
644
fill = { UNDPColorModule . graphGray }
623
645
textAnchor = 'middle'
624
646
fontSize = { 12 }
625
- dy = { 12 }
647
+ dy = { 15 }
626
648
>
627
649
{ Math . abs ( d ) < 1 ? d : format ( '~s' ) ( d ) . replace ( 'G' , 'B' ) }
628
650
</ text >
@@ -631,20 +653,20 @@ export function Graph(props: Props) {
631
653
< line
632
654
y1 = { 0 }
633
655
y2 = { graphHeight }
634
- x1 = { xScale ( 0 ) }
635
- x2 = { xScale ( 0 ) }
656
+ x1 = { xScaleLogAllowed ? 0 : xScale ( 0 ) }
657
+ x2 = { xScaleLogAllowed ? 0 : xScale ( 0 ) }
636
658
stroke = { UNDPColorModule . graphGray }
637
659
strokeWidth = { 1 }
638
660
/>
639
661
< text
640
- x = { xScale ( 0 ) }
662
+ x = { xScaleLogAllowed ? 0 : xScale ( 0 ) }
641
663
y = { graphHeight }
642
664
fill = { UNDPColorModule . graphGray }
643
665
textAnchor = 'middle'
644
666
fontSize = { 12 }
645
667
dy = { 15 }
646
668
>
647
- { 0 }
669
+ { xScaleLogAllowed ? '' : 0 }
648
670
</ text >
649
671
< text
650
672
transform = { `translate(${ graphWidth / 2 } , ${ graphHeight } )` }
@@ -664,6 +686,7 @@ export function Graph(props: Props) {
664
686
TRUNCATE_MAX_TEXT_LENGTH ,
665
687
) } ...`
666
688
: xIndicatorMetaData . IndicatorLabel }
689
+ { xScaleLogAllowed ? ' (Log scale)' : '' }
667
690
</ text >
668
691
</ g >
669
692
@@ -866,7 +889,11 @@ export function Graph(props: Props) {
866
889
: UNDPColorModule . graphGray
867
890
}
868
891
/>
869
- { showLabel ? (
892
+ { showLabel &&
893
+ ( selectedCountries . length === 0 ||
894
+ selectedCountries . indexOf (
895
+ countryData [ 'Country or Area' ] ,
896
+ ) !== - 1 ) ? (
870
897
< text
871
898
fontSize = { 10 }
872
899
fill = {
0 commit comments