Difficulty in displaying a set of information through JavaScript

Greetings,

I have created a tooltip that can be found at this link: http://jsfiddle.net/QBDGB/83/. In this tooltip, I have defined two "functions" like so:

function mousemove1(d) {
    div
      .text("data 1: " + d.value + " at " + formatTime(new Date(d.time)))
      .style("left", (d3.event.pageX) + "px")
      .style("top", (d3.event.pageY) + "px");
}

function mousemove2(d) {
    div
      .text("data2: " + d.value + " at " + formatTime(new Date(d.time)))
      .style("left", (d3.event.pageX) + "px")
      .style("top", (d3.event.pageY) + "px");
}

However, I am encountering an issue where the tooltip only displays the value of "data2" when I move the cursor. I would like the tooltip to recognize both series of data and display their respective values when hovering over the two curves of the graph.

Answer №1

1. Each function populates the div with new data, with data2 overwriting data1. Take a look at this fiddle to address this issue. Pay attention to the json variable added at line 123.

2. Furthermore, your .svg images are overlapping. Currently, only svg2 data is displayed because it does not recognize that you are also interacting with svg1.

To verify this, when hovering over a green or dark green area, only data2 is shown. However, when hovering over a blue area, both data1 and data2 are visible (as demonstrated in the provided link).

Once you hover over a blue area, the svg1 data remains unchanged when navigating to a green area until another blue area is interacted with.

Answer №2

When you bind two handlers to the mousemove event, the last one will overwrite the previous one.

It might be a good idea to pass both parameters at the same time to any of the handlers.

Here is an example of how you can do that:

function mousemove1(d1, d2) {
div
  .text("data 1: " + d1.value + " at " + formatTime(new Date(d1.time)) +"  "+ "data 2: " + d2.value + " at " + formatTime(new Date(d2.time)))
  .style("left", (d3.event.pageX) + "px")
  .style("top", (d3.event.pageY) + "px");
}

If you only need to pass one datalist at a time, you can use a switch statement like this:

function mousemove(d, caseNum) {
    switch(caseNum) {
    case1: // do your work of first case
        break;
    case2:  // do your work of second case
       break;
    case3:  // do your work of third case
       break;
    }
}

In the second method, you will need to pass the caseNum parameter to indicate the case number.

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

Upon refreshing the page, an inline script was not executed due to its violation of the Content Security Policy directive: "script-src 'self'"

When I refresh the page on my production build for all routes except "/", I encounter an error in my Node/React app which results in a blank page being displayed. The error message states: "Refused to execute inline script because it violates the followi ...

Utilizing Local Storage in Vuex Store with Vue.js

I have been working with localStorage for storing and retrieving items in my JavaScript code housed within a .vue file. However, I am now looking to find a way to transfer this stored data into my Vuex store, specifically within the mutations section locat ...

Receiving encoded characters in the response

URL: I have encountered an issue where I am trying to retrieve the PDF file from the URL above using code. In tools like Postman or Insomnia, I am able to see the output as expected in PDF format. However, when I attempt it with code, I am receiving rando ...

What is the best way to utilize Typescript when making queries to Firebase?

I have successfully integrated push notifications for my app using Firebase Cloud Functions. Now, I am looking to enhance the user experience by updating the app's badge count alongside the push notifications. I understand that this can only be achiev ...

Prevent Float Filtering in AngularJS

As I work on creating a directive in AngularJs, I am facing an issue. The directive is supposed to receive a JSON object from the app controller and display its content accordingly. To better illustrate my problem, I have created a simple demo which can b ...

Dynamically load the configuration for a JQuery plugin

Currently, I am utilizing a JQuery plugin from this source. My goal is to dynamically load the configuration of the plugin without directly modifying it within the plugin file. Below are the default options provided by the plugin: $.Slitslider.def ...

A guide on integrating the MUI Timepicker with the newest 6 version using Formik

I am currently experimenting with the MUI React Timepicker component and facing an issue during integration with Formik for validation. The problem lies in the inability to bind values properly in the initialValues of the form. const [initialValues, setI ...

Generate JSON dynamically using a foreach loop

I am utilizing the jquery flot charts library to visualize my data. Take a look at this example JSFiddle I created demonstrating how the JSON structure required for the chart should be. The data I am working with is sourced from a MySql stored procedure a ...

PapaParse is not properly recognizing the date format

Having trouble creating a chart using JSON data from papaparse, resulting in the following error: c3.js:2100 Failed to parse x '10-18-2018' to Date object I've tried adjusting the date format in the CSV file but haven't had any luck. I ...

Tips for creating Selenium code to verify the asterisk symbol

Looking to create Selenium code for validating the presence of asterisks with mandatory fields such as First Name*, Last Name*, Enter Address*, and Enter Phone Number*. The validation needs to confirm the asterisk appears after each field name. Currently ...

When using the map function, I am receiving an empty item instead of the intended item based on a condition

Need assistance with my Reducer in ngRx. I am trying to create a single item from an item matching an if condition, but only getting an empty item. Can someone please help me out? This is the code for the Reducer: on(rawSignalsActions.changeRangeSchema, ...

How can I merge these two Observables in Angular to create an array of objects?

Let's say we are working with two different datasets: student$ = from([ {id: 1, name: "Alex"}, {id: 2, name: "Marry"}, ]) address$ = from([ {id: 1, location: "Chicago", sid: 1}, {id: 2, location: &qu ...

Having difficulty positioning the Divider correctly among Grid items

I am trying to add a Divider between each item in my Grid. The desired output should resemble this:- Col 1 | Col 2 | Col 3 However, I am facing difficulties as the Divider is not visible and the items are displayed vertically. import "./style ...

Using PHP and JavaScript to enhance functionality around a hyperlink

I am trying to incorporate a small piece of Javascript on my website to ensure user confirmation. While I have successfully used the 'onclick' function with buttons, I am facing difficulty implementing it on a link. Here is the link in question: ...

Creating routes in Node.js after setting up middleware

Currently tackling a project using node.js and encountering a specific issue. After setting up all routes with express (app.get("..", func)), I find myself stuck with a middleware that catches all requests and redirects to a 404-page. The problem arises w ...

Trigger a click event to alter the child div elements

I have a <div> that expands when clicked and shrinks back when clicked again. Inside this div are images that should only appear once the div expands. My issue is ensuring that all images except the first one are hidden until the div expands. https: ...

What is the method for invoking an anonymous JavaScript object when its name is unknown?

I've been dynamically loading JavaScript modules with jQuery's getScript function. I want to be able to call an init method that all of these modules contain. Since the modules are loaded dynamically, I would rather not hard code their names. Is ...

Creating dynamic styles with Material-UI's useStyles

Attempting to implement the same logic using material-ui's useStyle feature <div className={'container ' + (state.unlocked ? 'containerUnlocked' : '')}> I thought it might look like this: <div className={`${clas ...

Determining special characters within a string using VueJS

As a newcomer to VueJS, I am currently developing an application that requires a signup and login system. My goal is to inform the user if they have entered a special character such as /[&^$*_+~.()\'\"!\-:@]/. In order to achieve th ...

Having trouble importing Bootstrap into Next.js? It seems like the issue may be related to the

I am currently facing an issue with importing bootstrap 5.3.2 (not react-bootstrap) into my NextJS 14.1.0 project that utilizes the new App Router. My goal is to strategically utilize individual Bootstrap components (not through data-attrs). I managed to ...