/
BandwidthChart.vue
135 lines (117 loc) · 3.58 KB
/
BandwidthChart.vue
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
135
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
<template>
<VChart
:key="chartKey"
chart-id="bandwidth-chart"
:chart-data="chartData"
:width="width"
:height="height"
:tooltip-constructor="tooltip"
/>
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { ChartType, TooltipModel, ChartData } from 'chart.js';
import { Tooltip, TooltipParams, TooltipId, ChartTooltipData } from '@/types/chart';
import { DataStamp } from '@/types/projects';
import { ChartUtils } from '@/utils/chart';
import VChart from '@/components/VChart.vue';
const props = withDefaults(defineProps<{
data: DataStamp[],
since: Date,
before: Date,
width: number,
height: number,
}>(), {
data: () => [],
since: () => new Date(),
before: () => new Date(),
width: 0,
height: 0,
});
const chartKey = ref<number>(0);
/**
* Returns formatted data to render chart.
*/
const chartData = computed((): ChartData => {
const data: number[] = props.data.map(el => el.value);
const xAxisDateLabels: string[] = ChartUtils.daysDisplayedOnChart(props.since, props.before);
return {
labels: xAxisDateLabels,
datasets: [{
data,
fill: true,
backgroundColor: '#c3c3c310',
borderColor: '#c3c3c3',
pointHoverBackgroundColor: '#FFFFFF',
pointBorderColor: '#c3c3c3',
pointHoverBorderWidth: 3,
hoverRadius: 8,
hitRadius: 3,
pointRadius: 2,
pointBorderWidth: 1,
pointBackgroundColor: '#FFFFFF',
order: 0,
}],
};
});
/**
* Used as constructor of custom tooltip.
*/
function tooltip(tooltipModel: TooltipModel<ChartType>): void {
const tooltipParams = new TooltipParams(tooltipModel, 'bandwidth-chart', TooltipId.Bandwidth,
allocatedTooltipMarkUp(tooltipModel), 76, 81);
Tooltip.custom(tooltipParams);
}
/**
* Returns allocated bandwidth tooltip's html mark up.
*/
function allocatedTooltipMarkUp(tooltipModel: TooltipModel<ChartType>): string {
if (!tooltipModel.dataPoints) {
return '';
}
const dataIndex = tooltipModel.dataPoints[0].dataIndex;
const dataPoint = new ChartTooltipData(props.data[dataIndex]);
return `<div class='allocated-tooltip'>
<p class='allocated-tooltip__value'>${dataPoint.date}<b class='allocated-tooltip__value__bold'> / ${dataPoint.value}</b></p>
<div class='allocated-tooltip__arrow'></div>
</div>`;
}
watch(() => props.width, () => {
chartKey.value += 1;
});
</script>
<style lang="scss">
.allocated-tooltip {
margin: 8px;
position: relative;
border-radius: 100px;
padding-top: 8px;
width: 145px;
font-family: 'font_regular', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #e4e4e4;
box-shadow: rgb(0 0 0 / 3%) 0 1px 10px 10px !important;
&__value {
font-size: 14px;
line-height: 26px;
text-align: center;
color: #444;
white-space: nowrap;
&__bold {
font-family: 'font_medium', sans-serif;
}
}
&__arrow {
width: 12px;
height: 12px;
border-radius: 8px 0 0;
transform: scale(1, 0.85) translate(0, 20%) rotate(45deg);
margin-bottom: -4px;
background-color: #e4e4e4;
}
}
</style>