I’ve been working on this for 10 hours, and I’m starting to think that it’s either something really small or something fundamental that I’m not here.
This is a simple component that all I want to do is make a usb card with d3 version 4 using geoAlbersUsa and the PROJECT in the panel so that it is SCALED. If I delete the projection, everything works fine, and I get the map. A moment in any shape or form, I am making a projection, it just shows a colored rectangle. Here is the code:
import React from 'react';
import * as d3 from 'd3';
import * as topojson from 'topojson';
import { Panel, Alert } from 'react-bootstrap';
class MapBlock extends React.Component {
constructor(props) {
super(props);
this.state = {
states: []
};
this.projection = d3.geoAlbersUsa().scale(1000);
this.geoPath = d3.geoPath().projection(this.projection);
}
componentDidMount() {
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
this.setState({states: topojson.feature(us, us.objects.states).features})
}.bind(this));
}
render() {
let { states } = this.state;
return (
<Panel>
<svg width="550" height="430">
<g className="states">
{
states.map((feature, index) => <path key={index} d={this.geoPath(feature)} />)
}
</g>
</svg>
</Panel>
)
}
}
export default MapBlock
html is also pretty simple:
<Grid>
<Row className="show-grid">
<Col sm={12} md={6}>
<MapBlock />
</Col>
<Col sm={12} md={6}>
Some Text ...
</Col>
</Row>
</Grid>
Any help is appreciated and please let me know if the details are not enough. Thanks in advance.
UPDATE:
Here's what I'm trying to work with the sentence from the original comment from @Mark now.
I tried something like this:
var states = topojson.feature(us, us.objects.states).features;
var width = 560;
var height = 300;
var b = path.bounds(states[0]),
s = .98 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];
projection
.scale(s)
.translate(t);
vis.selectAll("path")
.data(states)
.enter()
.append("path")
.attr("class", "states")
.attr("d", path);
To do this, I changed the visualization as simple as:
<Panel>
<div id="vis"></div>
</Panel>
, , - path.bounds(states[0]), , , , . "" , , :)