This component gets created as part of a page view that users navigate to off of the initial dashboard. All of the data that the svg needs to render correctly appears to be logging correctly but the SVG just isn't being created on initial navigation to this view. The function is running but no SVG is showing up in the html. At first I thought it was because the image hadn't been loaded yet, but the SVG is being appended to the div not directly to the image so it would at the very least I think show up in the html minus width and height. Any help would be much appreciated.
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);