UNPKG

d3plus-shape

Version:

Fancy SVG shapes for visualizations

284 lines (272 loc) 14.1 kB
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } 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, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : 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; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } 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 { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } import { extent } from "d3-array"; import { nest } from "d3-collection"; import { interpolatePath } from "d3-interpolate-path"; import { select } from "d3-selection"; import * as paths from "d3-shape"; import { accessor, constant, merge } from "d3plus-common"; import Shape from "./Shape.js"; import largestRect from "../geom/largestRect.js"; /** @class Area @extends Shape @desc Creates SVG areas based on an array of data. */ var Area = /*#__PURE__*/function (_Shape) { _inherits(Area, _Shape); var _super = _createSuper(Area); /** @memberof Area @desc Invoked when creating a new class instance, and overrides any default parameters inherited from Shape. @private */ function Area() { var _this; _classCallCheck(this, Area); _this = _super.call(this); _this._curve = constant("linear"); _this._defined = function () { return true; }; _this._labelBounds = function (d, i, aes) { var r = largestRect(aes.points); if (!r) return null; return { angle: r.angle, width: r.width, height: r.height, x: r.cx - r.width / 2 - _this._x(d, i), y: r.cy - r.height / 2 - _this._y(d, i) }; }; _this._labelConfig = Object.assign(_this._labelConfig, { textAnchor: "middle", verticalAlign: "middle" }); _this._name = "Area"; _this._x = accessor("x"); _this._x0 = accessor("x"); _this._x1 = null; _this._y = constant(0); _this._y0 = constant(0); _this._y1 = accessor("y"); return _this; } /** @memberof Area @desc Given a specific data point and index, returns the aesthetic properties of the shape. @param {Object} *data point* @param {Number} *index* @private */ _createClass(Area, [{ key: "_aes", value: function _aes(d) { var _this2 = this; var values = d.values.slice().sort(function (a, b) { return _this2._y1 ? _this2._x(a) - _this2._x(b) : _this2._y(a) - _this2._y(b); }); var points1 = values.map(function (v, z) { return [_this2._x0(v, z), _this2._y0(v, z)]; }); var points2 = values.reverse().map(function (v, z) { return _this2._y1 ? [_this2._x(v, z), _this2._y1(v, z)] : [_this2._x1(v, z), _this2._y(v, z)]; }); var points = points1.concat(points2); if (points1[0][1] > points2[0][1]) points = points.reverse(); points.push(points[0]); return { points: points }; } /** @memberof Area @desc Filters/manipulates the data array before binding each point to an SVG group. @param {Array} [*data* = the data array to be filtered] @private */ }, { key: "_dataFilter", value: function _dataFilter(data) { var _this3 = this; var areas = nest().key(this._id).entries(data).map(function (d) { d.data = merge(d.values); d.i = data.indexOf(d.values[0]); var x = extent(d.values.map(_this3._x).concat(d.values.map(_this3._x0)).concat(_this3._x1 ? d.values.map(_this3._x1) : [])); d.xR = x; d.width = x[1] - x[0]; d.x = x[0] + d.width / 2; var y = extent(d.values.map(_this3._y).concat(d.values.map(_this3._y0)).concat(_this3._y1 ? d.values.map(_this3._y1) : [])); d.yR = y; d.height = y[1] - y[0]; d.y = y[0] + d.height / 2; d.nested = true; d.translate = [d.x, d.y]; d.__d3plusShape__ = true; return d; }); areas.key = function (d) { return d.key; }; return areas; } /** @memberof Area @desc Draws the area polygons. @param {Function} [*callback*] @chainable */ }, { key: "render", value: function render(callback) { var _this4 = this; _get(_getPrototypeOf(Area.prototype), "render", this).call(this, callback); var userCurve = this._curve.bind(this)(this.config()); var curve = paths["curve".concat(userCurve.charAt(0).toUpperCase()).concat(userCurve.slice(1))]; var path = this._path = paths.area().defined(this._defined).curve(curve).x(this._x).x0(this._x0).x1(this._x1).y(this._y).y0(this._y0).y1(this._y1); var exitPath = paths.area().defined(function (d) { return d; }).curve(curve).x(this._x).y(this._y).x0(function (d, i) { return _this4._x1 ? _this4._x0(d, i) + (_this4._x1(d, i) - _this4._x0(d, i)) / 2 : _this4._x0(d, i); }).x1(function (d, i) { return _this4._x1 ? _this4._x0(d, i) + (_this4._x1(d, i) - _this4._x0(d, i)) / 2 : _this4._x0(d, i); }).y0(function (d, i) { return _this4._y1 ? _this4._y0(d, i) + (_this4._y1(d, i) - _this4._y0(d, i)) / 2 : _this4._y0(d, i); }).y1(function (d, i) { return _this4._y1 ? _this4._y0(d, i) + (_this4._y1(d, i) - _this4._y0(d, i)) / 2 : _this4._y0(d, i); }); this._enter.append("path").attr("transform", function (d) { return "translate(".concat(-d.xR[0] - d.width / 2, ", ").concat(-d.yR[0] - d.height / 2, ")"); }).attr("d", function (d) { return exitPath(d.values); }).call(this._applyStyle.bind(this)).transition(this._transition).attrTween("d", function (d) { return interpolatePath(select(this).attr("d"), path(d.values)); }); this._update.select("path").transition(this._transition).attr("transform", function (d) { return "translate(".concat(-d.xR[0] - d.width / 2, ", ").concat(-d.yR[0] - d.height / 2, ")"); }).attrTween("d", function (d) { return interpolatePath(select(this).attr("d"), path(d.values)); }).call(this._applyStyle.bind(this)); this._exit.select("path").transition(this._transition).attrTween("d", function (d) { return interpolatePath(select(this).attr("d"), exitPath(d.values)); }); return this; } /** @memberof Area @desc If *value* is specified, sets the area curve to the specified string and returns the current class instance. If *value* is not specified, returns the current area curve. @param {Function|String} [*value* = "linear"] @chainable */ }, { key: "curve", value: function curve(_) { return arguments.length ? (this._curve = typeof _ === "function" ? _ : constant(_), this) : this._curve; } /** @memberof Area @desc If *value* is specified, sets the defined accessor to the specified function and returns the current class instance. If *value* is not specified, returns the current defined accessor. @param {Function} [*value*] @chainable */ }, { key: "defined", value: function defined(_) { return arguments.length ? (this._defined = _, this) : this._defined; } /** @memberof Area @desc If *value* is specified, sets the x accessor to the specified function or number and returns the current class instance. If *value* is not specified, returns the current x accessor. @param {Function|Number} [*value*] @chainable */ }, { key: "x", value: function x(_) { if (!arguments.length) return this._x; this._x = typeof _ === "function" ? _ : constant(_); this._x0 = this._x; return this; } /** @memberof Area @desc If *value* is specified, sets the x0 accessor to the specified function or number and returns the current class instance. If *value* is not specified, returns the current x0 accessor. @param {Function|Number} [*value*] @chainable */ }, { key: "x0", value: function x0(_) { if (!arguments.length) return this._x0; this._x0 = typeof _ === "function" ? _ : constant(_); this._x = this._x0; return this; } /** @memberof Area @desc If *value* is specified, sets the x1 accessor to the specified function or number and returns the current class instance. If *value* is not specified, returns the current x1 accessor. @param {Function|Number|null} [*value*] @chainable */ }, { key: "x1", value: function x1(_) { return arguments.length ? (this._x1 = typeof _ === "function" || _ === null ? _ : constant(_), this) : this._x1; } /** @memberof Area @desc If *value* is specified, sets the y accessor to the specified function or number and returns the current class instance. If *value* is not specified, returns the current y accessor. @param {Function|Number} [*value*] @chainable */ }, { key: "y", value: function y(_) { if (!arguments.length) return this._y; this._y = typeof _ === "function" ? _ : constant(_); this._y0 = this._y; return this; } /** @memberof Area @desc If *value* is specified, sets the y0 accessor to the specified function or number and returns the current class instance. If *value* is not specified, returns the current y0 accessor. @param {Function|Number} [*value*] @chainable */ }, { key: "y0", value: function y0(_) { if (!arguments.length) return this._y0; this._y0 = typeof _ === "function" ? _ : constant(_); this._y = this._y0; return this; } /** @memberof Area @desc If *value* is specified, sets the y1 accessor to the specified function or number and returns the current class instance. If *value* is not specified, returns the current y1 accessor. @param {Function|Number|null} [*value*] @chainable */ }, { key: "y1", value: function y1(_) { return arguments.length ? (this._y1 = typeof _ === "function" || _ === null ? _ : constant(_), this) : this._y1; } }]); return Area; }(Shape); export { Area as default };