22import { computed } from ' vue'
33import { Line } from ' vue-chartjs'
44import { Chart as ChartJS , registerables } from ' chart.js'
5+ import annotationPlugin from ' chartjs-plugin-annotation'
56import ' chartjs-adapter-date-fns'
67
7- ChartJS .register (... registerables)
8+ import {
9+ CHART_COMMON_OPTIONS ,
10+ CHART_SCALE_X_OPTIONS ,
11+ resampleTimedData ,
12+ setChartDatetimeRange ,
13+ } from ' @/utils/commonCharts.js'
14+
15+ ChartJS .register (... registerables, annotationPlugin)
816ChartJS .defaults .color = ' #dee2e6'
917ChartJS .defaults .borderColor = ' #495057'
1018
@@ -13,39 +21,39 @@ const props = defineProps({
1321 type: Array ,
1422 required: true ,
1523 },
24+ timePickerState: {
25+ type: Object ,
26+ required: true ,
27+ },
28+ pickedSnapshotTs: {
29+ type: Date ,
30+ required: true ,
31+ },
32+ resampleUnitCount: {
33+ type: Number ,
34+ required: true ,
35+ },
36+ resampleUnit: {
37+ type: String ,
38+ required: true ,
39+ },
1640})
1741
1842const CHART_UNITS = [' pkt' , ' flw' , ' B' ]
1943const CHART_COLORS = [' #0DCAF0' , ' #198754' , ' #FFC107' ]
2044
2145const chartOptions = computed (() => {
22- let scaleXOptions = {
23- type: ' time' ,
24- time: {
25- displayFormats: {
26- millisecond: ' HH:mm:ss.SSS' ,
27- second: ' HH:mm:ss' ,
28- minute: ' HH:mm' ,
29- hour: ' HH:mm' ,
30- },
31- tooltipFormat: ' dd.MM. HH:mm' ,
32- },
33- ticks: {
34- autoSkip: false ,
35- maxRotation: 0 ,
36- major: {
37- enabled: true ,
38- },
39- },
40- }
46+ let scaleXOptions = { ... CHART_SCALE_X_OPTIONS }
4147
42- if (props .activity .length > 0 ) {
43- // + 'Z' to treat as UTC
44- scaleXOptions .min = new Date (props .activity [0 ].t2 + ' Z' )
45- scaleXOptions .max = new Date (props .activity [props .activity .length - 1 ].t2 + ' Z' )
46- }
48+ // Set the time range of the chart
49+ setChartDatetimeRange (scaleXOptions, props .timePickerState .from , props .timePickerState .to )
4750
4851 return {
52+ ... CHART_COMMON_OPTIONS,
53+ interaction: {
54+ intersect: false ,
55+ mode: ' index' ,
56+ },
4957 scales: {
5058 x: scaleXOptions,
5159 packets: {
@@ -85,16 +93,24 @@ const chartOptions = computed(() => {
8593 return ` ${ item .formattedValue } ${ unit} `
8694 },
8795 },
96+ usePointStyle: true ,
97+ },
98+ annotation: {
99+ annotations: {
100+ line1: {
101+ type: ' line' ,
102+ xMin: props .pickedSnapshotTs ,
103+ xMax: props .pickedSnapshotTs ,
104+ borderColor: ' #d980fa' ,
105+ borderWidth: 2 ,
106+ },
107+ },
88108 },
89109 },
90- animation: {
91- duration: 0 ,
92- },
93- maintainAspectRatio: false ,
94110 }
95111})
96112const chartData = computed (() => {
97- const data = props .activity .map ((dp ) => {
113+ let data = props .activity .map ((dp ) => {
98114 return {
99115 t: new Date (dp .t2 + ' Z' ),
100116 packets: dp .v .packets ,
@@ -103,6 +119,24 @@ const chartData = computed(() => {
103119 }
104120 })
105121
122+ // Resample data to avoid too many points
123+ data = resampleTimedData (
124+ data,
125+ ' t' ,
126+ props .resampleUnitCount ,
127+ props .resampleUnit ,
128+ (bucketData , bucketDt ) => {
129+ return [
130+ {
131+ t: bucketDt,
132+ packets: bucketData .reduce ((acc , dp ) => acc + dp .packets , 0 ),
133+ flows: bucketData .reduce ((acc , dp ) => acc + dp .flows , 0 ),
134+ bytes: bucketData .reduce ((acc , dp ) => acc + dp .bytes , 0 ),
135+ },
136+ ]
137+ },
138+ )
139+
106140 return {
107141 datasets: [
108142 {
@@ -114,8 +148,10 @@ const chartData = computed(() => {
114148 yAxisKey: ' packets' ,
115149 },
116150 borderColor: CHART_COLORS [0 ],
117- pointRadius: 4 ,
118- pointHitRadius: 5 ,
151+ pointRadius: 6 ,
152+ pointHoverRadius: 6 ,
153+ pointHitRadius: 7 ,
154+ pointStyle: ' rect' ,
119155 },
120156 {
121157 label: ' Flows' ,
@@ -126,8 +162,10 @@ const chartData = computed(() => {
126162 yAxisKey: ' flows' ,
127163 },
128164 borderColor: CHART_COLORS [1 ],
129- pointRadius: 4 ,
130- pointHitRadius: 5 ,
165+ pointRadius: 6 ,
166+ pointHoverRadius: 6 ,
167+ pointHitRadius: 7 ,
168+ pointStyle: ' circle' ,
131169 },
132170 {
133171 label: ' Bytes' ,
@@ -138,8 +176,10 @@ const chartData = computed(() => {
138176 yAxisKey: ' bytes' ,
139177 },
140178 borderColor: CHART_COLORS [2 ],
141- pointRadius: 4 ,
142- pointHitRadius: 5 ,
179+ pointRadius: 6 ,
180+ pointHoverRadius: 6 ,
181+ pointHitRadius: 7 ,
182+ pointStyle: ' star' ,
143183 },
144184 ],
145185 }
0 commit comments