Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
d3 events issue with angular 2 typescript

I am trying to include zooming and panning functionality in d3. which works in javascript but giving error in typescript. d3.event.translate and d3.event.scale are not working in angular2 typescript

this.svg = this.host.append('svg')
        .attr('width', this.width)
        .attr('height', this.height)
        .style({ 'float': 'right' })
        .attr("pointer-events", "all")
        .call(d3.behavior.zoom().on("zoom", redraw))
        .append('svg:g');

        function redraw() {
        this.svg.attr("transform",
            "translate(" + d3.event.translate + ")"
            + " scale(" + d3.event.scale + ")");
        }

show this error on console.

   Property 'translate' does not exist on type 'Event | BaseEvent'. Property 'scale' does not exist on type 'Event | BaseEvent'.
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

@mkaran's answer works, but rather defeats the purpose of typescript. The proper cast here is:

function redraw() {

  let e = (<d3.ZoomEvent> d3.event);

  this.svg.attr("transform",
    "translate(" + e.translate + ")"
    + " scale(" + e.scale + ")");
}

Since the purpose the TypeScript is types, resorting to any is considered bad practice*.

You should also attempt to avoid in-line functions, and rather use proper methods of your class. But that's a question for another day...

*sometimes it's just easier :)

over 4 years ago · Santiago Trujillo Report

0

You have a problem with your scope of this

.call(d3.behavior.zoom().on("zoom", redraw))
        .append('svg:g');

        function redraw() {
        this.svg.attr("transform",
            "translate(" + d3.event.translate + ")"
            + " scale(" + d3.event.scale + ")");
        }

should be something like:

.call(d3.behavior.zoom().on("zoom", redraw.bind(this))) //<-- bind the outer this here
        .append('svg:g');

        function redraw() {
        this.svg.attr("transform",
            "translate(" + d3.event.translate + ")"
            + " scale(" + d3.event.scale + ")");
        }

or with the es6 arrow syntax:

.call(d3.behavior.zoom().on("zoom", ()=> redraw() ) ) //<-- arrow syntax
        .append('svg:g');

        function redraw() {
        this.svg.attr("transform",
            "translate(" + d3.event.translate + ")"
            + " scale(" + d3.event.scale + ")");
        }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!