I'm diving into the world of Typescript and trying to figure out how to use tooltips for my d3 stacked bar chart. Any guidance on implementing mouseover effects in Typescript would be greatly

I am currently facing some issues with the code below and need guidance on how to proceed. I am new to this and unsure of how to call createtooltip. Any assistance would be greatly appreciated.

The error message states that createtooltip is declared but never called.

 private initSvg() {
    this.createTooltip = d3.select('body').append("div")
    .classed('chart-tooltip', true)
    .style('display', 'none');
    let element = this.chartContainer.nativeElement;
    this.svg = d3.select(element)
    .append('svg')
    .attr("preserveAspectRatio", "xMinYMin meet")
    .attr('class', 'chart')
    .attr('width', this.w)
    .attr('height', this.h)
    .attr("viewBox", "0 0 800 600");
    this.chart = this.svg.append('g')
    .classed('chart-contents', true)
    .attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")");
    this.layersBarArea = this.chart.append('g')
    .classed('layers', true);
}
 private drawAxis(){
    this.xAxis = this.chart.append('g')
    .classed('x axis', true)
    .attr("transform", "translate(0," + this.height + ")")
    .call(d3.axisBottom(this.x).ticks(7))
    .selectAll("text")
    .style("text-anchor", "end")
    .attr("dx", "-.8em")
    .attr("dy", ".15em")
    .attr("transform", "rotate(-65)" );
    this.chart.append("text")
    .attr("y", this.height + 40)
    .attr("x", (this.width/2))
    .classed('axis-title', true)
    .style("text-anchor", "end")
    .style('stroke', 'none');
    this.yAxis = this.chart.append('g')
    .classed('y axis', true)
    .call(d3.axisLeft(this.y).ticks(7));
    this.chart.append("text")
    .attr("transform", "rotate(-90)")
    .attr("y", 0 - 60)
    .attr("x", 0 - (this.height / 2))
    .style("text-anchor", "middle")
    .style('stroke', 'none')
    .classed('axis-title', true);
 }
 private updateChart(stackData:any){
    console.log("Update Chart");
    this.stackedSeries = this.stack(stackData);
    console.log("Look Here", this.stackedSeries);
    this.chart.selectAll(".layers").remove();
    var new_layer = this.chart.append('g')
    .classed('layers', true);
    this.x.domain(this.data.cpmans.map((d:any)=>{return d.label;}));
    this.y.domain([0, +d3.max(this.stackedSeries, function(d:any){return d3.max(d, (d:any)=>{return d[1];})})]);
    this.xAxis.transition().call(d3.axisBottom(this.x));
    this.yAxis.transition().call(d3.axisLeft(this.y));

    this.layersBar = new_layer.selectAll('.layer')
    .data(this.stackedSeries)
    .enter()
    .append('g')
    .classed('layer', true)
    .style('fill', (d:any,i:any)=>{return this.colors[i];});
    this.x.domain(this.data.cpmans.map((d:any)=>{return d.label;}));
    this.y.domain([0, +d3.max(this.stackedSeries, function(d:any){return d3.max(d, (d:any)=>{return d[1];})})]);
    this.layersBar.selectAll('rect')
    .data((d:any)=>{return d;})
    .enter()
    .append('rect')
    .attr('y', (d:any)=>{return this.y(d[1]);})
    .attr('x', (d:any, i:any)=>{return this.x(d.data.label);})
    .attr('width', this.x.bandwidth()/2)
    .attr('height', (d:any, i:any)=>{ return this.y(d[0]) - this.y(d[1]);})
    .on("mouseover", function() { this.tooltip.style("display", null); })
    .on("mouseout", function() { this.tooltip.style("display", "none"); })
    .on("mousemove", function(d) {
    var xPosition = d3.mouse(this)[0] - 15;
    var yPosition = d3.mouse(this)[1] - 25;
    this.tooltip.attr("transform", "translate(" + xPosition + "," + yPosition + ")");
    this.tooltip.select("text").text(d.y);
        });

 }
 private createTooltip(){
     this.tooltip = this.svg.append("g")
    .attr("class", "tooltip")
    .style("display", "none");
    this.tooltip.append("rect")
    .attr("width", 30)
    .attr("height", 20)
    .attr("fill", "white")
    .style("opacity", 0.5);
     this.tooltip.append("text")
    .attr("x", 15)
    .attr("dy", "1.2em")
    .style("text-anchor", "middle")
    .attr("font-size", "12px")
    .attr("font-weight", "bold");
 }

Answer №1

this.createTooltip is a method belonging to your class, but when you use it in initSvg(), it gets replaced by a d3 selection. As a result, you are unable to invoke the function, which causes an 'error' message to appear in your editor. Many individuals are eager to assist you, so try to simplify your question for better understanding. For those looking for a basic example of bar charts using D3, consider checking out the suggestion by @Arcteezy.

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

PageObjectModel Playwright, execute the locator().all() function - 'The execution context has been terminated, possibly due to navigating to another...'

Hey there, I'm currently working on a basic test using POM. Here is a snippet from one of my PageObjects Class: import { Expect, Page, Locator } from "@playwright/test"; export class InventoryPage { readonly page: Page; readonly addToC ...

