lucid-ui
Version:
A UI component library from AppNexus.
168 lines (134 loc) • 8.77 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import * as d3Selection from 'd3-selection';
import React from 'react';
import { omitProps } from '../../util/component-types';
import { lucidClassNames } from '../../util/style-helpers';
import DraggableLineChartD3 from './DraggableLineChartD3';
var cx = lucidClassNames.bind('&-DraggableLineChart');
var getEmptyRenderProp = function getEmptyRenderProp(preSelectText) {
return /*#__PURE__*/React.createElement("div", {
style: {
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center'
}
}, /*#__PURE__*/React.createElement("div", {
className: cx('&-empty-info')
}, preSelectText));
};
var getCleanData = function getCleanData(data) {
return data.map(function (_ref) {
var x = _ref.x,
y = _ref.y;
return {
x: x,
y: Number.isFinite(y) ? y : 0
};
});
};
var draggableLineChartDefaultProps = {
height: 315,
width: 1000,
margin: {
top: 65,
right: 80,
bottom: 65,
left: 80
}
};
var DraggableLineChart = /*#__PURE__*/function (_React$Component) {
_inherits(DraggableLineChart, _React$Component);
var _super = _createSuper(DraggableLineChart);
function DraggableLineChart(props) {
var _this;
_classCallCheck(this, DraggableLineChart);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "ref", void 0);
_defineProperty(_assertThisInitialized(_this), "d3LineChart", void 0);
_this.ref = /*#__PURE__*/React.createRef();
_this.d3LineChart = null;
return _this;
}
_createClass(DraggableLineChart, [{
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.d3LineChart.updateLineChart(this.props.data);
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
var svg = d3Selection.select(this.ref);
var _this$props = this.props,
height = _this$props.height,
width = _this$props.width,
margin = _this$props.margin,
data = _this$props.data,
onDragEnd = _this$props.onDragEnd,
xAxisTicksVertical = _this$props.xAxisTicksVertical,
dataIsCentered = _this$props.dataIsCentered,
_this$props$yAxisMin = _this$props.yAxisMin,
yAxisMin = _this$props$yAxisMin === void 0 ? 0 : _this$props$yAxisMin,
xAxisRenderProp = _this$props.xAxisRenderProp,
onPreselect = _this$props.onPreselect,
preSelectText = _this$props.preSelectText;
var emptyRenderProp = onPreselect && preSelectText ? function () {
return getEmptyRenderProp(preSelectText);
} : undefined;
this.d3LineChart = new DraggableLineChartD3(svg, {
height: height || draggableLineChartDefaultProps.height,
width: width || draggableLineChartDefaultProps.width,
margin: margin || draggableLineChartDefaultProps.margin,
data: getCleanData(data),
onDragEnd: onDragEnd,
xAxisTicksVertical: xAxisTicksVertical,
dataIsCentered: dataIsCentered,
yAxisMin: yAxisMin,
xAxisRenderProp: xAxisRenderProp,
emptyRenderProp: emptyRenderProp,
cx: cx,
onPreselect: onPreselect
});
this.d3LineChart.renderLineChart();
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
height = _this$props2.height,
width = _this$props2.width,
passThroughs = _objectWithoutProperties(_this$props2, ["height", "width"]);
return /*#__PURE__*/React.createElement("svg", _extends({}, omitProps(passThroughs, undefined, ['height', 'width', 'margin', 'data', 'onDragEnd', 'xAxisTicksVertical', 'dataIsCentered', 'yAxisMin', 'xAxisRenderProp', 'onPreselect', 'preSelectText']), {
ref: function ref(_ref2) {
return _this2.ref = _ref2;
},
className: cx('&'),
width: width,
height: height
}));
}
}]);
return DraggableLineChart;
}(React.Component);
_defineProperty(DraggableLineChart, "displayName", 'DraggableLineChart');
_defineProperty(DraggableLineChart, "peek", {
description: "\n\t\t\tThe draggable line chart is a single-lined line chart where\n\t\t\tthe points on the line are draggable and will update the data real-time.\n\t\t",
categories: ['visualizations', 'charts']
});
_defineProperty(DraggableLineChart, "defaultProps", draggableLineChartDefaultProps);
export default DraggableLineChart;