react-bulma-chartjs
Version:
Chartjs component is based on chart.js for React
142 lines (117 loc) • 5.58 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 _lodash = require('lodash');
var _chart = require('chart.js');
var _chart2 = _interopRequireDefault(_chart);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
require('../src/chat.css');
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 types = ['line', 'bar', 'radar', 'polarArea', 'pie', 'doughnut', 'bubble', 'scatter'];
var index = function (_React$Component) {
_inherits(index, _React$Component);
function index() {
_classCallCheck(this, index);
return _possibleConstructorReturn(this, (index.__proto__ || Object.getPrototypeOf(index)).apply(this, arguments));
}
_createClass(index, [{
key: 'componentWillMount',
value: function componentWillMount() {
this.chart = null;
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
this.onRenderChart();
}
}, {
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState) {
var _props = this.props,
type = _props.type,
data = _props.data,
options = _props.options,
plugins = _props.plugins,
height = _props.height,
width = _props.width;
if (height !== nextProps.height || width !== nextProps.width || type !== nextProps.type || !(0, _lodash.isEqual)(nextProps.data, data) || !(0, _lodash.isEqual)(nextProps.plugins, plugins) || !(0, _lodash.isEqual)(nextProps.options, options)) {
return true;
} else {
return false;
}
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
if (!(0, _lodash.isEqual)(prevProps.data, this.props.data)) {
var transform = function transform(datasets, nextDatasets) {
datasets.splice(nextDatasets.length);
nextDatasets.forEach(function (vo, key) {
if (datasets[key] && datasets[key] instanceof Object) {
datasets[key] = _extends({}, datasets[key], nextDatasets[key]);
} else {
datasets[key] = nextDatasets[key];
}
});
return datasets;
};
this.chart.config.data = _extends({}, this.chart.config.data, {
labels: transform(this.chart.config.data.labels, this.props.data.labels),
datasets: transform(this.chart.config.data.datasets, this.props.data.datasets)
});
this.chart.update();
} else {
this.onRenderChart();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
this.chart.destroy();
}
}, {
key: 'onRenderChart',
value: function onRenderChart() {
if (this.chart) this.chart.destroy();
if (!types.includes(this.props.type)) return;
this.chart = new _chart2.default(this.refs.canvas.getContext('2d'), _extends({}, this.props));
}
}, {
key: 'render',
value: function render() {
return _react2.default.createElement('canvas', {
ref: 'canvas',
className: 'chartjs',
width: this.props.width,
height: this.props.height
});
}
}]);
return index;
}(_react2.default.Component);
index.propTypes = {
data: _propTypes2.default.object.isRequired,
height: _propTypes2.default.number,
width: _propTypes2.default.number,
options: _propTypes2.default.object,
plugins: _propTypes2.default.arrayOf(_propTypes2.default.object, _propTypes2.default.func),
type: _propTypes2.default.oneOf(types)
};
index.defaultProps = {
type: 'doughnut',
height: 300,
width: 300,
data: {},
options: {},
plugins: [{}]
};
exports.default = index;