From 58ccdbf27093bae3bf6f7fd79880c5eacc873147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=8F=85?= Date: Sun, 9 Apr 2017 17:10:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96LineChart=E6=80=A7=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E5=87=8F=E5=B0=91=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84?= =?UTF-8?q?=E9=87=8D=E7=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javascripts/admin/components/LineChart.js | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/nodejs/static/javascripts/admin/components/LineChart.js b/nodejs/static/javascripts/admin/components/LineChart.js index e8c8c08f..6d074151 100644 --- a/nodejs/static/javascripts/admin/components/LineChart.js +++ b/nodejs/static/javascripts/admin/components/LineChart.js @@ -37,19 +37,34 @@ export default class extends React.Component { } } componentDidUpdate(prevProps, prevState) { - this.updateChart(); + try { + let prevPropsStr = JSON.stringify(prevProps); + let propsStr = JSON.stringify(this.props); + if (prevPropsStr != propsStr || prevState.windowWidth != this.state.windowWidth) { + this.updateChart(); + } + } catch(err) { + + } } updateChart() { + if (this.state.windowWidth <= 1) { + return; + } + if (!this.props.data || this.props.data.length <= 0) { + return; + } var myChart = echarts.init(this.refs.chart); myChart.setOption(this.getChartOption()); } getChartOption() { - let data = this.props.data; - let xName = this.props.xName; - let yName = this.props.yName; - let title = this.props.title; - let xArr = []; - let yArr = []; + let data = this.props.data; + let xName = this.props.xName; + let yName = this.props.yName; + let yLabel = this.props.yLabel; + let title = this.props.title; + let xArr = []; + let yArr = []; for (let i = 0; i < data.length; i++) { xArr.push(data[i][xName]); yArr.push(data[i][yName]); @@ -67,7 +82,7 @@ export default class extends React.Component { color: '#ddd' } }, - formatter: '{b}
' + title + ': {c}' + formatter: '{b}
' + yLabel + ': {c}' }, xAxis: { type: 'category', @@ -120,12 +135,8 @@ export default class extends React.Component { }; } render() { - var style = { - width : '100%', - height : '100%' - }; return ( -
+
) } } \ No newline at end of file