Using .on('mouseover', d => ..) does not yield the same `d` value as using .attr('foo', d => ..) in D3.js

I'm facing an issue with a mouseover tooltip in Observable that seems to fail when I transfer it to a Grafana plugin using React, D3, and Typescript.

The technique I followed can be found in this article: Link to Article

To simplify the code and debug, I have stripped it down to this basic structure with comprehensive console.log statements:

      const mouseGroup = chart
        .append('g')
        .attr('fill', 'none')
        .attr('pointer-events', 'all');
      const foobar = mouseGroup
        .selectAll('rect')
        .data<[number, number]>(plainArray)
        .join('rect')
        .attr('x', d => {
          console.log('d in x', d);
          console.log('xScale', xScale(d[0]));
          return xScale(d[0]);
        })
        .attr('y', d => yScale(d[1]))
        .attr('height', height)
        .attr('width', '5') // Hard-coded during debugging.
        .on('mouseover', (d, i) => {
          console.log(d, i);
          console.log(plainArray);
          tooltip.show(d);
        })
        .on('mouseout', () => tooltip.hide());

When hovering over a rect, I expect to see the d data displayed like [123, 456], but instead, it only shows a small integer. The first rect at index 0 has a d value of 4, which is not the expected output.

Despite some type errors, the main issue appears to lie within the console.log(d, i) statement as showcased here:

https://i.sstatic.net/NsEMt.png

