2

Ive been trying to make persistent tooltip by drawing a layer over the graph and track the tooltip based on mouse position. Unfortunately I kept getting an error on line xScaleFunc.invert(d3.mouse(this)[0]). Then i came across this custom invert code for scaleband scales. From here : https://bl.ocks.org/shimizu/808e0f5cadb6a63f28bb00082dc8fe3f .

The question is i'm not understanding whats going on here.I'm kinda new to D3 , any help would be much appreciated.

xScaleFunc.invert = (function(){
    var domain = xScaleFunc.domain()
    var range = xScaleFunc.range()
    var scale = d3.scaleQuantize().domain(range).range(domain)

    return function(x){
        return scale(x)
    }
})()
Emmanual
  • 124
  • 1
  • 8
  • 4
    Just for your information, this whole function could be just `xScale.invert = function(x){ return d3.scaleQuantize().domain(this.range()).range(this.domain())(x);}`, removing the currying and the IIFE. Regarding your question, I'm not sure that explaining it line by line would be on topic here at SO. Anyway, do you know how a quantize scale works? – Gerardo Furtado Feb 01 '19 at 11:47
  • 1
    https://stackoverflow.com/a/50846323/9938317 – rioV8 Feb 01 '19 at 12:13
  • @GerardoFurtado Hey thanks for the optimisation , i'm trying to learn as much as possible so any source would help. No i do not , ill have to go through the documentation i suppose. – Emmanual Feb 01 '19 at 13:07
  • @rioV8 thanks , will check that out. – Emmanual Feb 01 '19 at 13:08
  • @Emmanual Yes, you should. The moment you understand what a quantize scale does, and paying attention to the fact that the range/domain are inverted (regarding the original band scale), everything will be clear. – Gerardo Furtado Feb 01 '19 at 13:12
  • will get right on it , thank you :) @GerardoFurtado – Emmanual Feb 04 '19 at 08:12
  • 1
    @Emmanual If you still didn't understand fell free to ask for a proper answer. – Gerardo Furtado Feb 04 '19 at 08:34

0 Answers0