var SVGNS = "http://www.w3.org/2000/svg", Router = ReactRouter; var BarChart = React.createClass({ mixins: [Router.Navigation, Router.State], barHeight: 40, barMargin: 5, getInitialState: function() { return { item: this.props.items[0], sort: 'commits', rawData: [], points: [], min: 0, max: 1 }; }, componentDidMount: function() { this.fetchData(); }, onFilter: function(thing, i) { if (thing === 'item' && this.props.items[i] !== this.state.item) { this.setState({ item: this.props.items[i] }, this.fetchData); } else if (thing === 'sort' && ['commits', 'delta'][i] !== this.state.sort) { this.setState({ sort: ['commits', 'delta'][i] }, this.sort); } }, fetchData: function() { $.get(this.props.api, this.apiParams(), function(res){ this.setState({ rawData: res }, this.sort); }.bind(this)); }, sort: function() { var sortFun = function(a, b) { return Math.abs(b[this.state.sort]) - Math.abs(a[this.state.sort]); }.bind(this); var points = this.state.rawData.sort(sortFun).slice(0, 15); var min = 0, max = 1; points.map(function(el) { var val = el[this.state.sort]; if (val > max) { max = val; } if (val < min) { min = val; } }.bind(this)); s = { points: points, min: min, max: max }; console.log(s); this.setState(s); }, apiParams: function() { // Deep copy // Don't use jQuery.extend var params = JSON.parse(JSON.stringify(this.props.params)); params['item'] = this.state.item; return params; }, height: function() { if (this.state.points.length === 0) { return 0; } else { return this.y(this.state.points.length) - this.barMargin; } }, y: function(i) { return i*(this.barHeight + this.barMargin); }, render: function() { console.log("State:", this.state) return (