diff --git a/README.md b/README.md index 8c2c81169..ce1a3bf01 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,20 @@ import {Doughnut} from 'react-chartjs-2'; * redraw: PropTypes.bool, * width: PropTypes.number +### Chart instance +Chart instance can be accessed by placing a ref to the element as: + +``` +render() { + componentDidMount() { + console.log(this.refs.chart.chart_instance); // returns a Chart.js instance reference + } + return ( + + ) +} +``` + ### Events #### onElementsClick (function) diff --git a/dist/react-chartjs-2.js b/dist/react-chartjs-2.js index e8e00e9cc..37648269a 100644 --- a/dist/react-chartjs-2.js +++ b/dist/react-chartjs-2.js @@ -6,18 +6,18 @@ 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; }; })(); -exports.Doughnut = Doughnut; -exports.Pie = Pie; -exports.Line = Line; -exports.Bar = Bar; -exports.HorizontalBar = HorizontalBar; -exports.Radar = Radar; -exports.Polar = Polar; +var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; + +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; }; 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 _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 _react = (typeof window !== "undefined" ? window['React'] : typeof global !== "undefined" ? global['React'] : null); var _react2 = _interopRequireDefault(_react); @@ -156,33 +156,201 @@ var ChartComponent = _react2['default'].createClass({ exports['default'] = ChartComponent; -function Doughnut(props) { - return _react2['default'].createElement(ChartComponent, _extends({}, props, { type: 'doughnut' })); -} +var Doughnut = (function (_React$Component) { + _inherits(Doughnut, _React$Component); + + function Doughnut() { + _classCallCheck(this, Doughnut); + + _get(Object.getPrototypeOf(Doughnut.prototype), 'constructor', this).apply(this, arguments); + } + + _createClass(Doughnut, [{ + key: 'render', + value: function render() { + var _this = this; + + return _react2['default'].createElement(ChartComponent, _extends({}, this.props, { + ref: function (ref) { + return _this.chart_instance = ref && ref.chart_instance; + }, + type: 'doughnut' + })); + } + }]); + + return Doughnut; +})(_react2['default'].Component); + +exports.Doughnut = Doughnut; + +var Pie = (function (_React$Component2) { + _inherits(Pie, _React$Component2); + + function Pie() { + _classCallCheck(this, Pie); + + _get(Object.getPrototypeOf(Pie.prototype), 'constructor', this).apply(this, arguments); + } + + _createClass(Pie, [{ + key: 'render', + value: function render() { + var _this2 = this; + + return _react2['default'].createElement(ChartComponent, _extends({}, this.props, { + ref: function (ref) { + return _this2.chart_instance = ref && ref.chart_instance; + }, + type: 'pie' + })); + } + }]); + + return Pie; +})(_react2['default'].Component); + +exports.Pie = Pie; + +var Line = (function (_React$Component3) { + _inherits(Line, _React$Component3); + + function Line() { + _classCallCheck(this, Line); + + _get(Object.getPrototypeOf(Line.prototype), 'constructor', this).apply(this, arguments); + } + + _createClass(Line, [{ + key: 'render', + value: function render() { + var _this3 = this; + + return _react2['default'].createElement(ChartComponent, _extends({}, this.props, { + ref: function (ref) { + return _this3.chart_instance = ref && ref.chart_instance; + }, + type: 'line' + })); + } + }]); + + return Line; +})(_react2['default'].Component); + +exports.Line = Line; + +var Bar = (function (_React$Component4) { + _inherits(Bar, _React$Component4); + + function Bar() { + _classCallCheck(this, Bar); + + _get(Object.getPrototypeOf(Bar.prototype), 'constructor', this).apply(this, arguments); + } + + _createClass(Bar, [{ + key: 'render', + value: function render() { + var _this4 = this; + + return _react2['default'].createElement(ChartComponent, _extends({}, this.props, { + ref: function (ref) { + return _this4.chart_instance = ref && ref.chart_instance; + }, + type: 'bar' + })); + } + }]); + + return Bar; +})(_react2['default'].Component); + +exports.Bar = Bar; + +var HorizontalBar = (function (_React$Component5) { + _inherits(HorizontalBar, _React$Component5); + + function HorizontalBar() { + _classCallCheck(this, HorizontalBar); + + _get(Object.getPrototypeOf(HorizontalBar.prototype), 'constructor', this).apply(this, arguments); + } -function Pie(props) { - return _react2['default'].createElement(ChartComponent, _extends({}, props, { type: 'pie' })); -} + _createClass(HorizontalBar, [{ + key: 'render', + value: function render() { + var _this5 = this; + + return _react2['default'].createElement(ChartComponent, _extends({}, this.props, { + ref: function (ref) { + return _this5.chart_instance = ref && ref.chart_instance; + }, + type: 'horizontalBar' + })); + } + }]); + + return HorizontalBar; +})(_react2['default'].Component); -function Line(props) { - return _react2['default'].createElement(ChartComponent, _extends({}, props, { type: 'line' })); -} +exports.HorizontalBar = HorizontalBar; -function Bar(props) { - return _react2['default'].createElement(ChartComponent, _extends({}, props, { type: 'bar' })); -} +var Radar = (function (_React$Component6) { + _inherits(Radar, _React$Component6); -function HorizontalBar(props) { - return _react2['default'].createElement(ChartComponent, _extends({}, props, { type: 'horizontalBar' })); -} + function Radar() { + _classCallCheck(this, Radar); + + _get(Object.getPrototypeOf(Radar.prototype), 'constructor', this).apply(this, arguments); + } -function Radar(props) { - return _react2['default'].createElement(ChartComponent, _extends({}, props, { type: 'radar' })); -} + _createClass(Radar, [{ + key: 'render', + value: function render() { + var _this6 = this; + + return _react2['default'].createElement(ChartComponent, _extends({}, this.props, { + ref: function (ref) { + return _this6.chart_instance = ref && ref.chart_instance; + }, + type: 'radar' + })); + } + }]); + + return Radar; +})(_react2['default'].Component); + +exports.Radar = Radar; -function Polar(props) { - return _react2['default'].createElement(ChartComponent, _extends({}, props, { type: 'polarArea' })); -} +var Polar = (function (_React$Component7) { + _inherits(Polar, _React$Component7); + + function Polar() { + _classCallCheck(this, Polar); + + _get(Object.getPrototypeOf(Polar.prototype), 'constructor', this).apply(this, arguments); + } + + _createClass(Polar, [{ + key: 'render', + value: function render() { + var _this7 = this; + + return _react2['default'].createElement(ChartComponent, _extends({}, this.props, { + ref: function (ref) { + return _this7.chart_instance = ref && ref.chart_instance; + }, + type: 'polarArea' + })); + } + }]); + + return Polar; +})(_react2['default'].Component); + +exports.Polar = Polar; }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) },{"./utils/deepEqual":2,"chart.js":undefined,"react-dom":undefined}],2:[function(require,module,exports){ diff --git a/dist/react-chartjs-2.min.js b/dist/react-chartjs-2.min.js index 7cb567171..6105fc7eb 100644 --- a/dist/react-chartjs-2.min.js +++ b/dist/react-chartjs-2.min.js @@ -1 +1 @@ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.Chart=e()}}(function(){return function e(t,n,r){function o(a,u){if(!n[a]){if(!t[a]){var f="function"==typeof require&&require;if(!u&&f)return f(a,!0);if(i)return i(a,!0);var c=new Error("Cannot find module '"+a+"'");throw c.code="MODULE_NOT_FOUND",c}var d=n[a]={exports:{}};t[a][0].call(d.exports,function(e){var n=t[a][1][e];return o(n?n:e)},d,d.exports,e,t,n,r)}return n[a].exports}for(var i="function"==typeof require&&require,a=0;a=0||Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r]);return n},shouldComponentUpdate:function(e,t){var n=this._objectWithoutProperties(e,["id","width","height"]),r=this._objectWithoutProperties(this.props,["id","width","height"]);return!(0,w["default"])(n,r,{strict:!0})},componentWillUnmount:function(){this.chart_instance.destroy()},updateChart:function(){var e=this.props,t=e.data,n=e.options;this.chart_instance&&(n&&(this.chart_instance.options=v["default"].helpers.configMerge(this.chart_instance.options,n)),this.chart_instance.config.data=s({},this.chart_instance.config.data,t),this.chart_instance.update())},renderChart:function(){var e=this.props,t=e.data,n=e.options,r=(e.legend,e.type),o=y["default"].findDOMNode(this);this.chart_instance=new v["default"](o,{type:r,data:t,options:n})},handleOnClick:function(e){var t=this.chart_instance.getElementsAtEvent(e);if(t.length){var n=this.props.onElementsClick;n(t)}},render:function(){var e=this.props,t=e.height,n=e.width,r=e.onElementsClick;return l["default"].createElement("canvas",{height:t,width:n,onClick:"function"==typeof r?this.handleOnClick:null})}});n["default"]=b}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./utils/deepEqual":2,"chart.js":void 0,"react-dom":void 0}],2:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=Object.prototype.hasOwnProperty,o=function(e,t){return e===t?0!==e||1/e===1/t:e!==e&&t!==t},i=function a(e,t){if(o(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;for(var n=Object.keys(e),i=0;i=0||Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r]);return n},shouldComponentUpdate:function(t,e){var n=this._objectWithoutProperties(t,["id","width","height"]),r=this._objectWithoutProperties(this.props,["id","width","height"]);return!(0,v["default"])(n,r,{strict:!0})},componentWillUnmount:function(){this.chart_instance.destroy()},updateChart:function(){var t=this.props,e=t.data,n=t.options;this.chart_instance&&(n&&(this.chart_instance.options=d["default"].helpers.configMerge(this.chart_instance.options,n)),this.chart_instance.config.data=c({},this.chart_instance.config.data,e),this.chart_instance.update())},renderChart:function(){var t=this.props,e=t.data,n=t.options,r=(t.legend,t.type),o=l["default"].findDOMNode(this);this.chart_instance=new d["default"](o,{type:r,data:e,options:n})},handleOnClick:function(t){var e=this.chart_instance.getElementsAtEvent(t);if(e.length){var n=this.props.onElementsClick;n(e)}},render:function(){var t=this.props,e=t.height,n=t.width,r=t.onElementsClick;return s["default"].createElement("canvas",{height:e,width:n,onClick:"function"==typeof r?this.handleOnClick:null})}});n["default"]=b;var O=function(t){function e(){o(this,e),u(Object.getPrototypeOf(e.prototype),"constructor",this).apply(this,arguments)}return i(e,t),a(e,[{key:"render",value:function(){var t=this;return s["default"].createElement(b,c({},this.props,{ref:function(e){return t.chart_instance=e&&e.chart_instance},type:"doughnut"}))}}]),e}(s["default"].Component);n.Doughnut=O;var g=function(t){function e(){o(this,e),u(Object.getPrototypeOf(e.prototype),"constructor",this).apply(this,arguments)}return i(e,t),a(e,[{key:"render",value:function(){var t=this;return s["default"].createElement(b,c({},this.props,{ref:function(e){return t.chart_instance=e&&e.chart_instance},type:"pie"}))}}]),e}(s["default"].Component);n.Pie=g;var m=function(t){function e(){o(this,e),u(Object.getPrototypeOf(e.prototype),"constructor",this).apply(this,arguments)}return i(e,t),a(e,[{key:"render",value:function(){var t=this;return s["default"].createElement(b,c({},this.props,{ref:function(e){return t.chart_instance=e&&e.chart_instance},type:"line"}))}}]),e}(s["default"].Component);n.Line=m;var _=function(t){function e(){o(this,e),u(Object.getPrototypeOf(e.prototype),"constructor",this).apply(this,arguments)}return i(e,t),a(e,[{key:"render",value:function(){var t=this;return s["default"].createElement(b,c({},this.props,{ref:function(e){return t.chart_instance=e&&e.chart_instance},type:"bar"}))}}]),e}(s["default"].Component);n.Bar=_;var w=function(t){function e(){o(this,e),u(Object.getPrototypeOf(e.prototype),"constructor",this).apply(this,arguments)}return i(e,t),a(e,[{key:"render",value:function(){var t=this;return s["default"].createElement(b,c({},this.props,{ref:function(e){return t.chart_instance=e&&e.chart_instance},type:"horizontalBar"}))}}]),e}(s["default"].Component);n.HorizontalBar=w;var P=function(t){function e(){o(this,e),u(Object.getPrototypeOf(e.prototype),"constructor",this).apply(this,arguments)}return i(e,t),a(e,[{key:"render",value:function(){var t=this;return s["default"].createElement(b,c({},this.props,{ref:function(e){return t.chart_instance=e&&e.chart_instance},type:"radar"}))}}]),e}(s["default"].Component);n.Radar=P;var j=function(t){function e(){o(this,e),u(Object.getPrototypeOf(e.prototype),"constructor",this).apply(this,arguments)}return i(e,t),a(e,[{key:"render",value:function(){var t=this;return s["default"].createElement(b,c({},this.props,{ref:function(e){return t.chart_instance=e&&e.chart_instance},type:"polarArea"}))}}]),e}(s["default"].Component);n.Polar=j}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./utils/deepEqual":2,"chart.js":void 0,"react-dom":void 0}],2:[function(t,e,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var r=Object.prototype.hasOwnProperty,o=function(t,e){return t===e?0!==t||1/t===1/e:t!==t&&e!==e},i=function a(t,e){if(o(t,e))return!0;if("object"!=typeof t||null===t||"object"!=typeof e||null===e)return!1;for(var n=Object.keys(t),i=0;i; +export class Doughnut extends React.Component { + render() { + return ( + this.chart_instance = ref && ref.chart_instance} + type='doughnut' + /> + ); + } } -export function Pie (props) { - return ; +export class Pie extends React.Component { + render() { + return ( + this.chart_instance = ref && ref.chart_instance} + type='pie' + /> + ); + } } -export function Line (props) { - return ; +export class Line extends React.Component { + render() { + return ( + this.chart_instance = ref && ref.chart_instance} + type='line' + /> + ); + } } -export function Bar (props) { - return ; +export class Bar extends React.Component { + render() { + return ( + this.chart_instance = ref && ref.chart_instance} + type='bar' + /> + ); + } } -export function HorizontalBar (props) { - return ; +export class HorizontalBar extends React.Component { + render() { + return ( + this.chart_instance = ref && ref.chart_instance} + type='horizontalBar' + /> + ); + } } -export function Radar (props) { - return ; +export class Radar extends React.Component { + render() { + return ( + this.chart_instance = ref && ref.chart_instance} + type='radar' + /> + ); + } } -export function Polar (props) { - return ; +export class Polar extends React.Component { + render() { + return ( + this.chart_instance = ref && ref.chart_instance} + type='polarArea' + /> + ); + } }