nuke-biz-custom-chart
Version:
custom-chart
174 lines (131 loc) • 5.68 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 _rax = require('rax');
var _weexNuke = require('weex-nuke');
var _g2Mobile = require('@ali/g2-mobile');
var _g2Mobile2 = _interopRequireDefault(_g2Mobile);
var _raxCanvas = require('./rax-canvas');
var _raxCanvas2 = _interopRequireDefault(_raxCanvas);
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 isWeex = _weexNuke.Env.isWeex,
isWeb = _weexNuke.Env.isWeb;
if (isWeb) {
_g2Mobile2.default.Global.pixelRatio = 2;
}
var Chart = function (_Component) {
_inherits(Chart, _Component);
function Chart(props) {
_classCallCheck(this, Chart);
var _this = _possibleConstructorReturn(this, (Chart.__proto__ || Object.getPrototypeOf(Chart)).call(this, props));
_this.margin = 'margin' in props ? props.margin : 20;
_this.setRef = _this.setRef.bind(_this);
return _this;
}
_createClass(Chart, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _this2 = this;
this.chartInstance.getContext().then(function (context) {
// 存储好ctx,不用再init一个canvas画布了
_this2.ctx = context;
_this2.draw(context);
});
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
var _this3 = this;
if (isWeex) {
if (!this.ctx || !this.ctx.clearRect) {
return;
}
var data = nextProps.data,
config = nextProps.config,
style = nextProps.style;
var el = (0, _rax.findDOMNode)(this.chartInstance);
// 擦掉画布
this.ctx.clearRect(0, 0, style.width, style.height);
this.chart = new _g2Mobile2.default.Chart({
el: el,
context: this.ctx,
margin: isWeex ? 2 * this.margin : this.margin
});
this.chart.source(data, config);
this.chart.axis(false);
this.renderChildren(this.ctx);
} else {
// web就这么写吧,不管了
this.chartInstance.getContext().then(function (context) {
_this3.draw(context);
});
}
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate() {
return false;
}
}, {
key: 'setRef',
value: function setRef(ref) {
this.chartInstance = ref;
}
}, {
key: 'draw',
value: function draw(context) {
var _props = this.props,
children = _props.children,
data = _props.data,
config = _props.config;
var el = (0, _rax.findDOMNode)(this.chartInstance);
if (!children) {
return;
}
// context.render();
this.chart = new _g2Mobile2.default.Chart({
el: el,
context: context,
margin: isWeex ? 2 * this.margin : this.margin
});
this.chart.source(data, config);
this.chart.axis(false);
this.renderChildren(context);
}
}, {
key: 'renderChildren',
value: function renderChildren() {
var _this4 = this;
var children = this.props.children;
if (children && !Array.isArray(children)) {
children = [children];
}
children.forEach(function (item) {
if (item.type && item.type.draw) {
item.type.draw(_this4.chart, item.props);
}
});
this.chart.render();
}
}, {
key: 'render',
value: function render() {
var style = this.props.style;
var chartStyle = _extends({}, style);
// weex canvas must have backgroundColor
if (isWeex && !chartStyle.backgroundColor) {
chartStyle.backgroundColor = 'transparent';
}
return (0, _rax.createElement)(_raxCanvas2.default, { style: chartStyle, ref: this.setRef });
}
}]);
return Chart;
}(_rax.Component);
exports.default = Chart;
module.exports = exports['default'];