1
0
Fork 0
empact/app/scripts/charts.js

48 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-03-08 08:10:40 +00:00
var BarChart = React.createClass({displayName: "BarChart",
2015-03-07 17:22:32 +00:00
barHeight: 40,
barMargin: 5,
getInitialState: function() {
return {points: []};
},
componentDidMount: function() {
$.get(this.props.url, function(res){
this.setState({points: res});
}.bind(this))
},
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() {
var renderPoint = function(point, i) {
return (
2015-03-08 08:10:40 +00:00
React.createElement("g", {key: point.item},
React.createElement("rect", {
fill: colorFor(point.item),
width: point.value,
height: this.barHeight,
x: "0",
y: this.y(i)}),
React.createElement("text", {x: "20", y: this.y(i) + 25}, point.item + ": " + point.value)
)
2015-03-07 17:22:32 +00:00
);
}.bind(this)
return (
2015-03-08 08:10:40 +00:00
React.createElement("svg", {className: "bar_chart", width: "100%", height: this.height()},
this.state.points.map(renderPoint)
)
2015-03-07 17:22:32 +00:00
);
}
});