2015-03-11 12:10:11 +00:00
|
|
|
var StackedAreaChart = React.createClass({
|
2015-03-13 12:08:13 +00:00
|
|
|
mixins: [ReactRouter.Navigation, ReactRouter.State, SVGChartMixin, ChartDataMixin],
|
2015-03-11 12:10:11 +00:00
|
|
|
|
|
|
|
numElements: 10,
|
2015-03-15 12:10:02 +00:00
|
|
|
maxWeeks: 30,
|
2015-03-14 15:53:57 +00:00
|
|
|
height: 350,
|
2015-03-11 12:10:11 +00:00
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
item: this.props.items[0],
|
|
|
|
rawData: [],
|
|
|
|
top: [],
|
|
|
|
weeks: [],
|
2015-03-13 12:08:13 +00:00
|
|
|
max: 1
|
2015-03-11 12:10:11 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-03-11 20:21:28 +00:00
|
|
|
componentDidMount: function() {
|
|
|
|
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],
|
|
|
|
state: 'newProps'
|
2015-03-11 20:18:14 +00:00
|
|
|
}, this.fetchData);
|
|
|
|
},
|
|
|
|
|
2015-03-12 13:11:52 +00:00
|
|
|
shouldComponentUpdate: function(newProps, newState) {
|
2015-03-13 12:08:13 +00:00
|
|
|
if (!newState.canvasWidth) {
|
2015-03-12 13:11:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (newState.state !== 'newPoints') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2015-03-13 12:08:13 +00:00
|
|
|
handleNewData: function() {
|
2015-03-11 12:10:11 +00:00
|
|
|
// Group commits by items
|
2015-03-12 13:45:29 +00:00
|
|
|
var weeksList = _.chain(this.state.rawData)
|
|
|
|
.pluck('week')
|
|
|
|
.uniq()
|
|
|
|
.sort()
|
|
|
|
.reverse()
|
|
|
|
.take(this.maxWeeks)
|
|
|
|
.value();
|
|
|
|
|
2015-03-11 12:10:11 +00:00
|
|
|
var counts = _.reduce(this.state.rawData, function(res, el) {
|
2015-03-12 13:45:29 +00:00
|
|
|
if (weeksList.indexOf(el.week) === -1) {
|
|
|
|
return res;
|
|
|
|
}
|
2015-03-11 12:10:11 +00:00
|
|
|
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();
|
2015-03-12 13:45:29 +00:00
|
|
|
for (var i = top.length; i < this.numElements; i++) {
|
|
|
|
top[i] = null;
|
|
|
|
};
|
2015-03-11 12:10:11 +00:00
|
|
|
|
|
|
|
var weeks = _.reduce(this.state.rawData, function(res, el) {
|
2015-03-12 13:45:29 +00:00
|
|
|
if (weeksList.indexOf(el.week) === -1) {
|
|
|
|
return res;
|
|
|
|
}
|
2015-03-11 12:10:11 +00:00
|
|
|
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-12 13:45:29 +00:00
|
|
|
var max = _.max(_.map(weeksList, function(week) {
|
|
|
|
return _.sum(_.values(weeks[week]));
|
|
|
|
}));
|
|
|
|
|
2015-03-11 12:10:11 +00:00
|
|
|
this.setState({
|
2015-03-16 09:24:13 +00:00
|
|
|
top: top,
|
2015-03-12 13:11:52 +00:00
|
|
|
weeks: weeks,
|
2015-03-13 12:08:13 +00:00
|
|
|
max: max,
|
2015-03-12 13:11:52 +00:00
|
|
|
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;
|
2015-03-13 12:08:13 +00:00
|
|
|
|
2015-03-11 12:10:11 +00:00
|
|
|
var d = _.map(points, function(point, i) {
|
2015-03-13 12:08:13 +00:00
|
|
|
return 'L'+ Math.floor(i/(len-1)*maxWidth) +','+ Math.floor(maxHeight - point);
|
|
|
|
});
|
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() {
|
|
|
|
var maxWidth = this.state.canvasWidth,
|
|
|
|
maxHeight = this.height,
|
2015-03-16 09:24:13 +00:00
|
|
|
top = this.state.top,
|
2015-03-11 12:10:11 +00:00
|
|
|
max = this.state.max;
|
|
|
|
|
|
|
|
var points = _.chain(this.state.weeks)
|
|
|
|
.map(function(items, week) {
|
2015-03-16 09:24:13 +00:00
|
|
|
var values = _.map(top, function(item) {
|
2015-03-11 12:10:11 +00:00
|
|
|
return items[item] || 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
var sum = 0;
|
|
|
|
var points = _.map(values, function(val) {
|
|
|
|
sum += Math.floor(val/max*maxHeight);
|
|
|
|
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();
|
|
|
|
|
2015-03-16 09:24:13 +00:00
|
|
|
var paths = _.map(top, function(item, i) {
|
2015-03-12 13:11:52 +00:00
|
|
|
var itemPoints = _.map(points, function(pair) {
|
|
|
|
return pair[1][i];
|
|
|
|
});
|
|
|
|
return[item, itemPoints];
|
|
|
|
});
|
2015-03-11 12:10:11 +00:00
|
|
|
|
2015-03-12 13:45:29 +00:00
|
|
|
var colors = {};
|
2015-03-12 13:11:52 +00:00
|
|
|
var areas = _.map(paths, function(pair, i) {
|
|
|
|
var item = pair[0], path = pair[1];
|
2015-03-12 13:45:29 +00:00
|
|
|
if (item !== null) {
|
2015-03-16 07:30:06 +00:00
|
|
|
colors[item] = Colors[i];
|
2015-03-12 13:45:29 +00:00
|
|
|
}
|
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-16 07:30:06 +00:00
|
|
|
d={roundPathCorners(this.buildPathD(path), 3)}
|
|
|
|
color={Colors[i]} />
|
2015-03-11 12:10:11 +00:00
|
|
|
);
|
|
|
|
}.bind(this));
|
|
|
|
|
2015-03-14 15:53:57 +00:00
|
|
|
var words = {
|
|
|
|
items: {
|
|
|
|
repo: 'repositories',
|
|
|
|
team: 'teams',
|
|
|
|
user: 'contributors'
|
|
|
|
},
|
|
|
|
item: {
|
|
|
|
repo: 'repository',
|
2015-03-14 18:59:52 +00:00
|
|
|
team: 'team'
|
2015-03-14 15:53:57 +00:00
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
repo: 'which were the most attended by',
|
|
|
|
team: 'which were the most active working on',
|
|
|
|
user: 'which were the most active working on'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
who = this.getParams().repo || this.getParams().team || this.getParams().user || this.getParams().org;
|
|
|
|
|
|
|
|
var params = Object.keys(this.getParams());
|
|
|
|
params.splice(params.indexOf('org'), 1);
|
|
|
|
var subject = params[0];
|
|
|
|
|
2015-03-11 12:10:11 +00:00
|
|
|
return (
|
|
|
|
<div className="sachart-container">
|
2015-03-14 15:53:57 +00:00
|
|
|
<div className="whatsgoingon">
|
2015-03-16 11:20:32 +00:00
|
|
|
This stacked area chart represents <em>{words.items[this.state.item]}</em> {words.actions[this.state.item]} <em>{who}</em> {words.item[subject]} <WeekIntervalSelector />
|
2015-03-14 15:53:57 +00:00
|
|
|
</div>
|
2015-03-11 12:10:11 +00:00
|
|
|
<div className="filters">
|
2015-03-16 08:36:15 +00:00
|
|
|
<Selector thing="sort"
|
|
|
|
title="Show"
|
|
|
|
items={['commits']}
|
|
|
|
value={'commits'} />
|
2015-03-11 12:10:11 +00:00
|
|
|
<Selector thing="item"
|
2015-03-16 08:36:15 +00:00
|
|
|
title="Grouped by"
|
2015-03-11 12:10:11 +00:00
|
|
|
items={this.props.items}
|
|
|
|
value={this.state.item}
|
|
|
|
onChange={this.handleFilter.bind(this, 'item')} />
|
|
|
|
</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}
|
2015-03-13 12:08:13 +00:00
|
|
|
viewBox={"0 0 "+ (this.state.canvasWidth || 0) + " "+ maxHeight}>
|
2015-03-12 13:45:29 +00:00
|
|
|
{areas.reverse()}
|
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-13 12:08:13 +00:00
|
|
|
mixins: [ChartAnimationMixin],
|
2015-03-12 13:11:52 +00:00
|
|
|
|
|
|
|
getInitialState: function() {
|
2015-03-13 12:08:13 +00:00
|
|
|
return {};
|
2015-03-12 13:11:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
this.setState({
|
2015-03-13 12:08:13 +00:00
|
|
|
lastd: this.props.d || newProps.d,
|
|
|
|
}, this.animateAll);
|
2015-03-12 13:11:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
animateAll: function() {
|
2015-03-13 12:08:13 +00:00
|
|
|
this.clearAnimations(this.refs.path);
|
2015-03-12 13:11:52 +00:00
|
|
|
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"
|
2015-03-13 12:08:13 +00:00
|
|
|
d={this.state.lastd || 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
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|