1
1
import { Injectable } from '@angular/core' ;
2
2
import { getStyle , hexToRgba } from '@coreui/utils/src' ;
3
- import { customTooltips } from '@coreui/chartjs/dist/js/coreui-chartjs.js' ;
4
3
5
4
export interface IChartProps {
6
- Data ?: any ;
5
+ data ?: any ;
7
6
labels ?: any ;
8
7
options ?: any ;
9
8
colors ?: any ;
@@ -17,12 +16,12 @@ export interface IChartProps {
17
16
providedIn : 'any'
18
17
} )
19
18
export class DashboardChartsData {
20
- public mainChart : IChartProps = { } ;
21
-
22
19
constructor ( ) {
23
20
this . initMainChart ( ) ;
24
21
}
25
22
23
+ public mainChart : IChartProps = { } ;
24
+
26
25
public random ( min : number , max : number ) {
27
26
return Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) ;
28
27
}
@@ -33,8 +32,6 @@ export class DashboardChartsData {
33
32
const brandInfoBg = hexToRgba ( getStyle ( '--cui-info' ) , 10 ) ?? '#20a8d8' ;
34
33
const brandDanger = getStyle ( '--cui-danger' ) || '#f86c6b' ;
35
34
36
- // console.log(brandInfo, brandInfoBg);
37
-
38
35
// mainChart
39
36
this . mainChart . elements = period === 'Month' ? 12 : 27 ;
40
37
this . mainChart . Data1 = [ ] ;
@@ -43,28 +40,14 @@ export class DashboardChartsData {
43
40
44
41
// generate random values for mainChart
45
42
for ( let i = 0 ; i <= this . mainChart . elements ; i ++ ) {
46
- this . mainChart . Data1 . push ( this . random ( 50 , 200 ) ) ;
47
- this . mainChart . Data2 . push ( this . random ( 80 , 100 ) ) ;
43
+ this . mainChart . Data1 . push ( this . random ( 50 , 240 ) ) ;
44
+ this . mainChart . Data2 . push ( this . random ( 20 , 160 ) ) ;
48
45
this . mainChart . Data3 . push ( 65 ) ;
49
46
}
50
47
51
- this . mainChart . Data = [
52
- {
53
- data : this . mainChart . Data1 ,
54
- label : 'Current'
55
- } ,
56
- {
57
- data : this . mainChart . Data2 ,
58
- label : 'Previous'
59
- } ,
60
- {
61
- data : this . mainChart . Data3 ,
62
- label : 'BEP'
63
- }
64
- ] ;
65
-
48
+ let labels : string [ ] = [ ] ;
66
49
if ( period === 'Month' ) {
67
- this . mainChart . labels = [
50
+ labels = [
68
51
'January' ,
69
52
'February' ,
70
53
'March' ,
@@ -80,124 +63,113 @@ export class DashboardChartsData {
80
63
] ;
81
64
} else {
82
65
/* tslint:disable:max-line-length */
83
- this . mainChart . labels = [
84
- 'Monday' ,
85
- 'Tuesday' ,
86
- 'Wednesday' ,
87
- 'Thursday' ,
88
- 'Friday' ,
89
- 'Saturday' ,
90
- 'Sunday' ,
91
- 'Monday' ,
92
- 'Tuesday' ,
93
- 'Wednesday' ,
94
- 'Thursday' ,
95
- 'Friday' ,
96
- 'Saturday' ,
97
- 'Sunday' ,
66
+ const week = [
98
67
'Monday' ,
99
68
'Tuesday' ,
100
69
'Wednesday' ,
101
70
'Thursday' ,
102
71
'Friday' ,
103
72
'Saturday' ,
104
- 'Sunday' ,
105
- 'Monday' ,
106
- 'Thursday' ,
107
- 'Wednesday' ,
108
- 'Thursday' ,
109
- 'Friday' ,
110
- 'Saturday' ,
111
73
'Sunday'
112
74
] ;
75
+ labels = week . concat ( week , week , week ) ;
113
76
}
114
- /* tslint:enable:max-line-length */
115
- // @ts -ignore
116
- this . mainChart . options = {
117
- tooltips : {
118
- enabled : false ,
119
- custom : customTooltips ,
120
- intersect : true ,
121
- mode : 'index' ,
122
- position : 'nearest' ,
77
+
78
+ const colors = [
79
+ {
80
+ // brandInfo
81
+ backgroundColor : brandInfoBg ,
82
+ borderColor : brandInfo ,
83
+ pointHoverBackgroundColor : brandInfo ,
84
+ borderWidth : 2 ,
85
+ fill : true
86
+ } ,
87
+ {
88
+ // brandSuccess
89
+ backgroundColor : 'transparent' ,
90
+ borderColor : brandSuccess || '#4dbd74' ,
91
+ pointHoverBackgroundColor : '#fff'
92
+ } ,
93
+ {
94
+ // brandDanger
95
+ backgroundColor : 'transparent' ,
96
+ borderColor : brandDanger || '#f86c6b' ,
97
+ pointHoverBackgroundColor : brandDanger ,
98
+ borderWidth : 1 ,
99
+ borderDash : [ 8 , 5 ]
100
+ }
101
+ ] ;
102
+
103
+ const datasets = [
104
+ {
105
+ data : this . mainChart . Data1 ,
106
+ label : 'Current' ,
107
+ ...colors [ 0 ]
108
+ } ,
109
+ {
110
+ data : this . mainChart . Data2 ,
111
+ label : 'Previous' ,
112
+ ...colors [ 1 ]
113
+ } ,
114
+ {
115
+ data : this . mainChart . Data3 ,
116
+ label : 'BEP' ,
117
+ ...colors [ 2 ]
118
+ }
119
+ ] ;
120
+
121
+ const plugins = {
122
+ legend : {
123
+ display : false
124
+ } ,
125
+ tooltip : {
123
126
callbacks : {
124
- labelColor : function ( tooltipItem : { datasetIndex : string | number ; } , chart : { data : { datasets : { [ x : string ] : { borderColor : any ; } ; } ; } ; } ) {
127
+ labelColor : function ( context : any ) {
125
128
return {
126
- backgroundColor :
127
- chart . data . datasets [ tooltipItem . datasetIndex ] . borderColor
129
+ backgroundColor : context . dataset . borderColor
128
130
} ;
129
131
}
130
132
}
131
- } ,
132
- // animation: {
133
- // duration: 0
134
- // },
135
- responsiveAnimationDuration : 0 ,
136
- responsive : true ,
133
+ }
134
+ } ;
135
+
136
+ const options = {
137
137
maintainAspectRatio : false ,
138
+ plugins,
138
139
scales : {
139
- xAxes : [
140
- {
141
- gridLines : {
142
- drawOnChartArea : false
143
- } ,
144
- ticks : {
145
- callback : function ( value : any ) {
146
- return period === 'Month' ? value : value . charAt ( 0 ) ;
147
- }
148
- }
140
+ x : {
141
+ grid : {
142
+ drawOnChartArea : false
149
143
}
150
- ] ,
151
- yAxes : [
152
- {
153
- ticks : {
154
- beginAtZero : true ,
155
- maxTicksLimit : 5 ,
156
- stepSize : Math . ceil ( 250 / 5 ) ,
157
- max : 250
158
- }
144
+ } ,
145
+ y : {
146
+ beginAtZero : true ,
147
+ max : 250 ,
148
+ ticks : {
149
+ maxTicksLimit : 5 ,
150
+ stepSize : Math . ceil ( 250 / 5 )
159
151
}
160
- ]
152
+ }
161
153
} ,
162
154
elements : {
163
155
line : {
164
- borderWidth : 2
156
+ tension : 0.4
165
157
} ,
166
158
point : {
167
159
radius : 0 ,
168
160
hitRadius : 10 ,
169
161
hoverRadius : 4 ,
170
162
hoverBorderWidth : 3
171
163
}
172
- } ,
173
- legend : {
174
- display : false
175
164
}
176
165
} ;
177
- this . mainChart . colors = [
178
- {
179
- // brandInfo
180
- backgroundColor : brandInfoBg ,
181
- borderColor : brandInfo ,
182
- pointHoverBackgroundColor : '#fff'
183
- } ,
184
- {
185
- // brandSuccess
186
- backgroundColor : 'transparent' ,
187
- borderColor : brandSuccess || '#4dbd74' ,
188
- pointHoverBackgroundColor : '#fff'
189
- } ,
190
- {
191
- // brandDanger
192
- backgroundColor : 'transparent' ,
193
- borderColor : brandDanger || '#f86c6b' ,
194
- pointHoverBackgroundColor : '#fff' ,
195
- borderWidth : 1 ,
196
- borderDash : [ 8 , 5 ]
197
- }
198
- ] ;
199
- this . mainChart . legend = false ;
166
+
200
167
this . mainChart . type = 'line' ;
168
+ this . mainChart . options = options ;
169
+ this . mainChart . data = {
170
+ datasets,
171
+ labels
172
+ } ;
201
173
}
202
174
203
175
}
0 commit comments