wui-bigdata
Version:
Big data visualization library for WUI framework.
140 lines (121 loc) • 6.13 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
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 StackDiagram = function (_Component) {
_inherits(StackDiagram, _Component);
function StackDiagram() {
_classCallCheck(this, StackDiagram);
return _possibleConstructorReturn(this, Object.getPrototypeOf(StackDiagram).apply(this, arguments));
}
_createClass(StackDiagram, [{
key: 'render',
value: function render() {
var dataset = [{
name: 'PC',
sales: [{ year: 2005, profit: 3000 }, { year: 2006, profit: 1300 }, { year: 2007, profit: 3700 }, { year: 2008, profit: 4900 }, { year: 2009, profit: 700 }]
}, {
name: 'SmartPhone',
sales: [{ year: 2005, profit: 2000 }, { year: 2006, profit: 4000 }, { year: 2007, profit: 1810 }, { year: 2008, profit: 6540 }, { year: 2009, profit: 2820 }]
}, {
name: 'Software',
sales: [{ year: 2005, profit: 1100 }, { year: 2006, profit: 1700 }, { year: 2007, profit: 1680 }, { year: 2008, profit: 4000 }, { year: 2009, profit: 4900 }]
}];
var width = 600;
var height = 600;
var stack = _d2.default.layout.stack().values(function (d) {
return d.sales;
}).x(function (d) {
return d.year;
}).y(function (d) {
return d.profit;
});
var data = stack(dataset);
var padding = { top: 30, right: 100, bottom: 30, left: 50 };
var xRangeWidth = width - padding.left - padding.right;
var xScale = _d2.default.scale.ordinal().domain(data[0].sales.map(function (d) {
return d.year;
})).rangeBands([0, xRangeWidth], 0.3);
var maxProfit = _d2.default.max(data[data.length - 1].sales, function (d) {
return d.y0 + d.y;
});
var yRangeWidth = height - padding.top - padding.bottom;
var yScale = _d2.default.scale.linear().domain([0, maxProfit]).range([0, yRangeWidth]);
var color = _d2.default.scale.category10();
var labHeight = 50;
var labRadius = 10;
var area = _d2.default.svg.area().x(function (d) {
return xScale(d.year) + xScale.rangeBand() / 2;
}).y0(function (d) {
return yRangeWidth - yScale(d.y0);
}).y1(function (d) {
return yRangeWidth - yScale(d.y0 + d.y);
}).interpolate('basis');
if (this.props.shape === 'rect') {
return _react2.default.createElement(
'svg',
{ width: width, height: height },
data.map(function (d, index) {
return _react2.default.createElement(
'g',
{ key: index,
fill: color(index) },
d.sales.map(function (d, i) {
return _react2.default.createElement('rect', { key: i,
x: xScale(d.year),
y: yRangeWidth - yScale(d.y0 + d.y),
width: xScale.rangeBand(),
height: yScale(d.y),
transform: 'translate(' + padding.left + ', ' + padding.top + ')' });
}),
_react2.default.createElement('circle', { cx: width - padding.right * 0.98,
cy: padding.top * 2 + labHeight * index,
r: labRadius }),
_react2.default.createElement(
'text',
{ x: width - padding.right * 0.8,
y: padding.top * 2 + labHeight * index,
dy: labRadius / 2 },
d.name
)
);
})
);
} else {
return _react2.default.createElement(
'svg',
{ width: width, height: height },
data.map(function (d, index) {
return _react2.default.createElement(
'g',
{ key: index,
fill: color(index) },
_react2.default.createElement('path', { d: area(d.sales) }),
_react2.default.createElement('circle', { cx: width - padding.right * 0.98,
cy: padding.top * 2 + labHeight * index,
r: labRadius }),
_react2.default.createElement(
'text',
{ x: width - padding.right * 0.8,
y: padding.top * 2 + labHeight * index,
dy: labRadius / 2 },
d.name
)
);
})
);
}
}
}]);
return StackDiagram;
}(_react.Component);
exports.default = StackDiagram;