Skip to content

Commit

Permalink
[profiling] Ensure constant colors in profiling bar chart (elastic#14…
Browse files Browse the repository at this point in the history
…6696)

## Summary

Make sure that identical objects in the bar chart always get assigned
the same colors.

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Tim Rühsen <tim.ruhsen@elastic.co>
  • Loading branch information
3 people authored Dec 1, 2022
1 parent e7da574 commit 0a065f3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion x-pack/plugins/profiling/common/topn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0a065f3

Please sign in to comment.