The pow.invert() function is used to return a value from the domain when given a value from the range. This inversion is useful for interaction such as determining the data value that corresponds to the position of the mouse.
Syntax:
pow.invert( value )
Parameters: This function accepts only one parameter as given above and described below:
- value: It is a number that belongs to any value in the given range.
Return Values: This function returns a number from the domain.
The program below illustrates the pow.invert() function in D3.js:
Example 1: Taking all elements of range to be positive.
HTML
<!DOCTYPE html> < html lang = "en" > < head > Â Â < meta charset = "UTF-8" /> Â Â < meta name = "viewport" Â Â Â Â Â Â Â Â Â content=" width = device -width, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â initial-scale = 1 .0" /> Â Â < title >neveropen</ title > Â Â < script src = Â Â </ script > Â Â < script src = Â Â </ script > Â Â < script src = Â Â </ script > Â Â < script src = Â Â </ script > </ head > < body > Â Â < h2 style = "color: green" >neveropen</ h2 > Â Â Â Â < p >pow.invert() Function </ p > Â Â Â Â < script > Â Â Â Â var pow = d3.scalePow() Â Â Â Â Â Â .domain([0, 1]) Â Â Â Â Â Â .range([1, 2, 3, 4, 5, 6]); Â Â Â Â Â Â document.write("< h3 > pow.invert(1): " +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow.invert(1) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow.invert(2): " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow.invert(2) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow.invert(3): " +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow.invert(3) + "</ h3 >"); Â Â Â Â document.write("< h3 > pow(pow.invert(1)): " +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow(pow.invert(1)) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow(pow.invert(2)): " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow(pow.invert(2)) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow(pow.invert(3)): " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow(pow.invert(3)) + "</ h3 >"); Â Â </ script > </ body > </ html > |
Output:
Example 2: Taking a range array such that it contains positive and negative numbers both.
HTML
<!DOCTYPE html> < html lang = "en" > < head > Â Â < meta charset = "UTF-8" /> Â Â < meta name = "viewport" Â Â Â Â Â Â Â Â Â content=" width = device -width, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â initial-scale = 1 .0" /> Â Â < title >neveropen</ title > Â Â < script src = Â Â </ script > Â Â < script src = Â Â </ script > Â Â < script src = Â Â </ script > Â Â < script src = Â Â </ script > </ head > < body > Â Â < h2 style = "color: green" >neveropen</ h2 > Â Â Â Â < p >pow.invert() Function </ p > Â Â Â Â < script > Â Â Â Â var pow = d3.scalePow() Â Â Â Â Â Â .domain([-1, 1]) Â Â Â Â Â Â .rangeRound([10, 20, 30, 40, 50, 60]) Â Â Â Â Â Â .exponent(2); Â Â Â Â Â Â document.write("< h3 > pow.invert(1): " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow.invert(1) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow.invert(2): " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow.invert(2) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow.invert(3): " +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow.invert(3) + "</ h3 >"); Â Â Â Â document.write("< h3 > pow(pow.invert(-1)): " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow(pow.invert(-1)) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow(pow.invert(-2)): " + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow(pow.invert(-2)) + "</ h3 >"); Â Â Â Â document.write("< h3 >pow(pow.invert(-1)): " +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pow(pow.invert(-3)) + "</ h3 >"); Â Â </ script > </ body > </ html > |
Output: