Этот компонент создается как часть просмотра страницы, на котором пользователи переходят на начальную панель. Все данные, которые SVG должен правильно отображать, правильно регистрируются, но SVG просто не создается при начальной навигации к этому представлению. Функция работает, но SVG не отображается в html. Сначала я подумал, что это связано с тем, что изображение еще не загружено, но SVG присоединяется к div не непосредственно к изображению, поэтому, по крайней мере, я думаю, что оно появляется в html минус ширина и высота. Любая помощь приветствуется.d3 svg не рендеринг на рендеринг исходного компонента. React
import React, { Component, PropTypes } from 'react';
import $ from 'jquery';
import { connect } from 'react-redux';
class BrainComponent extends Component {
static propTypes = {
showBrainData: PropTypes.bool.isRequired,
showThisHitData: PropTypes.object,
hit: PropTypes.object
};
constructor(props, context) {
super(props, context);
this.state = {
showBrainData: this.props.showBrainData,
};
}
componentWillMount() {
console.log(this.props);
}
handleBrainVizColor() {
console.log(this.props.hit.Hic);
let colorString;
const hic = this.props.hit.Hic;
const redNum = 80 + (Math.round(hic/2));
const greeNum = 180 - (Math.round(hic/2));
colorString = "rgba(" + redNum + "," + greeNum + ", 0, .4)";
return colorString;
}
renderBrainViz() {
console.log(this.props.hit);
this.renderRecangles();
}
componentDidMount() {
// this.renderBrainViz.bind(this);
}
renderRecangles() {
d3.select(".brain-canvas").remove();
const fillColor = this.handleBrainVizColor();
console.log(this.props.showBrainData);
if (this.props.showBrainData) {
const brainWidth = $(".brain-img").width();
const brainHeight = $(".brain-img").height();
let xLocation;
let yLocation;
let width = brainWidth/2;
let height = brainHeight/2;
console.log(this.props.hit.ImpactDirection);
switch (this.props.hit.ImpactDirection) {
case 'URP':
xLocation = brainWidth/2;
yLocation = 0;
break;
case 'LRP':
xLocation = 0;
yLocation = brainHeight/2;
height += 10;
break;
case 'CR':
break;
case 'LR':
xLocation = 0;
yLocation = 0;
width = brainWidth;
break;
case 'UR':
xLocation = 0;
yLocation = brainWidth/2;
width = brainWidth;
break;
case 'BA':
break;
case 'LF':
xLocation = 0;
yLocation = 0;
width = brainWidth;
break;
case 'UF':
xLocation = 0;
yLocation = 0;
width = brainWidth;
break;
case 'ULP':
xLocation = 0;
yLocation = 0;
break;
case 'LLP':
xLocation = 0;
yLocation = 0;
break;
}
const brainCanvas = d3.select(".brain-one").append("svg")
.attr("width", brainWidth)
.attr("height", brainHeight)
.attr("class", "brain-canvas");
brainCanvas.append("rect")
.attr("x", xLocation)
.attr("y", yLocation)
.attr("width", width)
.attr("height", height)
.style("fill", fillColor);
}
}
render() {
return (
<div className="brain-container">
<div className="brain-one">
<img className="brain-img" src="https://s3-us-west-2.amazonaws.com/mvtrak/new-brain.png" />
</div>
{ this.renderBrainViz() }
{ !this.props.showBrainData ? <div>
<div className="brain-overlay"></div>
<div className="select-hit-text">SELECT HIT BELOW</div>
</div> : null }
</div>
);
}
}
function mapStateToProps(state) {
console.log(state);
return {
hit: state.hitData
};
}
export default connect(mapStateToProps)(BrainComponent);
Это удивительно полезно, и все это делает тонны смысла. – hifilorau