【发布时间】:2015-09-01 15:51:52
【问题描述】:
我正在尝试使用 ReactJS 在矩形中间显示文本。我已经尝试了不同的方法,但文本总是出现在右侧的图表之外。
我正在尝试执行以下操作: http://bl.ocks.org/mbostock/7341714
我得到以下结果:
如何让数字 60 出现在矩形的中间?
这是我的代码:
var BarChart = React.createClass({
getInitialState: function () {
return {props: this.props};
},
componentWillReceiveProps: function(nextProps) {
this.setState({
props: nextProps
});
},
render: function() {
return (
<svg width={this.state.props.width} height={this.state.props.height}>
<g style={{strokeWidth:4, stroke:"black"}}>
<rect fill={"white"} width={146} height={18}/>
<text> {this.state.props.propValue} </text>
</g>
</svg>
);
}
【问题讨论】:
-
如果我将 {this.state.props.propValue} 替换为非 React JS 变量,我只能在栏内添加文本。如果我用简单的字母替换它,文本将正确显示在里面。
标签: javascript d3.js reactjs