Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collision #72

Open
EmilMoe opened this issue Jul 5, 2018 · 7 comments
Open

Collision #72

EmilMoe opened this issue Jul 5, 2018 · 7 comments

Comments

@EmilMoe
Copy link

EmilMoe commented Jul 5, 2018

I am using datalabels on my bubble chart, but sometimes the bubbles and datalabels collide. Is there any solution to fix collision or detect when they are overlapping?

I can see that in the path chart > $datalabels > labels > [1] > [..n] > Label > _hitbox > _rect I have this

cx: 434.368
cy: 74.09599999999998
x0: 388.0193671875
x1: 481.7166328125
y0: 58.69599999999998
y1: 90.49599999999998

Should I check if there's something overlapping there?

update

I can see a problem with this approach as the _rect isn't updated until there has been a mouse over.

@simonbrunel
Copy link
Member

Isn't it the same issue as #49 and #55 but for bubble charts? If not, can you provide a jsfiddle that showcases your issue. Anyway, $datalabels and everything prefixed by _ is private and should not be accessed in production code since it could break without notice in upcoming minor/patch releases.

@EmilMoe
Copy link
Author

EmilMoe commented Jul 5, 2018

Thanks. Looks like same issues but I see no solutions though. Will try to find the properties from a variable without _ in front, but I don't know if that's possible. Also I need to figure out how to update the labels location.

Will be happy to share a jsfiddle later, but for now it's integrated in a vue component so a lot of extra code around it.

@EmilMoe
Copy link
Author

EmilMoe commented Jul 6, 2018

I have been looking and seems to be nearly no public API then I can access?

This is how far I am in the code

detectCollisions(context) {
                if (this.hasCheckedForCollisions)
                    return

                let rectangles = []

                context.chart.$datalabels.labels[0].forEach(label => {
                    if (label._hitbox._rect) {
                        rectangles.push(this.getBoundaries(label._hitbox._rect, label.$context.dataset.data[label.$context.dataIndex].r))
                        this.hasCheckedForCollisions = true
                    }
                })

                if (this.hasCheckedForCollisions)
                    this.checkAllRectangles(rectangles)
            },
            getBoundaries(rectangle, importance) {
                return {
                    x: rectangle.x0,
                    y: rectangle.y0,
                    w: rectangle.x1 - rectangle.x0,
                    h: rectangle.y1 - rectangle.y0,
                    i: importance,
                }
            },
            checkAllRectangles(rectangles) {
                rectangles.forEach((r1, i1) => {
                    if (i1 === rectangles.length - 1)
                        return

                    rectangles.forEach((r2, i2) => {
                        if (this.isEqualRectangles(r1, r2))
                            return

                    if (this.isColliding(r1, r2))
                        this.handleCollision(r1, r2)
                    })
                })
            },
            isColliding(rect1, rect2) {
                if (rect1.x < rect2.x + rect2.w &&
                    rect1.x + rect1.w > rect2.x &&
                    rect1.y < rect2.y + rect2.h &&
                    rect1.h + rect1.y > rect2.y)
                    return true

                return false
            },
            isEqualRectangles(rect1, rect2) {
                return rect1.x === rect2.x && rect1.y === rect2.y && rect1.h === rect2.h && rect1.w === rect2.w
            },
            handleCollision(rect1, rect2) {
                console.log(rect1)
                rect1.y = rect1.h + rect2.y
                console.log(rect1)
            }

@simonbrunel
Copy link
Member

I have been looking and seems to be nearly no public API then I can access?

No because the plugin is still in beta and I don't want to commit on implementation details. I'm currently rewriting the whole layouting part and moved the _hitbox outside the label so your previous implementation will break at the next release.

Anyway, I'm currently looking to introduce a new option to prevent labels to overlap and I'm wondering what is your expected behavior when 2 labels overlap?

@krosskawasaki
Copy link

I have been looking and seems to be nearly no public API then I can access?

No because the plugin is still in beta and I don't want to commit on implementation details. I'm currently rewriting the whole layouting part and moved the _hitbox outside the label so your previous implementation will break at the next release.

Anyway, I'm currently looking to introduce a new option to prevent labels to overlap and I'm wondering what is your expected behavior when 2 labels overlap?

A good solution is what Amcharts does...
image

@naftis
Copy link

naftis commented May 13, 2022

I'm having the same issue and while I find generic or D3 solutions (here for example), they are difficult to implement for Chart.js as the algorithms aren't simple.

I'm using a bubble chart and it's becoming an increasingly worse issue while the amount of our data is getting bigger. Do we still have any kind of solution for this?

image

@EmilMoe
Copy link
Author

EmilMoe commented May 13, 2022

I'm having the same issue and while I find generic or D3 solutions (here for example), they are difficult to implement for Chart.js as the algorithms aren't simple.

I'm using a bubble chart and it's becoming an increasingly worse issue while the amount of our data is getting bigger. Do we still have any kind of solution for this?

image

I switched to apex charts as they are more modern, maybe it's fixed the

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants