diff --git a/x-pack/plugins/profiling/common/topn.ts b/x-pack/plugins/profiling/common/topn.ts index 65ea193d85e02..bce287cf281f1 100644 --- a/x-pack/plugins/profiling/common/topn.ts +++ b/x-pack/plugins/profiling/common/topn.ts @@ -255,10 +255,22 @@ export function groupSamplesByCategory({ rotations: Math.ceil(subcharts.length / 10), }); + // We want the mapping from the category string to the color to be constant, + // so that the same category string will always map to the same color. + const stringhash = (s: string): number => { + let hash: number = 0; + for (let i = 0; i < s.length; i++) { + const ch = s.charCodeAt(i); + hash = (hash << 5) - hash + ch; // eslint-disable-line no-bitwise + hash &= hash; // eslint-disable-line no-bitwise + } + return hash % subcharts.length; + }; + return orderBy(subcharts, ['Percentage', 'Category'], ['desc', 'asc']).map((chart, index) => { return { ...chart, - Color: colors[index], + Color: colors[stringhash(chart.Category)], Index: index + 1, Series: chart.Series.map((value) => { return {