1
- import 'react-app-polyfill/ie11' ; // For IE 11 support
2
- import 'react-app-polyfill/stable' ;
3
- import React , { useState , useEffect , useRef , useMemo } from 'react' ;
1
+ import React , { useState , useEffect , useRef , useMemo } from 'react' ;
4
2
import PropTypes from 'prop-types' ;
5
- import Chart from 'chart.js'
6
- import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs'
7
- import " @coreui/chartjs/dist/css/coreui-chartjs.css" ;
3
+ import Chart from 'chart.js' ;
4
+ import { customTooltips as cuiCustomTooltips } from '@coreui/chartjs' ;
5
+ import ' @coreui/chartjs/dist/css/coreui-chartjs.css' ;
8
6
9
7
//component - CoreUI / CCharts
10
8
11
- const CCharts = props => {
9
+ const CCharts = props => {
12
10
13
11
const {
14
12
//
@@ -23,7 +21,7 @@ const CCharts = props=>{
23
21
24
22
//console.log(Chart);
25
23
26
- const compData = useRef ( { firstRun :true } ) . current ;
24
+ const compData = useRef ( { firstRun : true } ) . current ;
27
25
const [ chart , setChart ] = useState ( ) ;
28
26
const ref = useRef ( ) ;
29
27
@@ -32,58 +30,58 @@ const CCharts = props=>{
32
30
33
31
// methods
34
32
35
- const renderChart = ( ) => {
36
- destroyChart ( )
33
+ const renderChart = ( ) => {
34
+ destroyChart ( ) ;
37
35
setChart ( new Chart (
38
36
ref . current . getContext ( '2d' ) ,
39
37
chartConfig
40
- ) )
41
- }
42
- const updateChart = ( ) => {
43
- Object . assign ( chart , chartConfig )
44
- chart . update ( )
45
- }
46
- const destroyChart = ( ) => {
38
+ ) ) ;
39
+ } ;
40
+ const updateChart = ( ) => {
41
+ Object . assign ( chart , chartConfig ) ;
42
+ chart . update ( ) ;
43
+ } ;
44
+ const destroyChart = ( ) => {
47
45
if ( chart ) {
48
- chart . destroy ( )
46
+ chart . destroy ( ) ;
49
47
}
50
- }
48
+ } ;
51
49
52
50
// vars
53
51
54
52
const _uid = '' ;
55
- const safeId = ( ( ) => {
53
+ const safeId = ( ( ) => {
56
54
// as long as this._uid() works there is no need to generate the key
57
- const key = ( ) => Math . random ( ) . toString ( 36 ) . replace ( '0.' , '' )
58
- return '__safe_id__' + ( _uid || key ( ) )
55
+ const key = ( ) => Math . random ( ) . toString ( 36 ) . replace ( '0.' , '' ) ;
56
+ return '__safe_id__' + ( _uid || key ( ) ) ;
59
57
} ) ( ) ;
60
- const computedDatasets = ( ( ) => {
61
- return datasets
58
+ const computedDatasets = ( ( ) => {
59
+ return datasets ;
62
60
} ) ( ) ;
63
61
//
64
- const computedLabels = ( ( ) => {
62
+ const computedLabels = ( ( ) => {
65
63
if ( labels && typeof labels !== 'string' ) {
66
- return labels
64
+ return labels ;
67
65
} else if ( ! datasets || ! datasets [ 0 ] || ! datasets [ 0 ] . data ) {
68
- return [ ]
66
+ return [ ] ;
69
67
}
70
- const emptyLabels = Array ( datasets [ 0 ] . data . length ) . fill ( '' )
68
+ const emptyLabels = Array ( datasets [ 0 ] . data . length ) . fill ( '' ) ;
71
69
if ( labels === 'indexes' ) {
72
- return emptyLabels . map ( ( u , i ) => i + 1 )
70
+ return emptyLabels . map ( ( u , i ) => i + 1 ) ;
73
71
} else if ( labels === 'months' ) {
74
- return emptyLabels . map ( ( u , i ) => months [ i % 12 ] )
72
+ return emptyLabels . map ( ( u , i ) => months [ i % 12 ] ) ;
75
73
}
76
- return emptyLabels
74
+ return emptyLabels ;
77
75
} ) ( ) ;
78
- const computedData = ( ( ) => {
76
+ const computedData = ( ( ) => {
79
77
return {
80
78
datasets : computedDatasets ,
81
79
labels : computedLabels
82
- }
80
+ } ;
83
81
} ) ( ) ;
84
- const customTooltips = ( ( ) => {
82
+ const customTooltips = ( ( ) => {
85
83
if ( options && options . tooltips ) {
86
- return
84
+ return ;
87
85
}
88
86
return {
89
87
tooltips : {
@@ -95,52 +93,52 @@ const CCharts = props=>{
95
93
callbacks : {
96
94
labelColor ( tooltipItem , chart ) {
97
95
function getValue ( prop ) {
98
- return typeof prop === 'object' ? prop [ tooltipItem . index ] : prop
96
+ return typeof prop === 'object' ? prop [ tooltipItem . index ] : prop ;
99
97
}
100
- const dataset = chart . data . datasets [ tooltipItem . datasetIndex ]
98
+
99
+ const dataset = chart . data . datasets [ tooltipItem . datasetIndex ] ;
101
100
//tooltipLabelColor is coreUI custom prop used only here
102
101
const backgroundColor = getValue (
103
102
dataset . tooltipLabelColor ||
104
103
dataset . pointHoverBackgroundColor ||
105
104
dataset . borderColor ||
106
105
dataset . backgroundColor
107
- )
106
+ ) ;
108
107
return {
109
108
backgroundColor
110
- }
109
+ } ;
111
110
}
112
111
}
113
112
}
114
- }
113
+ } ;
115
114
} ) ( ) ;
116
- const computedOptions = ( ( ) => {
117
- return Object . assign ( { } , options , customTooltips || { } )
115
+ const computedOptions = ( ( ) => {
116
+ return Object . assign ( { } , options , customTooltips || { } ) ;
118
117
} ) ( ) ;
119
- const chartConfig = ( ( ) => {
118
+ const chartConfig = ( ( ) => {
120
119
return {
121
120
type : type ,
122
121
data : computedData ,
123
122
options : computedOptions || options ,
124
123
plugins : plugins
125
- }
124
+ } ;
126
125
} ) ( ) ;
127
126
128
127
//watch
129
128
130
129
//chartConfig
131
- useMemo ( ( ) => {
130
+ useMemo ( ( ) => {
132
131
if ( compData . firstRun ) return ;
133
132
updateChart ( ) ;
134
133
} , [ chartConfig ] ) ;
135
134
136
-
137
135
// effect
138
136
139
137
useEffect ( ( ) => {
140
138
renderChart ( ) ;
141
139
compData . firstRun = false ;
142
140
return ( ) => {
143
- destroyChart ( )
141
+ destroyChart ( ) ;
144
142
} ;
145
143
} , [ ] ) ;
146
144
@@ -152,7 +150,7 @@ const CCharts = props=>{
152
150
</ div >
153
151
) ;
154
152
155
- }
153
+ } ;
156
154
157
155
CCharts . propTypes = {
158
156
//
@@ -164,7 +162,6 @@ CCharts.propTypes = {
164
162
type : PropTypes . string
165
163
} ;
166
164
167
- CCharts . defaultProps = {
168
- } ;
165
+ CCharts . defaultProps = { } ;
169
166
170
167
export default CCharts ;
0 commit comments