In an attempt to understand why .attr() displays the correct d data while .on() provides a different simplistic integer, I introduced additional logging for .attr('x', d => ... revealing the expected d data:

https://i.sstatic.net/X9tGY.png

The constant values of d in .on('mouseover', (d, i) => { remain consistent and unexpected:

i=0, d=4
i=1, d=8
i=2, d=15
i=3, d=16
i=4, d=23
i=5, d=42

The functionality works fine in Observable, leading me to question if Grafana could potentially interfere with the mouseover and d parameters?

A simple test where I changed the rect fill color to "yellow" upon mouseover worked correctly, proving the execution of my mouseover function. Additionally, all console.log statements trigger as intended when hovering over each rect.

An intriguing observation is that although plainArray contains 312 elements, I can only view the first six when inspecting the DOM. Surprisingly, numerous console.log outputs from .attr('x', ...) are generated.

The contents of plainArray are detailed below:

https://i.sstatic.net/wygJU.png

Answer №1

Oh no, can't believe I missed that! :facepalm:

It finally clicked for me when I typed out

but I'm only seeing the first six elements in the DOM
, realizing that another part of my code was messing with those elements. Sure enough, further down I spotted more code targeting rect elements.

const values = [7, 13, 24, 28, 35, 49];
...
         const bars = graph
           .selectAll('rect')
           .data(values)
...

Once I got rid of the extra code targeting rect, everything fell into place like it should have.

Lesson learned: never forget to clean up old code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Oops! An issue occurred: The value 'NgxMatDrpModule' was not expected in the specified file path: node_modules/ngx-mat-daterange-picker/ngx-mat-daterange-picker.d.ts

Encountered an error while building an Angular 7 app using the command ng build --configuration=dev. The exception shows a single quote prefixed to NgxMatDrpModule. Even after deleting node_modules, package-lock.json and reinstalling node modules, the issu ...

Navigating the world of Typescript: mastering union types and handling diverse attributes

I am currently working on building a function that can accept two different types of input. type InputA = { name: string content: string color: string } type InputB = { name: string content: number } type Input = InputA | InputB As I try to impleme ...

Using NgModel with a custom element

I am currently using a basic component within my form as shown below: <app-slider [min]="field.min" [max]="field.max" [value]="field.min"></app-slider> This component consists of the following code: HTML: <input #mySlider class="s ...

Dealing with typescript error due to snakecase attributes being sent from the database while the frontend only accepts pascalcase attributes

I am facing a challenge regarding converting snake case values from my api to pascal case attributes in the front end. Here is the scenario: Frontend Request Axios request fetching multiple user data, for example: axios.get('/users') API Resp ...

Beginner: Add "shared" module elements to app.module and include them in app.component.html as part of the app's layout

I am trying to incorporate three components from a "shared" module into app.component.html. Here is my current setup: <header></header> <div class="main-wrapper"> <div class="bg-trick"></div> &l ...

The implementation of TypeScript 3.5 resulted in a malfunction where the imported namespace was unable to locate the Enum during runtime

I recently upgraded an older Angular.js application from Typescript 2.7 to 3.5 and successfully compiled it using tsc.exe. During application runtime, I encountered an error message in certain parts of the code: TypeError: Cannot read property 'Enu ...

acquiring the main class instance within a function without relying on an arrow function

Within my Angular project, I have integrated a datatable with row grouping and row callbacks. Datatable Options openPositionDatatableOptions = { sDom: 'rt<"bottom"p>', ajax: (data, callback, settings) => { this.service.ge ...

What is the best approach for injecting services (local and API) in Angular 13 based on different environments (local and QA)?

api-local.service.ts import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { LoginRequest } from '../login/login-request'; @Injectable({ providedIn: 'root&ap ...

What is the best way to create a Typescript type consisting of only the public members of a different type?

Inside the realm of Typescript 4.3.5 In what manner can I establish a type that consists solely of the public members and properties of another type? Take into account: class Thing { public name: string private secret: string public greet(): string ...

Building NextJS with Typescript encountered an error while using @auth0/nextjs-auth0

I am currently facing an issue while trying to build my NextJS application using Typescript. The problem lies with the auth0/nextjs-auth0 package, causing persistent errors during installation. One specific error can be found at the following link: Shari ...

Unable to establish a connection to 'X' as it is not recognized as a valid property

Trying to implement a Tinder-like swiping feature in my Angular project, but encountering an error stating that the property parentSubject is not recognized within my card component. Despite using the @Input() annotation for the property, it still fails to ...

Attempting to successfully upload this Angular 7 form to my TypeScript code. Making use of ngForm and [(ngModel)] to achieve this

I am having trouble passing form information using the onSubmit() function. It seems to be undefined when I try to execute it initially. Could there be a syntax error that I'm missing? <form class="gf-formbox" name="credentials" (ngSubmit)="onSubm ...

Is there a way to subscribe to various observables simultaneously in Angular 2, and then pause until fresh data is available on each of them?

I have an Angular component that relies on 3 services, each of which has an observer I can subscribe to. The view of the component needs to be updated whenever there are changes in the observed data, which occurs through websockets (feathers.js). I want th ...

An error occurs in TypeScript when attempting to reduce a loop on an array

My array consists of objects structured like this type AnyType = { name: 'A' | 'B' | 'C'; isAny:boolean; }; const myArray :AnyType[] =[ {name:'A',isAny:true}, {name:'B',isAny:false}, ] I am trying ...

Trouble encountered when attempting to call a function within another function in StencilJS

I am currently following a tutorial on building a drag and drop file uploader using StencilJS for some practice and fun. However, I have encountered an error in the code. Below is a snippet of the code, but I can provide more if necessary. @Component({ ...

What causes an inference site to have varying effects when accessed directly versus when it is retrieved from a function?

Below is the provided code snippet : declare class BaseClass<TValue = any> { value: TValue; foo(value: TValue): void; } type Wrapped<T> = { value: T } declare class ConcreteClasss<TValue> extends BaseClass<TValue> { construc ...

Using InjectionToken within an Angular APP_INITIALIZER function

I am currently working on implementing an APP_INITIALIZER in my Angular 10 project. The factory function I'm using has multiple dependencies, one of which is an InjectionToken<string>. However, I have encountered an issue when trying to inject i ...

Can someone please point me in the right direction to locate a Typescript project within Visual Studio

I've been struggling with this issue for days and still can't find a solution. After installing the Typescript tool for Visual Studio 2015, it appears to be successfully installed. https://i.stack.imgur.com/nlcyC.jpg However, I am unable to loc ...

Why isn't the customer's name a part of the CFCustomerDetails class?

Currently, I am utilizing the cashfree-pg-sdk-nodejs SDK to integrate Cashfree payment gateway into my application. Upon examining their source code, I noticed that the CFCustomerDetails class does not include the customerName attribute. https://i.stack.i ...

Issue with Angular 8: discrepancy between the value utilized in component.html and the value stored in component.ts (Azure application service)

Encountering a peculiar behavior in one of my Angular applications. In the component.html file, I aim to display "UAT" and style the Angular mat elements with a vibrant orange color when in UAT mode, while displaying them in blue without any mention of UAT ...