score:3
Accepted answer
D3 ordinal scales don't have invert
methods, but you can create your own.
Once I saw a clever alternative using a scaleQuantile
, along these lines:
xscale.invert = function(x) {
var domain = this.domain();
var range = this.range()
var scale = d3.scaleQuantize().domain(range).range(domain)
return scale(x)
};
Then, in your case, just use:
console.log(xscale.invert(2)) // returns c
Here is a demo:
var xscale = d3.scaleBand() // v4
.domain(["a", "b", "c", "d", "e"])
.range([0, 5]);
xscale.invert = function(x) {
var domain = this.domain();
var range = this.range()
var scale = d3.scaleQuantize().domain(range).range(domain)
return scale(x)
}
d3.range(0, 5.2, 0.2).forEach(function(d) {
console.log(d.toFixed(2) + " corresponds to: " + xscale.invert(d))
})
<script src="https://d3js.org/d3.v5.min.js"></script>
PS: This solution will ignore any padding you have in the scale.
Source: stackoverflow.com
Related Query
- Get domain from range using ScaleBand
- d3.js how to get minimum value of scale's domain when using nice()
- Get data from rest service using D3
- Create data visualizations using Express and get data from a PostgreSQL database
- Get width from element using D3.js
- How to get value from the element using selection in d3
- Get click event from SVG bounding box using d3js
- How to color SVG image based on value from color range using d3?
- How can I get the equivalent to a "this" keyword from d3 using d3.select()?
- how to get data from mongodb and display as a d3 chart using nodejs
- How to get Average from DOW of Date column using crossfilter to populate in bar chart
- Get ID from dataset when clicking on a series using Dimple JS
- Axis Range and Domain issue on bar graph using D3
- Get Selected Slice Color from Pie chart using DC.js
- How can I get the source code from a page using javascript
- get d3.extent of property from a shapefile to use as domain
- How to get parent node from child in json using d3js as Force Layout?
- How to get all value displayed from loop in javascripts using d3.js? always got last value displayed in charts
- Can't get JSON data from file from local domain URL (Django static file)
- d3 scaleBand not returning maximum range for last element in domain
- How to get the count of "Message" and distinct count of "name" from json using javascript?
- Using D3 loaded with json file, How can get parent nodes value from json file?
- How do I prevent selection of data outside my selected range from highlighting when using crossfilter.js?
- How to write vertical text from bottom to top without using transform rotate?
- How do I get the width of an svg element using d3js?
- Get one element from d3js selection, by index
- D3: Create a continuous color scale with many strings/inputs for the range and dynamically changing values of the domain
- D3 get attributes from element
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- Importing data from .csv using d3.js
More Query from same tag
- Problem with select a rect element with d3
- How do I correctly give a function as a parameter in JavaScript with d3
- dc.js - re-arrange labels in row chart
- What is a good way to convert %H:%M:%S strings to timespan?
- D3 Accessing array in Json object to attribute
- d3: Text displayed when clicked on leaflet map
- How to change easing in d3 pathTween function
- How to Build this Rounded Two Tone Donut Chart?
- D3JS scaling and transition performance
- Color regions in a D3 map based on external data
- Draw circles inside pie - d3 chart
- D3 find the angle of line
- How to link a JSON object from R source code to d3.js for building a shiny app - shiny doesn't render d3
- Removing a circle in d3 with a specific cx and cy values?
- D3 add stroke to the side of a path only
- d3 js line graph takes two different data formats. how do I differentiate those two?
- How to get a value of select with d3?
- Create nodes based on content BBox and get width of source node when adding links in force layout
- Updating links on a force directed graph from dynamic json data
- Is it possible to pass the scope from one controller file to another file
- D3 line chart and adding data points real time
- Multiple force-layout graphs with d3 in seperate svg/div's
- How to use select option value (string) inside a d3.Threshold.range() that needs an object in D3.js?
- D3.js line chart: axis with fixed position
- Ticks not showing up for a multi-series d3 svg chart
- Scan AWS DynamoDB records only when there is new information
- Update zoom state in d3 v4 after manually setting translation and scale
- d3 tooltip. Collapsible tree. Error when .on(mouseover,function(....)) is called
- Uncaught TypeError: Cannot read property '0' of null in d3?
- Angular 2 typescript d3 type issue: Property 'x' does not exist on type '[number, number]'