2015-03-11 12:10:11 +00:00
|
|
|
var StackedAreaChart = React.createClass({
|
2015-03-12 13:11:52 +00:00
|
|
|
mixins: [Router.Navigation, Router.State, Chart],
|
2015-03-11 12:10:11 +00:00
|
|
|
|
|
|
|
numElements: 10,
|
2015-03-12 13:11:52 +00:00
|
|
|
maxWeeks: 20,
|
2015-03-11 12:10:11 +00:00
|
|
|
height: 250,
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2015-03-12 13:11:52 +00:00
|
|
|
currentApi: null,
|
|
|
|
currentParams: null,
|
2015-03-11 12:10:11 +00:00
|
|
|
item: this.props.items[0],
|
|
|
|
rawData: [],
|
|
|
|
top: [],
|
|
|
|
max: 1,
|
|
|
|
weeks: [],
|
2015-03-12 13:11:52 +00:00
|
|
|
canvasWidth: 0,
|
|
|
|
state: 'initial'
|
2015-03-11 12:10:11 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-03-11 20:21:28 +00:00
|
|
|
componentDidMount: function() {
|
|
|
|
this.fetchData();
|
|
|
|
this.calculateViewBoxWidth();
|
|
|
|
window.addEventListener('resize', this.calculateViewBoxWidth);
|
|
|
|
},
|
|
|
|
|
2015-03-11 20:18:14 +00:00
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
this.setState({
|
2015-03-12 13:11:52 +00:00
|
|
|
item: newProps.items[0],
|
|
|
|
sort: 'commits',
|
|
|
|
state: 'newProps'
|
2015-03-11 20:18:14 +00:00
|
|
|
}, this.fetchData);
|
|
|
|
},
|
|
|
|
|
2015-03-12 13:11:52 +00:00
|
|
|
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;
|
2015-03-11 12:10:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleFilter: function(thing, i) {
|
|
|
|
if (this.props.items[i] !== this.state.item) {
|
|
|
|
this.setState({
|
2015-03-12 13:11:52 +00:00
|
|
|
item: this.props.items[i],
|
|
|
|
state: 'newProps'
|
2015-03-11 12:10:11 +00:00
|
|
|
}, this.fetchData);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleClick: function(point) {
|
|
|
|
var params = {org: this.getParams().org};
|
|
|
|
params[this.state.item] = point.item;
|
|
|
|
this.transitionTo(this.state.item, params);
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchData: function() {
|
2015-03-12 13:11:52 +00:00
|
|
|
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));
|
2015-03-11 12:10:11 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
apiParams: function() {
|
|
|
|
var params = _.clone(this.props.params);
|
|
|
|
params['item'] = this.state.item;
|
|
|
|
return params;
|
|
|
|
},
|
|
|
|
|
|
|
|
buildPoints: function() {
|
|
|
|
// Group commits by items
|
|
|
|
var counts = _.reduce(this.state.rawData, function(res, el) {
|
|
|
|
if (res[el.item] === undefined) {
|
|
|
|
res[el.item] = el.commits;
|
|
|
|
} else {
|
|
|
|
res[el.item] += el.commits;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
// Extract top items from
|
|
|
|
var top = _.chain(_.pairs(counts)) // Take [item, count] pairs from counts object
|
|
|
|
.sortBy(1).reverse() // sort them by count (descending)
|
|
|
|
.take(this.numElements) // take first N pairs
|
|
|
|
.pluck(0) // keep only items, omit the counts
|
|
|
|
.value();
|
|
|
|
|
|
|
|
var weeks = _.reduce(this.state.rawData, function(res, el) {
|
|
|
|
if (res[el.week] === undefined) {
|
|
|
|
res[el.week] = {};
|
|
|
|
}
|
2015-03-11 20:18:14 +00:00
|
|
|
if (top.indexOf(el.item) > -1) {
|
|
|
|
res[el.week][el.item] = el.commits;
|
|
|
|
}
|
2015-03-11 12:10:11 +00:00
|
|
|
return res;
|
|
|
|
}, {});
|
2015-03-11 20:18:14 +00:00
|
|
|
var max = _.chain(weeks).keys().sort().reverse().take(15).map(function(week) {
|
|
|
|
return _.sum(_.values(weeks[week]));
|
|
|
|
})
|
|
|
|
.max()
|
|
|
|
.value();
|
2015-03-11 12:10:11 +00:00
|
|
|
|
2015-03-11 20:18:14 +00:00
|
|
|
// var max = _.max(_.map(weeks, function(items, week) {
|
|
|
|
// return _.sum(_.values(items));
|
|
|
|
// }));
|
2015-03-11 12:10:11 +00:00
|
|
|
|
2015-03-12 13:11:52 +00:00
|
|
|
// console.log("New points!");
|
2015-03-11 12:10:11 +00:00
|
|
|
this.setState({
|
|
|
|
top: top,
|
|
|
|
max: max,
|
2015-03-12 13:11:52 +00:00
|
|
|
weeks: weeks,
|
|
|
|
state: 'newPoints'
|
2015-03-11 12:10:11 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
buildPathD: function(points) {
|
|
|
|
var maxWidth = this.state.canvasWidth,
|
|
|
|
maxHeight = this.height,
|
2015-03-11 20:18:14 +00:00
|
|
|
maxValue = this.state.max,
|
2015-03-11 12:10:11 +00:00
|
|
|
len = points.length;
|
|
|
|
var d = _.map(points, function(point, i) {
|
2015-03-11 20:18:14 +00:00
|
|
|
return 'L'+ Math.floor(i/len*maxWidth) +','+ Math.floor(maxHeight - point);
|
2015-03-11 12:10:11 +00:00
|
|
|
});
|
2015-03-11 20:18:14 +00:00
|
|
|
d.unshift('M0,'+ maxHeight);
|
2015-03-11 12:10:11 +00:00
|
|
|
d.push('L'+ maxWidth +','+ maxHeight +'Z');
|
|
|
|
|
|
|
|
return d.join(' ');
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2015-03-12 13:11:52 +00:00
|
|
|
// console.log("Rendering!");
|
2015-03-11 12:10:11 +00:00
|
|
|
var maxWidth = this.state.canvasWidth,
|
|
|
|
maxHeight = this.height,
|
|
|
|
rtop = this.state.top.reverse(),
|
|
|
|
max = this.state.max;
|
|
|
|
|
|
|
|
var points = _.chain(this.state.weeks)
|
|
|
|
.map(function(items, week) {
|
|
|
|
var values = _.map(rtop, function(item) {
|
|
|
|
return items[item] || 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
var sum = 0;
|
2015-03-11 20:18:14 +00:00
|
|
|
// console.log('----------');
|
2015-03-11 12:10:11 +00:00
|
|
|
var points = _.map(values, function(val) {
|
|
|
|
sum += Math.floor(val/max*maxHeight);
|
2015-03-11 20:18:14 +00:00
|
|
|
// console.log(val, max, maxHeight, sum);
|
2015-03-11 12:10:11 +00:00
|
|
|
return sum;
|
|
|
|
});
|
|
|
|
|
|
|
|
return [week, points];
|
|
|
|
})
|
|
|
|
.sort(0)
|
2015-03-12 13:11:52 +00:00
|
|
|
.reverse()
|
|
|
|
.take(this.maxWeeks)
|
|
|
|
.reverse()
|
2015-03-11 12:10:11 +00:00
|
|
|
.value();
|
|
|
|
|
|
|
|
var paths = _.reduce(rtop, function(res, item, i) {
|
|
|
|
res[item] = _.map(points, function(pair) {
|
|
|
|
return pair[1][i];
|
2015-03-12 13:11:52 +00:00
|
|
|
});
|
2015-03-11 12:10:11 +00:00
|
|
|
return res;
|
|
|
|
}, {});
|
2015-03-12 13:11:52 +00:00
|
|
|
var paths = _.map(rtop, function(item, i) {
|
|
|
|
var itemPoints = _.map(points, function(pair) {
|
|
|
|
return pair[1][i];
|
|
|
|
});
|
|
|
|
return[item, itemPoints];
|
|
|
|
});
|
2015-03-11 12:10:11 +00:00
|
|
|
|
|
|
|
var colors = {}
|
2015-03-12 13:11:52 +00:00
|
|
|
// console.log('----- Areas!');
|
|
|
|
var areas = _.map(paths, function(pair, i) {
|
|
|
|
var item = pair[0], path = pair[1];
|
2015-03-11 12:10:11 +00:00
|
|
|
colors[item] = Colors2[i];
|
2015-03-12 13:11:52 +00:00
|
|
|
// console.log("Building path for", item, path);
|
|
|
|
// console.log('Area', i);
|
2015-03-11 12:10:11 +00:00
|
|
|
return (
|
2015-03-12 13:11:52 +00:00
|
|
|
<StackedArea key={'area-'+ i}
|
2015-03-11 12:10:11 +00:00
|
|
|
item={item}
|
2015-03-12 13:11:52 +00:00
|
|
|
d={roundPathCorners(this.buildPathD(path), 5)}
|
|
|
|
color={colors[item]} />
|
2015-03-11 12:10:11 +00:00
|
|
|
);
|
|
|
|
}.bind(this));
|
2015-03-12 13:11:52 +00:00
|
|
|
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(
|
|
|
|
<StackedArea key={'area-'+ i}
|
|
|
|
item={''}
|
|
|
|
d={d}
|
|
|
|
color="rgba(0, 0, 0, 0)" />
|
|
|
|
);
|
|
|
|
};
|
2015-03-11 12:10:11 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="sachart-container">
|
|
|
|
<div className="filters">
|
|
|
|
<Selector thing="item"
|
|
|
|
items={this.props.items}
|
|
|
|
value={this.state.item}
|
|
|
|
onChange={this.handleFilter.bind(this, 'item')} />
|
|
|
|
<Selector thing="sort"
|
|
|
|
items={['commits']}
|
|
|
|
value={'commits'} />
|
|
|
|
</div>
|
2015-03-12 13:11:52 +00:00
|
|
|
<svg ref="svg" className="sachart" key="sachart-svg"
|
2015-03-11 12:10:11 +00:00
|
|
|
width="100%" height={maxHeight}
|
|
|
|
viewBox={"0 0 "+ this.state.canvasWidth + " "+ maxHeight}>
|
2015-03-12 13:11:52 +00:00
|
|
|
{areas}
|
2015-03-11 12:10:11 +00:00
|
|
|
</svg>
|
|
|
|
<ul className="legend">
|
|
|
|
{_.pairs(colors).map(function(pair){
|
|
|
|
return (
|
2015-03-11 20:18:14 +00:00
|
|
|
<li key={'legend-'+ pair[0]}>
|
|
|
|
<div className="color-dot" style={{backgroundColor: pair[1]}}></div>
|
|
|
|
{pair[0]}
|
|
|
|
</li>
|
2015-03-11 12:10:11 +00:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var StackedArea = React.createClass({
|
2015-03-12 13:11:52 +00:00
|
|
|
mixins: [Chart],
|
|
|
|
easing: '0.55, 0.055, 0.675, 0.19',
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {lastd: ''};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
|
|
|
// console.log("-- mounted area");
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
// console.log("New area props!", newProps.item);
|
|
|
|
this.setState({
|
|
|
|
lastd: this.props.d,
|
|
|
|
}, this.state.lastd === '' ? null : this.animateAll);
|
|
|
|
},
|
|
|
|
|
|
|
|
animateAll: function() {
|
|
|
|
// console.log("Animating area", this.props.item);
|
|
|
|
this.animate(this.refs.path, 'd', this.state.lastd, this.props.d);
|
|
|
|
},
|
|
|
|
|
2015-03-11 12:10:11 +00:00
|
|
|
render: function() {
|
|
|
|
return (
|
2015-03-12 13:11:52 +00:00
|
|
|
<path ref="path"
|
|
|
|
d={this.props.d}
|
2015-03-11 20:18:14 +00:00
|
|
|
fill={this.props.color}
|
|
|
|
shapeRendering="optimizeQuality" />
|
2015-03-11 12:10:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|