1
+ import * as React from 'react' ;
1
2
import * as d3 from 'd3' ;
2
- import React , { useEffect , useRef , useState } from 'react' ;
3
3
4
4
const LineChart = ( props ) => {
5
- const d3Container = useRef < HTMLDivElement | null | any > ( null ) ;
6
- const [ containerWidth , setContainerWidth ] = useState ( 0 ) ;
5
+ const d3Container = React . useRef < HTMLDivElement | null | any > ( null ) ;
6
+ const [ containerWidth , setContainerWidth ] = React . useState ( 0 ) ;
7
7
8
8
const drawChart = ( width ) => {
9
9
if ( ! d3Container ) {
@@ -14,7 +14,7 @@ const LineChart = (props) => {
14
14
const svg = d3 . select ( d3Container . current ) ;
15
15
svg . selectAll ( '*' ) . remove ( ) ;
16
16
17
- const margin = { top : 24 , right : 48 , bottom : 32 , left : 48 } ;
17
+ const margin = { top : 16 , right : 0 , bottom : 32 , left : 32 } ;
18
18
const height = + svg . attr ( 'height' ) - margin . top - margin . bottom ;
19
19
const drawWidth = width - margin . left - margin . right ;
20
20
@@ -45,12 +45,12 @@ const LineChart = (props) => {
45
45
. domain ( [ 0 , d3 . max ( props . data , ( d ) => d . value ) ] )
46
46
. range ( [ height , 0 ] ) ;
47
47
48
- const yAxis = g . append ( 'g' ) . call ( d3 . axisLeft ( yScale ) . tickSize ( - drawWidth ) . tickPadding ( 8 ) ) ;
48
+ const yAxis = g . append ( 'g' ) . call ( d3 . axisLeft ( yScale ) . ticks ( 6 ) . tickSize ( - drawWidth ) . tickPadding ( 8 ) ) ;
49
49
yAxis . attr ( 'shape-rendering' , 'crispEdges' ) . selectAll ( '.tick text' ) . style ( 'font-size' , 'var(--type-scale-fixed-small)' ) . style ( 'fill' , 'var(--theme-border)' ) ;
50
50
yAxis . selectAll ( '.domain, .tick line' ) . attr ( 'stroke' , 'var(--theme-border)' ) ;
51
51
yAxis . selectAll ( '.domain' ) . remove ( ) ;
52
52
53
- const xAxis = g . append ( 'g' ) . attr ( 'transform' , `translate(0,${ height } )` ) . call ( d3 . axisBottom ( xScale ) ) ;
53
+ const xAxis = g . append ( 'g' ) . attr ( 'transform' , `translate(0,${ height } )` ) . call ( d3 . axisBottom ( xScale ) . ticks ( 6 ) ) ;
54
54
xAxis . selectAll ( '.tick text' ) . style ( 'fill' , 'var(--theme-border)' ) . style ( 'font-size' , 'var(--type-scale-fixed-small)' ) ;
55
55
xAxis . selectAll ( '.tick line, .domain' ) . style ( 'stroke' , 'var(--theme-border)' ) ;
56
56
xAxis . selectAll ( '.domain' ) . remove ( ) ;
@@ -117,10 +117,8 @@ const LineChart = (props) => {
117
117
const yUpper = yScale ( d . upper_ci ) ;
118
118
const yMidpoint = ( yLower + yUpper ) / 2 ;
119
119
120
- // Midpoint
121
120
g . append ( 'circle' ) . attr ( 'cx' , x ) . attr ( 'cy' , yMidpoint ) . attr ( 'r' , 3 ) . attr ( 'fill' , color ) ;
122
121
123
- // Midpoint label
124
122
g . append ( 'text' )
125
123
. attr ( 'x' , x + 5 )
126
124
. attr ( 'y' , yMidpoint )
@@ -149,7 +147,7 @@ const LineChart = (props) => {
149
147
} ) ;
150
148
}
151
149
152
- useEffect ( ( ) => {
150
+ React . useEffect ( ( ) => {
153
151
if ( ! d3Container || ! d3Container . current ) {
154
152
return ;
155
153
}
@@ -167,7 +165,7 @@ const LineChart = (props) => {
167
165
return ( ) => window . removeEventListener ( 'resize' , handleResize ) ;
168
166
} , [ props . data ] ) ;
169
167
170
- useEffect ( ( ) => {
168
+ React . useEffect ( ( ) => {
171
169
drawChart ( containerWidth ) ;
172
170
} , [ containerWidth , props . data , props . showErrorBars ] ) ;
173
171
0 commit comments