File size: 2,950 Bytes
f61d311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Injectable } from '@angular/core';
import { getStyle, hexToRgba } from '@coreui/utils/src';
import { Predictions } from 'src/app/predictions.service';
import { TableRow } from './dashboard.component';

export interface IChartProps {
  data?: any;
  labels?: any;
  options?: any;
  colors?: any;
  type?: any;
  legend?: any;

  [propName: string]: any;
}

@Injectable({
  providedIn: 'any'
})
export class DashboardChartsData {
  public mainChart: IChartProps = {};
  public predicted: number[] = [];
  public actual: number[] = [];


  initMainChart(data: TableRow[]) {
    const styleActual = '#20a8d8';
    const stylePredicted = '#f86c6b';
    const stylePredictedBg = hexToRgba(getStyle('--cui-info'), 10) ?? '#20a8d8';

    this.predicted = data.map(d => d.predicted)
    this.actual = data.map(d => d.actual).filter(d => !!d) as number[];
    this.mainChart['elements'] = data.length;
    this.mainChart['Predicted'] = this.predicted;
    this.mainChart['Actual'] = this.actual;

    let labels: string[] = data.map(d => d.time);

    const colors = [
      {
        // stylePredicted
        backgroundColor: stylePredictedBg,
        borderColor: stylePredicted,
        borderDash: [5, 5],
        pointHoverBackgroundColor: stylePredicted,
        fill: "+1",
      },
      {
        // styleActual
        backgroundColor: 'transparent',
        borderColor: styleActual,
        pointHoverBackgroundColor: '#fff',
        fill: true
      },
    ];

    const datasets = [
      {
        data: this.mainChart['Predicted'],
        label: 'Predicted',
        ...colors[0]
      },
      {
        data: this.mainChart['Actual'],
        label: 'Actual',
        ...colors[1]
      }
    ];
    // if(this.mainChart["Actual"].length > 0) {
    //   datasets.push(
        
    //   )
    // }

    const plugins = {
      legend: {
        display: true,
        position: "top"
      },
      tooltip: {
        callbacks: {
          labelColor: function(context: any) {
            return {
              backgroundColor: context.dataset.borderColor
            };
          }
        }
      }
    };

    const options = {
      maintainAspectRatio: false,
      plugins,
      scales: {
        x: {
          grid: {
            drawOnChartArea: true
          }
        },
        y: {
          beginAtZero: true,
          max: 100,
          ticks: {
            callback: (val: string, _index: number, _ticks: unknown) => {
              return `${val} °F`
            },
            maxTicksLimit: 20,
            stepSize: 5
          }
        }
      },
      elements: {
        line: {
          tension: 0
        },
        point: {
          radius: 0,
          hitRadius: 10,
          hoverRadius: 4,
          hoverBorderWidth: 3
        }
      }
    };

    this.mainChart.type = 'line';
    this.mainChart.options = options;
    this.mainChart.data = {
      datasets,
      labels
    };
  }

}