-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChart.vue
More file actions
234 lines (207 loc) · 5.61 KB
/
Chart.vue
File metadata and controls
234 lines (207 loc) · 5.61 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<template>
<PageHeader>
Chart
<template #description>
A component for displaying line charts with smooth curves and interactive tooltips.
</template>
</PageHeader>
<div class="chart-props">
<div class="chart-props__item">
<h4 class="chart-props__name">
lines
</h4>
<p class="chart-props__description">
An array of line objects to display on the chart.
</p>
</div>
<div class="chart-props__item">
<h4 class="chart-props__name">
detalization
</h4>
<p class="chart-props__description">
Controls how timestamps are formatted on the X-axis legend and tooltip.
Does not affect data aggregation — only the display format.
</p>
<ul class="chart-props__list">
<li><code>'days'</code> — shows day and month (e.g., "19 dec")</li>
<li><code>'hours'</code> — shows day, month, and time (e.g., "19 dec, 14:00")</li>
<li><code>'minutes'</code> — shows day, month, and time (e.g., "19 dec, 14:30")</li>
</ul>
<div class="chart-props__control">
<span class="chart-props__control-label">Try it:</span>
<Select
v-model="detalizationSelected"
:align="{ vertically: 'below', horizontally: 'left' }"
:is-disabled="false"
:items="detalizationItems"
/>
</div>
</div>
</div>
<Heading :level="3">
Single Line
</Heading>
<div class="chart-example">
<div class="chart-example__showcase">
<Chart
:lines="[singleLineData]"
:detalization="currentDetalization"
/>
</div>
</div>
<Heading :level="3">
Multiple Lines
</Heading>
<div class="chart-example">
<div class="chart-example__showcase">
<Chart
:lines="multipleLinesData"
:detalization="currentDetalization"
/>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
import PageHeader from '../../components/PageHeader.vue';
import { Chart, ChartLineColor, Heading, Select } from '../../../src/vue';
import type { ChartItem, ChartLine } from '../../../src/vue/components/chart';
import type { ContextMenuItem, DefaultItem } from '../../../src/vue/components/context-menu/ContextMenu.types';
/**
* Detalization types for chart timestamp formatting
*/
type DetalizationValue = 'minutes' | 'hours' | 'days';
/**
* Mapping from Select title to detalization value
*/
const detalizationMap: Record<string, DetalizationValue> = {
days: 'days',
hours: 'hours',
minutes: 'minutes',
};
/**
* Generate sample chart data
*
* @param points - Number of data points to generate
* @param intervalSeconds - Time interval between points in seconds
* @param baseValue - Base value for random count generation
*/
function generateData(points: number, intervalSeconds: number, baseValue = 100): ChartItem[] {
const now = Math.floor(Date.now() / 1000);
return Array.from({ length: points }, (_, i) => ({
timestamp: now - (points - i) * intervalSeconds,
count: Math.floor(Math.random() * baseValue) + Math.floor(baseValue / 2),
}));
}
/**
* Empty handler for select option activation
*/
const onActivate = (): void => {};
/**
* Currently selected detalization option
*/
const detalizationSelected = ref<DefaultItem>({
title: 'days',
onActivate,
});
/**
* Available detalization options for the Select component
*/
const detalizationItems: ContextMenuItem[] = [
{ title: 'days',
onActivate },
{ title: 'hours',
onActivate },
{ title: 'minutes',
onActivate },
];
/**
* Current detalization value derived from selected option
*/
const currentDetalization = computed<DetalizationValue>(() => {
return detalizationMap[detalizationSelected.value.title] || 'days';
});
/**
* Single line chart data - 30 days of events
*/
const singleLineData = computed<ChartLine>(() => ({
label: 'events',
data: generateData(30, 86400, 2000),
color: ChartLineColor.Red,
}));
/**
* Multiple lines chart data - accepted and filtered events
*/
const multipleLinesData = computed<ChartLine[]>(() => [
{
label: 'accepted',
data: generateData(30, 86400, 150),
color: ChartLineColor.Red,
},
{
label: 'filtered',
data: generateData(30, 86400, 50),
color: ChartLineColor.LightGrey,
},
]);
</script>
<style scoped>
.chart-props {
display: flex;
flex-direction: column;
gap: var(--spacing-l);
margin-bottom: var(--spacing-xl);
&__item {
padding: var(--spacing-m);
background-color: var(--base--bg-secondary);
border-radius: var(--radius-m);
}
&__name {
margin: 0 0 var(--spacing-s);
}
&__description {
margin: 0 0 var(--spacing-s);
color: var(--base--text-secondary);
}
&__code {
margin: 0;
padding: var(--spacing-s);
background-color: var(--base--bg-primary);
border-radius: var(--radius-s);
overflow-x: auto;
}
&__list {
display: flex;
flex-direction: column;
gap: var(--spacing-s);
margin: 0 0 var(--spacing-m);
padding-left: var(--spacing-l);
color: var(--base--text-secondary);
code {
padding: var(--spacing-xxs) var(--spacing-ms);
background-color: var(--base--bg-primary);
border-radius: var(--radius-s);
}
}
&__control {
display: flex;
align-items: center;
gap: var(--spacing-s);
}
&__control-label {
color: var(--base--text-secondary);
}
}
.chart-example {
display: grid;
grid-template-columns: 1fr;
gap: var(--spacing-l);
margin-bottom: var(--spacing-xl);
position: relative;
&__showcase {
width: 100%;
background-color: var(--base--bg-secondary);
border-radius: var(--radius-m);
}
}
</style>