1
私はチュートリアルの下のコードを使用してReactでd3折れ線グラフを作成しています。d3.timeFormatがNaNを返します
react.min.js:12エラー:属性D:予想される数、 "MNaN、NaNLNaN、NANC ..." それは私に次のエラーを与え続け
。
私がD3で作成したparseDate関数は、曜日プロパティのNaNを返します。私は何をしていますか?ここでは、コードは次のようになります。
var LineChart = React.createClass({
//define the width, height and chartId as proptypes
propTypes: {
width:React.PropTypes.number,
height:React.PropTypes.number,
chartId:React.PropTypes.string
},
//set default props
getDefaultProps: function() {
return {
width: 800,
height: 300,
chartId: 'v1_chart'
};
},
//set width as this.state.width for 2-way binding to create responsive chart
getInitialState:function(){
return {
width:this.props.width
};
},
render: function(){
//dummy data
const data=[
{day:'02-11-2016',count:180},
{day:'02-12-2016',count:250},
{day:'02-13-2016',count:150},
{day:'02-14-2016',count:496},
{day:'02-15-2016',count:140},
{day:'02-16-2016',count:380},
{day:'02-17-2016',count:100},
{day:'02-18-2016',count:150}
];
//dimensions and position of the chart inside SVG container
const margin = {top:5, right:50, bottom:20, left:50};
//ww is set dynamically using this.state.width for responsive charts
const w = this.state.width - (margin.left + margin.right);
const h = this.props.height - (margin.top + margin.bottom);
//use d3 to parse the dates
const parseDate = d3.timeFormat("%m-%d-%y");
data.forEach(d=>{
d.date = parseDate(d.day);
});
//create scales
const x = d3.scaleTime()
.domain(d3.extent(data, function (d) {
return d.date;
}))
.rangeRound([0, w]);
const y = d3.scaleLinear()
.domain([0,d3.max(data,function(d){
return d.count+100;
})])
.range([h, 0]);
//generate line using scales
const line = d3.line()
.x(d=>{
x(d.date);
})
.y(d=>{
y(d.count);
}).curve(d3.curveBasis);
//transform the chart position
const transform = 'translate(' + margin.left + ',' + margin.top + ')';
//render the line chart
return (
<div>
<svg id={this.props.chartId} width={this.state.width} height={this.props.height}>
<g transform={transform}>
<path className="line shadow" d={line(data)} strokeLinecap="round"/>
</g>
</svg>
</div>
);
}
});
こんにちはアンドリュー、私はそれをしなかったが、それでも次のエラーを取得:react.min.js:12エラー:属性d:予想される番号 "MNaN、NaNLNaN、NaNC ..."。回線機能に何か問題があるようですか? –
chemook78
ラインジェネレータでスケーリングされたデータ値を返す必要があるかもしれません。または、矢印機能で角括弧をスキップします。私はあなたのコードが(少し簡略化)作業を示す必要がありますスニペットを追加しました。 –