Skip to content

Commit b580a37

Browse files
committed
refactor(dashboard): drop obsolete ChartModule, move to ChartjsModule and c-chart component
1 parent 9490791 commit b580a37

File tree

4 files changed

+97
-136
lines changed

4 files changed

+97
-136
lines changed
Lines changed: 83 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Injectable } from '@angular/core';
22
import { getStyle, hexToRgba } from '@coreui/utils/src';
3-
import { customTooltips } from '@coreui/chartjs/dist/js/coreui-chartjs.js';
43

54
export 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
})
1918
export 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
}

src/app/views/dashboard/dashboard.component.html

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,13 @@ <h4 class="card-title mb-0" id="traffic">
5959
</form>
6060
</c-col>
6161
</c-row>
62-
<div
63-
[@.disabled]="true"
64-
[ngStyle]="{ 'height.px': 300, 'marginTop.px': 40 }"
65-
class="chart-wrapper"
66-
>
67-
<canvas
68-
[chartType]="mainChart.type"
69-
[colors]="mainChart.colors"
70-
[datasets]="mainChart.Data"
71-
[labels]="mainChart.labels"
72-
[options]="mainChart.options"
73-
cNoZoneBaseChart
74-
class="chart"
75-
>
76-
</canvas>
77-
</div>
62+
<c-chart [data]="mainChart.data"
63+
[height]="300"
64+
[ngStyle]="{'marginTop.px': 40}"
65+
[options]="mainChart.options"
66+
[type]="mainChart.type">
67+
Main chart
68+
</c-chart>
7869
</c-card-body>
7970
<c-card-footer>
8071
<c-row class="text-center" [md]="5" [xs]="1">

src/app/views/dashboard/dashboard.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ interface IUser {
2222
styleUrls: ['dashboard.component.scss']
2323
})
2424
export class DashboardComponent implements OnInit {
25+
constructor(private chartsData: DashboardChartsData) {
26+
}
27+
2528
public users: IUser[] = [
2629
{
2730
name: 'Yiorgos Avraamu',
@@ -108,9 +111,6 @@ export class DashboardComponent implements OnInit {
108111
trafficRadio: new FormControl('Month')
109112
});
110113

111-
constructor(private chartsData: DashboardChartsData) {
112-
}
113-
114114
ngOnInit(): void {
115115
this.initCharts();
116116
}
@@ -120,7 +120,7 @@ export class DashboardComponent implements OnInit {
120120
}
121121

122122
setTrafficPeriod(value: string): void {
123-
this.trafficRadioGroup.setValue({trafficRadio: value});
123+
this.trafficRadioGroup.setValue({ trafficRadio: value });
124124
this.chartsData.initMainChart(value);
125125
this.initCharts();
126126
}

src/app/views/dashboard/dashboard.module.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
ButtonGroupModule,
88
ButtonModule,
99
CardModule,
10-
ChartModule,
1110
FormModule,
1211
GridModule,
1312
NavModule,
@@ -16,6 +15,7 @@ import {
1615
TabsModule
1716
} from '@coreui/angular';
1817
import { IconModule } from '@coreui/icons-angular';
18+
import { ChartjsModule } from '@coreui/angular-chartjs';
1919

2020
import { DashboardRoutingModule } from './dashboard-routing.module';
2121
import { DashboardComponent } from './dashboard.component';
@@ -37,14 +37,12 @@ import { WidgetsModule } from '../widgets/widgets.module';
3737
FormModule,
3838
ButtonModule,
3939
ButtonGroupModule,
40-
ChartModule,
40+
ChartjsModule,
4141
AvatarModule,
4242
TableModule,
4343
WidgetsModule
4444
],
45-
declarations: [
46-
DashboardComponent,
47-
]
45+
declarations: [DashboardComponent]
4846
})
4947
export class DashboardModule {
5048
}

0 commit comments

Comments
 (0)