Closed benjamin658 closed 8 years ago
先判斷data陣列的長度大於0才render LineTooltip後就可以運作了
'use strict';
/**
* Created by Ben Hu on 2015/11/24.
*/
import React from 'react';
import d3 from 'd3';
import {LineTooltip} from 'react-d3-tooltip';
import TradeBalanceAction from '../../actions/TradeBalanceAction';
import TradeBalanceStore from '../../stores/TradeBalanceStore';
const MyLineChart = React.createClass({
getInitialState(){
return {
chartData: TradeBalanceStore.getState().allTradeBalance
}
},
componentDidMount(){
TradeBalanceStore.listen(this._onTradeBalanceChange);
if (this.isMounted()) {
TradeBalanceAction.getAllTradeBalance(this.props.country);
}
},
componentWillUnmount() {
TradeBalanceStore.unlisten(this._onTradeBalanceChange);
},
_onTradeBalanceChange(tradeBalanceData){
this.setState({
chartData: tradeBalanceData.allTradeBalance,
yearTradeBalanceData: tradeBalanceData.yearTradeBalance
});
},
render(){
var parseDate = d3.time.format("%Y").parse;
var width = 1000,
height = 350,
margins = {left: 100, right: 100, top: 50, bottom: 50},
chartSeries = [
{
field: 'balance',
name: 'Balance',
color: '#3498db',
style: {
"stroke-width": 3,
"stroke-opacity": .7,
"fill-opacity": .0
}
},
{
field: 'yearExportValue',
name: 'Export',
color: '#8e44ad',
style: {
"stroke-width": 3,
"stroke-opacity": .7,
"fill-opacity": .0
}
},
{
field: 'yearImportValue',
name: 'Import',
color: '#ff7f0e',
style: {
"stroke-width": 3,
"stroke-opacity": .7,
"fill-opacity": .0
}
}
],
title = this.props.country.toUpperCase() + " Trade Balance",
x = function (d) {
return parseDate(d.year);
},
xRange = [0, width - margins.left - margins.right],
yRange = [height - margins.top - margins.bottom, 0],
yDomain = [
d3.min(this.state.chartData, function (d) {
return d.balance;
}),
d3.max(this.state.chartData, function (d) {
return d.yearExportValue;
})
],
xScale = 'time';
return (
<div>
{(this.state.chartData.length > 0)
? <LineTooltip
margins={margins}
title={title}
data={this.state.chartData}
width={width}
height={height}
chartSeries={chartSeries}
x={x}
xRange={xRange}
xScale={xScale}
yRange={yRange}
yDomain={yDomain}/>
: <div>rendering...</div>
}
</div>
)
}
});
export default MyLineChart;
我將LineTooltip的範例套上不同的資料後,圖有正確的畫出來,但是hover時tooltip並沒有出現,另外console log也沒也任何的錯誤,sample code如下
資料格式如下
感謝!