9
9
</div >
10
10
</template >
11
11
12
- <script >
12
+ <script lang="ts" >
13
13
import { PolarArea } from " vue-chartjs" ;
14
- import { RadialLinearScale , ArcElement , Legend , Tooltip } from " chart.js" ;
15
- import { registerChartComponents , commonOptions , tooltipLabelColor } from " ./chartConfig" ;
14
+ import { RadialLinearScale , ArcElement , Legend , Tooltip , type TooltipItem } from " chart.js" ;
15
+ import { registerChartComponents , commonOptions , tooltipLabelColor } from " ./chartConfig.ts " ;
16
16
import formatter from " @/mixins/formatter" ;
17
17
import colors , { dimColor } from " @/colors" ;
18
- import { TYPES , GROUPS } from " ./types" ;
19
18
import LegendList from " ./LegendList.vue" ;
19
+ import { defineComponent , type PropType } from " vue" ;
20
+ import type { CURRENCY } from " @/types/evcc" ;
21
+ import { TYPES , GROUPS , type Session } from " ./types.ts" ;
20
22
21
23
registerChartComponents ([RadialLinearScale , ArcElement , Legend , Tooltip ]);
22
24
23
- export default {
25
+ export default defineComponent ( {
24
26
name: " AvgCostGroupedChart" ,
25
27
components: { PolarArea , LegendList },
26
28
mixins: [formatter ],
27
29
props: {
28
- sessions: { type: Array , default : () => [] },
29
- currency: { type: String , default: " EUR" },
30
- groupBy: { type: String , default: GROUPS .LOADPOINT },
30
+ sessions: { type: Array as PropType <Session []>, default : () => [] },
31
+ currency: { type: String as PropType <CURRENCY >, default: " EUR" },
32
+ groupBy: {
33
+ type: String as PropType <Exclude <GROUPS , GROUPS .NONE >>,
34
+ default: GROUPS .LOADPOINT ,
35
+ },
31
36
colorMappings: { type: Object , default : () => ({ loadpoint: {}, vehicle: {} }) },
32
37
suggestedMax: { type: Number , default: 0 },
33
- costType: { type: String , default: TYPES .PRICE },
38
+ costType: { type: String as PropType < TYPES > , default: TYPES .PRICE },
34
39
},
35
40
computed: {
36
41
chartData() {
37
42
console .log (` update ${this .costType } grouped data ` );
38
- const aggregatedData = {};
43
+ const aggregatedData: Record < string , { energy : number ; cost : number }> = {};
39
44
40
45
this .sessions .forEach ((session ) => {
41
46
const groupKey = session [this .groupBy ];
@@ -45,10 +50,10 @@ export default {
45
50
const chargedEnergy = session .chargedEnergy ;
46
51
if (this .costType === TYPES .CO2 ) {
47
52
aggregatedData [groupKey ].energy += chargedEnergy ;
48
- aggregatedData[groupKey].cost += session .co2PerKWh * chargedEnergy;
53
+ aggregatedData [groupKey ].cost += ( session .co2PerKWh || 0 ) * chargedEnergy ;
49
54
} else if (this .costType === TYPES .PRICE ) {
50
55
aggregatedData [groupKey ].energy += chargedEnergy ;
51
- aggregatedData[groupKey].cost += session .price ;
56
+ aggregatedData [groupKey ].cost += session .price || 0 ;
52
57
}
53
58
});
54
59
@@ -85,7 +90,7 @@ export default {
85
90
aspectRatio: 1 ,
86
91
borderRadius: 8 ,
87
92
borderWidth: 3 ,
88
- color: colors .text ,
93
+ color: colors .text || " " ,
89
94
spacing: 0 ,
90
95
radius: " 100%" ,
91
96
plugins: {
@@ -96,45 +101,47 @@ export default {
96
101
position: " topBottomCenter" ,
97
102
callbacks: {
98
103
title : () => null ,
99
- label : (tooltipItem ) => {
100
- const { label , raw = 0 } = tooltipItem;
104
+ label : (tooltipItem : TooltipItem <" polarArea" >) => {
105
+ const { label, dataset, dataIndex } = tooltipItem ;
106
+ const d = dataset .data [dataIndex ];
107
+
101
108
return (
102
109
label +
103
110
" : " +
104
111
(this .costType === TYPES .CO2
105
- ? this .fmtCo2Long (raw )
106
- : this .fmtPricePerKWh (raw , this .currency ))
112
+ ? this .fmtCo2Long (d )
113
+ : this .fmtPricePerKWh (d , this .currency ))
107
114
);
108
115
},
109
116
labelColor: tooltipLabelColor (true ),
110
117
},
111
- },
118
+ } as any ,
112
119
},
113
120
scales: {
114
121
r: {
115
122
suggestedMin: 0 ,
116
123
suggestedMax: this .suggestedMax ,
117
124
beginAtZero: false ,
118
125
ticks: {
119
- color: colors .muted ,
120
- backdropColor: colors .background ,
126
+ color: colors .muted || " " ,
127
+ backdropColor: colors .background || " " ,
121
128
font: { size: 10 },
122
129
callback: this .formatValue ,
123
130
maxTicksLimit: 6 ,
124
131
},
125
132
angleLines: { display: false },
126
- grid: { color: colors .border },
127
- },
133
+ grid: { color: colors .border || " " },
134
+ } as any ,
128
135
},
129
136
};
130
137
},
131
138
},
132
139
methods: {
133
- formatValue (value ) {
140
+ formatValue(value : number ) {
134
141
return this .costType === TYPES .CO2
135
142
? this .fmtCo2Medium (value )
136
143
: this .fmtPricePerKWh (value , this .currency );
137
144
},
138
145
},
139
- };
146
+ }) ;
140
147
</script >
0 commit comments