Configuring the tsconfig outDir will specify where the output files will be stored

What am I missing in the tsconfig settings if I only want to output files in the root directory? If I set it as "rootDir":"src" "outDir":"build", or "rootDir":"src" "outDir":"&q ...

Error encountered: "The requested resource does not have the 'Access-Control-Allow-Origin' header in Angular 6 and Nodejs."

I have encountered an issue with my Angular 6 app and Node.js. When I submit the form, I am receiving the following error: Failed to load http://localhost:3000/contact/send: Response to preflight request doesn't pass access control check: No 'Ac ...

Incorporating an external module into your Angular application for local use

I currently have two projects: my-components, which is an Angular library, and my-showcase, an Angular app. Whenever I make changes or add a new component to my library, I commit it to a private git repository and publish it to a private npm registry. This ...

Unable to use global modules in NestJS without importing them

Currently, I am in the process of integrating a global module into my nest.js project I have written a service as shown below: export interface ConfigData { DB_NAME: string; } @Injectable() export class ConfigManager { private static _inst ...

Tips for activating AG Grid Column Auto Sizing on your website

The Issue I am experiencing difficulty in getting columns to expand to the size of their content upon grid rendering. Despite following the guidance provided in the official documentation example, and consulting sources such as Stack Overflow, I have att ...

What is the best way to interrupt the current song playing?

I am currently working on developing an audio player using reactjs that has a design similar to this https://i.sstatic.net/Hnw0C.png. The song boxes are rendered within a map function, and when any song box is clicked, it should start playing. However, I a ...

What is preventing me from iterating through a dictionary or an array of keys?

After trying to log the dictionary using console.log(JSON.stringify(this.idTitleDict)) as suggested by @Kobe, I noticed that it was showing empty curly braces. All the code related to this dictionary (including its declaration and population) can be found ...

Firebase and Nx: Encountering issues with running emulators

I've been attempting to launch the Firebase emulators within the Nx workspace. Initially, I added firebase to my project: npm install firebase @angular/fire --save nx g @angular/fire:ng-add // configures the package in the project (unsuccessful) ng ...

Error encountered: The configuration for "@typescript-eslint/parser" specifies the project option, which does not align with the project settings in the .eslintrc.js file

After using create-react-app to kickstart a fresh typescript react application and then integrating firebase, I proceeded to run firebase init. During this process, I opted for the typescript selection, enabled es lint, and also activated functions. Upon ...

suggestions for customizing Angular Material

The guidelines regarding materials specify that: "For any Angular Material component, you are allowed to define custom CSS for the component's host element that impacts its positioning or layout, such as margin, position, top, left, transform, and z- ...

The function is trying to access a property that has not been defined, resulting in

Here is a sample code that illustrates the concept I'm working on. Click here to run this code. An error occurred: "Cannot read property 'myValue' of undefined" class Foo { myValue = 'test123'; boo: Boo; constructor(b ...

A combination of MVC6, tsd, and typings has proven to be

Given that TSD has been officially deprecated, I am looking towards the future and seeking guidance on how to use typings in the MVC6 framework. Any advice or tips would be greatly appreciated. I attempted following a tutorial from the typings website, wh ...

What could be causing my website to lose its responsiveness after linking a domain?

Recently, I created a basic website for an event in my town using AWS Amplify from Amazon. Initially, the website was hosted without a custom domain and had a random URL. It worked well on both web and mobile platforms. However, after connecting a custom d ...

"Looking to incorporate dynamic Carousel Indicators into your Angular2 project? Here's how you can

After moving to just one slide, the "next" and "prev" buttons stop working. Additionally, clicking on the indicators to move slides is also ineffective. Interestingly, when I remove the div with the class carousel-indicators, the "next" and "prev" buttons ...

"Utilize TypeScript to create a function that can handle either a single value or

I am attempting to develop a double function with the following structure: type MaybeArray<T> = T | T[]; function double<T extends MaybeArray<number>>(data: T): T extends number[] ? number[] : number { if (Array.isArray(data)) { / ...

What is the method for extending props from React.HTMLProps<HTMLButtonElement>?

"react": "^17.0.2", "typescript": "^4.2.4" Can someone help me understand how to extend props from React.HTMLProps? import { FC, HTMLProps } from 'react' export interface SliderButtonProps extends HTMLPro ...

Tips for styling a date while iterating through a dynamic object in Angular

When looping through a data response from an API and displaying it in Angular, I have encountered a scenario where some fields are in date format. Is there a way to check the object and format the date to 'd-MMM-yyyy'? <table class="view- ...

Launching a Node.js application on a Kubernetes cluster

Currently, I have a local Angular 6 application that I run using "npm start." My next goal is to deploy this application in Kubernetes. However, I'm unsure about how to dockerize an Angular 6 based application and run it in Kubernetes. Any assistance ...

mentioning a JSON key that includes a period

How can I reference a specific field from the JSON data in Angular? { "elements": [ { "LCSSEASON.IDA2A2": "351453", "LCSSEASON.BRANCHIDITERATIONINFO": "335697" }, { "LCSSEASON.IDA2A2": "353995", "LCSSEASON.BRANCHIDITER ...