I have a scatter chart (using apexchart) and I am trying to prepare my data (objects in an array) before adding it to the chart.
My problem is that I often have some data that can have the exact same values on the x and y axis. This means that they will overlap eachother and only one item will be shown.
I first added a simple solution by using Math.random() to each duplicated value, but this means that they will be repositioning slightly for each time the chart is updated, which will make the users confused.
So what I want to achieve is to add the same "jitter"/changes for each items all the time. So my thought is to do it like this:
//1. The first three duplicates should add 0.05 to the x value. So it will say:
item1.x = 10 //original value
item2.x = 10.05
item3.x = 10.1
item4.x = 10.15
//2. Then, If more duplicates than 3, the next 3 should reduce 0.05 of the original value:
item5.x = 9.95
item6.x = 9.90
item7.x = 9.85
//If more than 7 items with the same value, then just use the original value.
How could be the best way to achieve this? Right now I have the duplicated values and I can add "jitter", but how can I make it so it keeps count and do as I want above?
I managed to filter out the duplicates, but then I got stuck:
const duplicates = AllItems.filter((val, i, self) => self.findIndex(item => item.x == val.x && item.y == val.y) != i);
Any ideas?