score:3
While scales may seem to be an appropriate method for plotting geographic points: don't use this approach.
You lose control over rotation of a projection and you cannot use a non cylindrical projection (only unrotated cylindrical projections can plot lat and long independently). But it also makes it very hard to align features positioned by scales with other map elements if they don't use the same approach.
Instead, D3 has a wide range of built in projections.
The projections take a [longitude,latitude] pair and return a [x,y] coordinate. Latitudes and longitudes must be in degrees, x and y are in pixels.
To create a projection you can use:
var projection = d3.geoMercator() // or geoAlbers, geoSupportedProjection, etc.
To use it, just pass it a coordinate:
projection([long,lat]) // [x,y]
In your case this might look like (for the cx, cy looks similar)
.attr("cx", function(d) { return projection([d.long,d.lat])[0] })
Now this projection is centered at 0,0 degrees by default and set up for a 960x500 pixel map. But you can modify scale, center and rotation, for example:
var projection = d3.geoMercator().center([-100,35]).scale(1000)
For a more complete run down of projection methods you should look at the documentation for d3-geo.
In your case there is a special composite projection that covers the US, d3.geoAlbersUsa, which has room for Hawaii and Alaska. But, because of its composite nature is less flexible, though you can still scale it. The default scale anticipates 960x600 pixel map (setting larger map scales spreads the map over a larger area).
Source: stackoverflow.com
Related Query
- How to get map to show longitude and latitude accurately with d3
- Draw a map with D3.js: How to get the right scale() and translate()?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- D3.js: How to combine 2 datasets in order to create a map and show values on.mouseover?
- How to draw circles with radii given in kilometers accurately on world map
- topojson / D3 / Map with longitude + latitude
- How can I use SVG translate to center a d3.js projection to given latitude and longitude values?
- How to draw D3 Map with United States, Canada, Mexico and Puerto Rico
- How to convert latitude and longitude into (x,y) coordinates using d3.js
- Zoom world map in D3 upto particular level Around a given latitude and longitude
- How to get d3 map to refresh with new saved data with javascript
- How to implement mouse wheel zoom in D3 like Google Map with overlay and Ctrl + Scroll event?
- How can I scale my map to fit my svg size with d3 and geojson path data
- How do you get JSON data and create a line chart with d3.js?
- D3 Line Chart- Adding line interactivity: How to get id's and select a line with each?
- How to show d3.js usa map with some state name outside the state pointing to it
- Initiate d3 map over certain area given latitude and longitude
- Issue with D3.JS and Flask-trying to get map of U.S
- Add Static Lines on D3 Map Between Latitude and Longitude Points
- How to show x and y coordinates of XY chart with mouseover using d3.js
- How does the following code get away with not defining initial x and y coordinates for nodes?
- How to show and hide a nodes text with button click and mouseover/mouseout ? D3/JS
- how to make sankyplots with no color for source and target node and how to do legend to show the different colors of linkgroup
- Using crossfilter with dc.js and d3.js - how to get csv year data into date format?
- D3.js: How to get the computed width and height for an arbitrary element?
- How do I remove all children elements from a node and then apply them again with different color and size?
- How do I save/export an SVG file after creating an SVG with D3.js (IE, safari and chrome)?
- d3.js Map (<svg>) Auto Fit into Parent Container and Resize with Window
- How do I show a bootstrap tooltip with an SVG object?
More Query from same tag
- Positioning externally loaded svg in D3
- d3 linechart transition with missing data
- On button click open a new window and draw multiple large circles with D3
- Calculate bounding polygon of alpha shape from the Delaunay triangulation
- NVD3 multi bar horizontal chart x axis domain
- Changing the color of an svg image
- D3.js - overlapped bar chart when using rangeBands()
- nextAll selector, transform elements after clicked element
- Having trouble drawing d3.js Focus+Context via Brushing chart with multiple paths
- Put D3 map over google maps
- D3js value of .data(...) field does not change on window resize. Is there a way to change it?
- Leaflet + D3 - No mouseover on Safari
- Optimize pendulum animation (javascript)
- d3 Bar Chart append text to bar
- Selecting element in external SVG
- geoJSON projection with d3.js and dc.js for South Africa and provinces
- How to show the value of each element when hovering
- How to filter d3.nested data in javascript - nested function
- Arbitrary variables in d3.js while function call
- Linked tooltip on heatmap and sparkline
- How to pass data to d3.json()
- Add html content with line break inside d3 circle
- Plot a shape using X,Y points
- Semantic zoom on map with circle showing capital
- Error: <rect> attribute y: Expected length, "NaN"
- Is there a method to find a Y value that is exceeded X % of the time in a time series?
- Trying To Plot Json Data On World Map Using leafletjs But Data Displayed is Not Accurate
- Code within d3.json() callback is not executed
- How to create an outline for selected edges and vertices in d3.js?
- Trying to add x and y axis D3