diff --git a/app/scripts/src/stacked_area_chart.jsx b/app/scripts/src/stacked_area_chart.jsx index ed8dc42..c345790 100644 --- a/app/scripts/src/stacked_area_chart.jsx +++ b/app/scripts/src/stacked_area_chart.jsx @@ -1,17 +1,21 @@ var StackedAreaChart = React.createClass({ - mixins: [Router.Navigation, Router.State], + mixins: [Router.Navigation, Router.State, Chart], numElements: 10, + maxWeeks: 20, height: 250, getInitialState: function() { return { + currentApi: null, + currentParams: null, item: this.props.items[0], rawData: [], top: [], max: 1, weeks: [], - canvasWidth: 500 + canvasWidth: 0, + state: 'initial' }; }, @@ -23,21 +27,29 @@ var StackedAreaChart = React.createClass({ componentWillReceiveProps: function(newProps) { this.setState({ - 'item': newProps.items[0], - 'sort': 'commits' + item: newProps.items[0], + sort: 'commits', + state: 'newProps' }, this.fetchData); }, - calculateViewBoxWidth: function() { - this.setState({ - canvasWidth: this.refs.svg.getDOMNode().offsetWidth - }); + shouldComponentUpdate: function(newProps, newState) { + // console.log("Should update?", newState.state); + if (newState.canvasWidth === 0) { + return false; + } + if (newState.state !== 'newPoints') { + return false; + } + // console.log("Updating!"); + return true; }, handleFilter: function(thing, i) { if (this.props.items[i] !== this.state.item) { this.setState({ - item: this.props.items[i] + item: this.props.items[i], + state: 'newProps' }, this.fetchData); } }, @@ -49,10 +61,26 @@ var StackedAreaChart = React.createClass({ }, fetchData: function() { - $.get(this.props.api, this.apiParams(), function(res){ - this.setState({ - rawData: res - }, this.buildPoints); + if (!this.apiParams().item) { + return; + } + if (this.state.currentApi === this.props.api && + this.state.currentParams === JSON.stringify(this.apiParams())) { + return; + } + + // console.log('-----> fetching', this.props.api, this.state.item); + this.setState({ + currentApi: this.props.api, + currentParams: JSON.stringify(this.apiParams()), + state: 'loadingData' + }, function() { + $.get(this.props.api, this.apiParams(), function(res){ + this.setState({ + rawData: res, + state: 'newData' + }, this.buildPoints); + }.bind(this)); }.bind(this)); }, @@ -99,10 +127,12 @@ var StackedAreaChart = React.createClass({ // return _.sum(_.values(items)); // })); + // console.log("New points!"); this.setState({ top: top, max: max, - weeks: weeks + weeks: weeks, + state: 'newPoints' }); }, @@ -117,18 +147,11 @@ var StackedAreaChart = React.createClass({ d.unshift('M0,'+ maxHeight); d.push('L'+ maxWidth +','+ maxHeight +'Z'); - // for (var i = 0; i < missing; i++) { - // d.push('L'+ i +','+ this.props.height/2); - // } - // for (var i = 0; i < points.length; i++) { - // d.push('L'+ missing+i +','+ points[i]); - // } - // d.push('L'+ this.props.width +','+ this.props.height/2, 'Z'); - return d.join(' '); }, render: function() { + // console.log("Rendering!"); var maxWidth = this.state.canvasWidth, maxHeight = this.height, rtop = this.state.top.reverse(), @@ -151,27 +174,49 @@ var StackedAreaChart = React.createClass({ return [week, points]; }) .sort(0) + .reverse() + .take(this.maxWeeks) + .reverse() .value(); var paths = _.reduce(rtop, function(res, item, i) { res[item] = _.map(points, function(pair) { return pair[1][i]; - }).slice(-15); + }); return res; }, {}); + var paths = _.map(rtop, function(item, i) { + var itemPoints = _.map(points, function(pair) { + return pair[1][i]; + }); + return[item, itemPoints]; + }); - var i = -1; var colors = {} - var areas = _.map(paths, function(path, item) { - i++; + // console.log('----- Areas!'); + var areas = _.map(paths, function(pair, i) { + var item = pair[0], path = pair[1]; colors[item] = Colors2[i]; + // console.log("Building path for", item, path); + // console.log('Area', i); return ( - + d={roundPathCorners(this.buildPathD(path), 5)} + color={colors[item]} /> ); }.bind(this)); + areas = areas.reverse(); + for (var i = areas.length; i < this.numElements; i++) { + // console.log('Area (empty)', i); + var d = 'M0,'+ this.height +' L'+ maxWidth +','+ maxHeight +'Z'; + areas.push( + + ); + }; return (
@@ -184,10 +229,10 @@ var StackedAreaChart = React.createClass({ items={['commits']} value={'commits'} />
- - {areas.reverse()} + {areas}