Skip to content

Commit d6a829e

Browse files
committed
fix(frontend/activity_timeline): use SI prefixes
Modifies activity timeline values to be displayed in SI-prefixed format, e.g. instead of `5100000 B` `5.1 MB`, etc. This change should improve readability.
1 parent 748409e commit d6a829e

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

frontend/src/components/ActivityTimeline.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'chartjs-adapter-date-fns'
88
import {
99
CHART_COMMON_OPTIONS,
1010
CHART_SCALE_X_OPTIONS,
11+
formatSI,
1112
resampleTimedData,
1213
setChartDatetimeRange,
1314
} from '@/utils/commonCharts.js'
@@ -64,7 +65,7 @@ const chartOptions = computed(() => {
6465
x: scaleXOptions,
6566
packets: {
6667
ticks: {
67-
callback: (v) => `${v} ${CHART_UNITS[0]}`,
68+
callback: (v) => formatSI(v, CHART_UNITS[0]),
6869
color: CHART_COLORS[0],
6970
},
7071
grid: { tickColor: CHART_COLORS[0], drawOnChartArea: false },
@@ -73,7 +74,7 @@ const chartOptions = computed(() => {
7374
},
7475
flows: {
7576
ticks: {
76-
callback: (v) => `${v} ${CHART_UNITS[1]}`,
77+
callback: (v) => formatSI(v, CHART_UNITS[1]),
7778
color: CHART_COLORS[1],
7879
},
7980
grid: { tickColor: CHART_COLORS[1], drawOnChartArea: false },
@@ -82,7 +83,7 @@ const chartOptions = computed(() => {
8283
},
8384
bytes: {
8485
ticks: {
85-
callback: (v) => `${v} ${CHART_UNITS[2]}`,
86+
callback: (v) => formatSI(v, CHART_UNITS[2]),
8687
color: CHART_COLORS[2],
8788
},
8889
grid: { tickColor: CHART_COLORS[2] },
@@ -96,7 +97,7 @@ const chartOptions = computed(() => {
9697
callbacks: {
9798
label: (item) => {
9899
let unit = CHART_UNITS[item.datasetIndex]
99-
return `${item.formattedValue} ${unit}`
100+
return formatSI(item.parsed.y, unit, 2)
100101
},
101102
},
102103
usePointStyle: true,

frontend/src/utils/commonCharts.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,30 @@ export function resampleTimedData(data, dtKey, unitCount, unit, reduceFn, addEmp
110110
// Ensure correct order of buckets
111111
return result.sort((a, b) => a[dtKey] - b[dtKey])
112112
}
113+
114+
/**
115+
* Formats a number using SI prefixes (k, M, G, ...)
116+
*
117+
* Values < 1e3 are not formatted.
118+
*
119+
* @param {Number} value Value to format
120+
* @param {String} unit Unit to append to the formatted value (e.g. 'B', 'Hz')
121+
* @param {Number} decimalPlaces Number of decimal places to show (only for values
122+
* >= 1e3). Default is 1.
123+
* @returns {String} Formatted string
124+
*/
125+
export function formatSI(value, unit = '', decimalPlaces = 1) {
126+
const absValue = Math.abs(value)
127+
if (absValue < 1e3) return `${value} ${unit}`
128+
129+
const units = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
130+
let i = 0
131+
let formattedValue = value
132+
133+
while (i < units.length && Math.abs(formattedValue) >= 1e3) {
134+
formattedValue /= 1e3
135+
i++
136+
}
137+
138+
return `${formattedValue.toFixed(decimalPlaces)} ${units[i]}${unit}`
139+
}

0 commit comments

Comments
 (0)