このコンポーネントは、ページビューの一部として作成され、ユーザーは最初のダッシュボードからナビゲートします。 svgが正しくレンダリングする必要があるすべてのデータが正しくロギングされているように見えますが、SVGはこのビューの初期ナビゲーションでは作成されていません。関数は実行されていますが、htmlにはSVGは表示されません。最初は画像がまだロードされていなかったためだと思っていましたが、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