11import { Injectable } from '@angular/core' ;
22import { getStyle , hexToRgba } from '@coreui/utils/src' ;
3- import { customTooltips } from '@coreui/chartjs/dist/js/coreui-chartjs.js' ;
43
54export interface IChartProps {
6- Data ?: any ;
5+ data ?: any ;
76 labels ?: any ;
87 options ?: any ;
98 colors ?: any ;
@@ -17,12 +16,12 @@ export interface IChartProps {
1716 providedIn : 'any'
1817} )
1918export class DashboardChartsData {
20- public mainChart : IChartProps = { } ;
21-
2219 constructor ( ) {
2320 this . initMainChart ( ) ;
2421 }
2522
23+ public mainChart : IChartProps = { } ;
24+
2625 public random ( min : number , max : number ) {
2726 return Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) ;
2827 }
@@ -33,8 +32,6 @@ export class DashboardChartsData {
3332 const brandInfoBg = hexToRgba ( getStyle ( '--cui-info' ) , 10 ) ?? '#20a8d8' ;
3433 const brandDanger = getStyle ( '--cui-danger' ) || '#f86c6b' ;
3534
36- // console.log(brandInfo, brandInfoBg);
37-
3835 // mainChart
3936 this . mainChart . elements = period === 'Month' ? 12 : 27 ;
4037 this . mainChart . Data1 = [ ] ;
@@ -43,28 +40,14 @@ export class DashboardChartsData {
4340
4441 // generate random values for mainChart
4542 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 ) ) ;
4845 this . mainChart . Data3 . push ( 65 ) ;
4946 }
5047
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 [ ] = [ ] ;
6649 if ( period === 'Month' ) {
67- this . mainChart . labels = [
50+ labels = [
6851 'January' ,
6952 'February' ,
7053 'March' ,
@@ -80,124 +63,113 @@ export class DashboardChartsData {
8063 ] ;
8164 } else {
8265 /* 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 = [
9867 'Monday' ,
9968 'Tuesday' ,
10069 'Wednesday' ,
10170 'Thursday' ,
10271 'Friday' ,
10372 'Saturday' ,
104- 'Sunday' ,
105- 'Monday' ,
106- 'Thursday' ,
107- 'Wednesday' ,
108- 'Thursday' ,
109- 'Friday' ,
110- 'Saturday' ,
11173 'Sunday'
11274 ] ;
75+ labels = week . concat ( week , week , week ) ;
11376 }
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 : {
123126 callbacks : {
124- labelColor : function ( tooltipItem : { datasetIndex : string | number ; } , chart : { data : { datasets : { [ x : string ] : { borderColor : any ; } ; } ; } ; } ) {
127+ labelColor : function ( context : any ) {
125128 return {
126- backgroundColor :
127- chart . data . datasets [ tooltipItem . datasetIndex ] . borderColor
129+ backgroundColor : context . dataset . borderColor
128130 } ;
129131 }
130132 }
131- } ,
132- // animation: {
133- // duration: 0
134- // },
135- responsiveAnimationDuration : 0 ,
136- responsive : true ,
133+ }
134+ } ;
135+
136+ const options = {
137137 maintainAspectRatio : false ,
138+ plugins,
138139 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
149143 }
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 )
159151 }
160- ]
152+ }
161153 } ,
162154 elements : {
163155 line : {
164- borderWidth : 2
156+ tension : 0.4
165157 } ,
166158 point : {
167159 radius : 0 ,
168160 hitRadius : 10 ,
169161 hoverRadius : 4 ,
170162 hoverBorderWidth : 3
171163 }
172- } ,
173- legend : {
174- display : false
175164 }
176165 } ;
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+
200167 this . mainChart . type = 'line' ;
168+ this . mainChart . options = options ;
169+ this . mainChart . data = {
170+ datasets,
171+ labels
172+ } ;
201173 }
202174
203175}
0 commit comments