score:1
Accepted answer
Have you ever considered to connect your projection and path generator?
var path = d3.geoPath(projection);
Also set the path in the resize method. Label the 2 path elements
svg.select('#nation').attr('d', path(topojson.feature(us, us.objects.nation))); svg.select('#nation2').attr('d', path(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })));
the onload in the body has to go
<body>
there are no tags with class
land
orstate
//svg.select('.land').attr('d', path); //svg.select('.state').attr('d', path);
why call projection
size
andtranslate
if you callfitSize
Here is the script code
var width = parseInt(d3.select('#mapContainer').style('width')) - 50
,height = width/960*600;
var svg = d3.select("svg")
.attr("width", width + 50)
.attr("height",height);
var defs = svg.select("defs");
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
var topoFeatureStates = topojson.feature(us, us.objects.states);
var projection = d3.geoIdentity()
.fitSize([width, height], topoFeatureStates);
var path = d3.geoPath(projection);
defs.append("path")
.datum(topojson.feature(us, us.objects.nation))
.attr("id", "nation")
.attr("d", path);
svg.append("use")
.attr("xlink:href", "#nation")
.attr("fill-opacity", 0.2)
.attr("filter", "url(#blur)");
svg.append("use")
.attr("xlink:href", "#nation")
.attr("fill", "#fff");
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("fill", "none")
.attr("stroke", "#777")
.attr("stroke-width", 0.70)
.attr("d", path);
// function to resize map
function resize() {
// adjust things when the window size changes
width = parseInt(d3.select('#mapContainer').style('width')) - 50;
//console.log(parseInt(d3.select('#mapContainer').style('width')));
height = width/960*600;
svg.attr('width', width + 50).attr('height', height);
// update projection
projection
.fitSize([width, height], topoFeatureStates);
// resize the map
svg.selectAll('path').attr('d', path);
}
// resize event
d3.select(window).on("resize", resize);
});
Edit
Wondering why the path
in the resize did not I work had a second look at the responsive example. It attaches the result of topojson
as datum to the path
.
Modified the code to do the same.
Source: stackoverflow.com
Related Query
- Create a responsive D3 map with svg filter dropshadow
- create a responsive svg with react
- How to create a responsive map using d3
- d3.js - svg filter equivalent with d3.js
- Export leaflet map WITH SVG layer to image
- Making a responsive angled svg shape with pattern
- How to create responsive svg using d3.js
- Create map that combines data from two files with common column - but data doesn't match one to one
- d3js v5: Create a SVG with groups from a JSON file
- How to create SVG with grid lines using D3JS
- How to display country name labels on an SVG map drawn with d3?
- How to Add or Create a Map Scale Bar to a Map created with D3.js
- How can I scale my map to fit my svg size with d3 and geojson path data
- d3.js create same svg object with different data
- Trying to create a map with D3, but it isn't showing up
- Create rect with blurred stroke in svg using d3.js
- How to make SVG responsive in D3 with viewbox?
- D3.js load US Map with Initial Zoom on one State by SVG
- trouble with D3js world map and svg group element width
- Using datamaps, I am trying to create a map with not 100% opacity
- Using datamaps, I am trying to create a map with not 100% opacity
- d3js - Create a server side SVG with javascript actions
- Responsive SVG with d3.js and height attribute
- Create map with kendo ui
- Create d3 map with geo-albers-usa-territories projection
- How to use an array (inside a javascript script) to create a SVG Polygon chart with D3.js?
- Create circle on svg map
- Trying to create an SVG with a grid of images. But the images don't show
- how to create co-ordinates in svg for d3 js map
- How can create a table with using filter on csv data
More Query from same tag
- Example of D3 USA Map done in angular2
- reading in csv with date parsing
- How to invert direction of nodes in diagram in d3.js
- Unit testing AngularJS directive, postLink D3 not changing DOM
- How to call a function on click of a checkbox which is a popup on click of d3 element like rect
- How to fix the configuration of the nodes that are returned? (D3, Javascript, Neo4j)
- Space out part of the pie chart in d3?
- Layering D3 Graphs: Swimlanes + Data Connections
- Sliding Value Selector With D3
- Looking for a way to display labels on sunburst chart (could not find a working example)
- How to add a key to a networkD3 visualization
- How do I display dates on the x-axis for nvd3 / d3.js?
- Read Data from JSON file using d3.js
- Creating hierarchical JSON from MySQL results and PHP for D3.js tree?
- TCPDF draws <defs> tags
- d3.js Add labels in Chord diagram
- Typings Error when using D3 V4 on Angular 2 and Typescript V2
- d3 accessor of hierarchical array of objects
- How to make bars have different patterns in d3.js?
- JQuery slider doesn't interact with data drawn in vertical line with d3
- d3.js: swap between topojson maps on mouseclick?
- d3.js string values as x axis domain
- Stacked Bar Chart to Grouped Bar Chart transition not working
- Key binding with d3.svg.path()
- Dots of Scatterplot in d3 are not rendering
- How to expand the only one piece of arc in d3.js?
- How to select right chart for your data
- D3.js - How to add the general update pattern to a stacked bar chart
- how to plot the image inside the polygon in d3
- Specifying Ticks on D3 Bar chart with Time Series data and scaleBand