wui-bigdata
Version:
Big data visualization library for WUI framework.
166 lines (146 loc) • 6.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _d = require('d3');
var _d2 = _interopRequireDefault(_d);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var BarChart = function (_Component) {
_inherits(BarChart, _Component);
function BarChart() {
_classCallCheck(this, BarChart);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(BarChart).call(this));
_this.state = {
data: [{ value: 50 }, { value: 43 }, { value: 120 }, { value: 87 }, { value: 99 }, { value: 167 }, { value: 142 }]
};
return _this;
}
_createClass(BarChart, [{
key: 'sortItems',
value: function sortItems() {
this.setState({
data: this.state.data.sort(function (a, b) {
return a.value - b.value;
})
});
}
}, {
key: 'addItem',
value: function addItem() {
this.state.data.push({ value: Math.floor(Math.random() * 100) });
this.forceUpdate();
}
}, {
key: 'mouseEnter',
value: function mouseEnter(index) {
this.state.data[index].fill = 'yellow';
this.forceUpdate();
}
}, {
key: 'mouseLeave',
value: function mouseLeave(index) {
this.state.data[index].fill = 'steelblue';
this.forceUpdate();
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
_d2.default.select(this.refs.xAxis).call(this.xAxis);
_d2.default.select(this.refs.yAxis).call(this.yAxis);
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate() {
_d2.default.select(this.refs.xAxis).call(this.xAxis);
_d2.default.select(this.refs.yAxis).call(this.yAxis);
}
}, {
key: 'render',
value: function render() {
var _this2 = this;
var width = 600;
var height = 600;
var padding = {
left: 20,
right: 20,
top: 20,
bottom: 20
};
var xAxisWidth = 500;
var yAxisWidth = 500;
var xScale = _d2.default.scale.ordinal().domain(_d2.default.range(this.state.data.length)).rangeRoundBands([0, xAxisWidth], 0.2);
var yScale = _d2.default.scale.linear().domain([0, _d2.default.max(this.state.data, function (d) {
return d.value;
})]).range([0, yAxisWidth]);
var rectData = this.state.data.map(function (item, index) {
return {
fill: item.fill || 'steelblue',
x: padding.left + xScale(index),
y: height - padding.bottom - yScale(item.value),
width: xScale.rangeBand(),
height: yScale(item.value)
};
});
var textData = this.state.data.map(function (item, index) {
return {
fill: 'white',
fontSize: '14px',
textAnchor: 'middle',
x: padding.left + xScale(index),
y: height - padding.bottom - yScale(item.value),
dx: xScale.rangeBand() / 2,
dy: '1em',
value: item.value
};
});
this.xAxis = _d2.default.svg.axis().scale(xScale).orient('bottom');
yScale.range([yAxisWidth, 0]);
this.yAxis = _d2.default.svg.axis().scale(yScale).orient('left');
return _react2.default.createElement(
'div',
null,
_react2.default.createElement(
'svg',
{ height: height, width: width },
rectData.map(function (item, index) {
return _react2.default.createElement('rect', _extends({ onMouseEnter: _this2.mouseEnter.bind(_this2, index),
onMouseLeave: _this2.mouseLeave.bind(_this2, index),
key: index }, item));
}),
textData.map(function (item, index) {
return _react2.default.createElement(
'text',
_extends({ key: index }, item),
item.value
);
}),
_react2.default.createElement('g', { ref: 'xAxis',
className: 'axis',
transform: 'translate(' + padding.left + ',' + (height - padding.bottom) + ')' }),
_react2.default.createElement('g', { ref: 'yAxis',
className: 'axis',
transform: 'translate(' + padding.left + ',' + (height - padding.top - yAxisWidth) + ')' })
),
_react2.default.createElement(
'button',
{ onClick: this.sortItems.bind(this) },
'Sort'
),
_react2.default.createElement(
'button',
{ onClick: this.addItem.bind(this) },
'Add'
)
);
}
}]);
return BarChart;
}(_react.Component);
exports.default = BarChart;