Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Husain
Advocate I
Advocate I

d3.mouse

Hi

I am trying to get X,Y coordinates of mouse position using d3.mouse function

 

 

 

export class Visual implements IVisual {
    private host: IVisualHost;
    private svg: Selection<SVGElement>;
    private container: Selection<SVGElement>;

  constructor(options: VisualConstructorOptions) {     
        this.svg = d3.select(options.element)
            .append('svg')
        this.container = this.svg.append("g")
            .classed('container', true)  


this.container
        .on("mousemove", function () {
            d3.select(".tooltip").remove()
                var mouse = d3.mouse(this.container)
//I get following error with d3.mouse(parameter). I tried d3.mouse(this) as well
//Argument of type 'SVGElement' is not assignable to parameter of type 'ContainerElement'.
  //Type 'SVGElement' is missing the following properties from type 'SVGGElement': transform, //getBBox, getCTM, getScreenCTM, and 2 more.

 

 

 

 

1 ACCEPTED SOLUTION
dm-p
Super User
Super User

Hi @Husain,

This is where the problem starts:

private container: Selection<SVGElement>;

In your constructor, you're correctly assigning the 'g' element, but TypeScript thinks it's a SVGElement due to your property declaration and gets upset when you try to call d3.mouse on it. This needs to be declared as a ContainerElement.

To make the code in your post work to that point, change the container declaration as follows:

private container: Selection<ContainerElement>;

Then, your d3.mouse function can work on this, which is scoped as the element you're calling the event function on. This line should now read as follows:

.on("mousemove", function () {
    d3.select(".tooltip").remove()
        var mouse = d3.mouse(this)

 Your partial listing would now look as follows if you just want to copy/paste the whole thing:

export class Visual implements IVisual {
    private host: IVisualHost;
    private svg: Selection<SVGElement>;
    private container: Selection<ContainerElement>; /** Now has correct type */

  constructor(options: VisualConstructorOptions) {     
        this.svg = d3.select(options.element)
            .append('svg')
        this.container = this.svg.append("g")
            .classed('container', true)  


this.container
        .on("mousemove", function () {
            d3.select(".tooltip").remove()
                var mouse = d3.mouse(this) /** `this` is declared as the element (this.container) in the listener function */

This block of code will now compile correctly.

Good luck!

Daniel


If my post solves your challenge, then please consider accepting as a solution to help other forum members find the answer more quickly 🙂

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




View solution in original post

1 REPLY 1
dm-p
Super User
Super User

Hi @Husain,

This is where the problem starts:

private container: Selection<SVGElement>;

In your constructor, you're correctly assigning the 'g' element, but TypeScript thinks it's a SVGElement due to your property declaration and gets upset when you try to call d3.mouse on it. This needs to be declared as a ContainerElement.

To make the code in your post work to that point, change the container declaration as follows:

private container: Selection<ContainerElement>;

Then, your d3.mouse function can work on this, which is scoped as the element you're calling the event function on. This line should now read as follows:

.on("mousemove", function () {
    d3.select(".tooltip").remove()
        var mouse = d3.mouse(this)

 Your partial listing would now look as follows if you just want to copy/paste the whole thing:

export class Visual implements IVisual {
    private host: IVisualHost;
    private svg: Selection<SVGElement>;
    private container: Selection<ContainerElement>; /** Now has correct type */

  constructor(options: VisualConstructorOptions) {     
        this.svg = d3.select(options.element)
            .append('svg')
        this.container = this.svg.append("g")
            .classed('container', true)  


this.container
        .on("mousemove", function () {
            d3.select(".tooltip").remove()
                var mouse = d3.mouse(this) /** `this` is declared as the element (this.container) in the listener function */

This block of code will now compile correctly.

Good luck!

Daniel


If my post solves your challenge, then please consider accepting as a solution to help other forum members find the answer more quickly 🙂

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!


On how to ask a technical question, if you really want an answer (courtesy of SQLBI)




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (6,564)