UNPKG

rumble-charts

Version:

Truly declarative React charts components

130 lines (123 loc) 7.33 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib_es6 = require('./external/tslib/tslib.es6.js'); var React = require('react'); require('./helpers/colorFunc.js'); require('d3-shape'); require('d3-ease'); var isString = require('./helpers/isString.js'); var isUndefined = require('./helpers/isUndefined.js'); var normalizeNumber = require('./helpers/normalizeNumber.js'); var isNumber = require('./helpers/isNumber.js'); var value = require('./helpers/value.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); /** * Renders ticks (labels and lines) for axis (x and y). */ function Ticks(props) { var className = props.className, scaleX = props.scaleX, scaleY = props.scaleY, _a = props.axis, axis = _a === void 0 ? 'x' : _a, style = props.style; var x = scaleX.factory(props); var y = scaleY.factory(props); var horizontal = (axis === 'y' && !scaleX.swap && !scaleY.swap) || (axis === 'x' && (scaleX.swap || scaleY.swap)); var position = props.position || (axis === 'x' ? (scaleX.swap || scaleY.swap ? 'top' : 'bottom') : 'left'); var ticks1 = value.value([props.ticks], props); var ticks2 = isNumber.isNumber(ticks1) ? { maxTicks: ticks1 } : (ticks1 || {}); var ticks = Array.isArray(ticks2) ? ticks2 : generateTicks(props, ticks2); return React__default["default"].createElement("g", { className: className, style: style, opacity: props.opacity }, ticks.map(function (tick, index) { return renderTick({ ticksLength: ticks.length, tick: tick, index: index, x: x, y: y, horizontal: horizontal, position: position, props: props }); })); } function generateTicks(props, _a) { var maxTicks = _a.maxTicks, minDistance = _a.minDistance, distance = _a.distance; var axis = props.axis, maxX = props.maxX, maxY = props.maxY, minX = props.minX, minY = props.minY; var max = axis === 'y' ? maxY : maxX; var min = axis === 'y' ? minY : minX; var length = max - min; if (isUndefined.isUndefined(minDistance)) { minDistance = Math.min(1, length); } if (isUndefined.isUndefined(maxTicks)) { maxTicks = Math.min((length + minDistance) / minDistance, 5); } if (isUndefined.isUndefined(distance)) { distance = Math.max(minDistance, length / maxTicks); distance = Math.ceil(distance / minDistance) * minDistance; } var result = []; for (var i = min; i < max + minDistance; i += distance) { result.push(i); } return result; } function renderTick(_a) { var _b; var ticksLength = _a.ticksLength, _tick = _a.tick, index = _a.index, x = _a.x, y = _a.y, horizontal = _a.horizontal, position = _a.position, props = _a.props; var axis = props.axis, className = props.className, layerWidth = props.layerWidth, layerHeight = props.layerHeight, scaleX = props.scaleX, scaleY = props.scaleY; var tick = isNumber.isNumber(_tick) ? (_b = {}, _b[axis] = _tick, _b) : _tick; if ('tickVisible' in props) { var tickVisible = value.value(props.tickVisible, { index: index, ticksLength: ticksLength, tick: tick, props: props }); if (!tickVisible) { return; } } var tickAttributes = value.value(props.tickAttributes, { index: index, ticksLength: ticksLength, tick: tick, props: props }); var tickStyle = value.value(props.tickStyle, { index: index, ticksLength: ticksLength, tick: tick, props: props }); var pX = axis === 'x' ? x(tick.x) : normalizeNumber.normalizeNumber(position, layerWidth); var pY = axis === 'y' ? y(tick.y) : normalizeNumber.normalizeNumber(position, layerHeight); var transform = (scaleX.swap || scaleY.swap) ? ('translate(' + pY + ' ' + pX + ')') : ('translate(' + pX + ' ' + pY + ')'); return React__default["default"].createElement("g", tslib_es6.__assign({ key: index, style: tickStyle, transform: transform, className: className && (className + '-tick ' + className + '-tick-' + index) }, tickAttributes), renderLabel({ ticksLength: ticksLength, tick: tick, index: index, props: props }), renderLine({ ticksLength: ticksLength, tick: tick, index: index, horizontal: horizontal, props: props })); } function renderLabel(_a) { var ticksLength = _a.ticksLength, tick = _a.tick, index = _a.index, props = _a.props; var className = props.className, axis = props.axis; if ('labelVisible' in props) { var labelVisible = value.value(props.labelVisible, { index: index, ticksLength: ticksLength, tick: tick, props: props }); if (!labelVisible) { return; } } var labelAttributes = value.value([tick.labelAttributes, props.labelAttributes], { index: index, ticksLength: ticksLength, tick: tick, props: props }); var labelStyle = value.value([tick.labelStyle, props.labelStyle], { index: index, ticksLength: ticksLength, tick: tick, props: props }); var label = value.value([tick.label, props.label, tick[axis]], { index: index, ticksLength: ticksLength, tick: tick, props: props }); if (isString.isString(label) || isNumber.isNumber(label)) { return React__default["default"].createElement("text", tslib_es6.__assign({ style: labelStyle, className: className && ("".concat(className, "-label ").concat(className, "-label-").concat(index)) }, labelAttributes), value.value(props.labelFormat, label + '') || label); } else { return label; } } function renderLine(_a) { var ticksLength = _a.ticksLength, tick = _a.tick, index = _a.index, horizontal = _a.horizontal, props = _a.props; var _b = props.lineLength, lineLength = _b === void 0 ? 5 : _b, _c = props.lineOffset, lineOffset = _c === void 0 ? 0 : _c, layerWidth = props.layerWidth, layerHeight = props.layerHeight, className = props.className; if ('lineVisible' in props) { var lineVisible = value.value(props.lineVisible, { index: index, ticksLength: ticksLength, tick: tick, props: props }); if (!lineVisible) { return null; } } var lineAttributes = value.value([tick.lineAttributes, props.lineAttributes], { index: index, ticksLength: ticksLength, tick: tick, props: props }); var lineStyle = value.value([tick.lineStyle, props.lineStyle], { index: index, ticksLength: ticksLength, tick: tick, props: props }); var _lineLength = normalizeNumber.normalizeNumber(value.value([tick.lineLength, lineLength], { index: index, ticksLength: ticksLength, tick: tick, props: props }), horizontal ? layerWidth : layerHeight); var _lineOffset = normalizeNumber.normalizeNumber(value.value([tick.lineOffset, lineOffset], { index: index, ticksLength: ticksLength, tick: tick, props: props }), horizontal ? layerWidth : layerHeight); var d = horizontal ? ('M' + _lineOffset + ',0 h' + _lineLength) : ('M0,' + _lineOffset + ' v' + _lineLength); return React__default["default"].createElement("path", tslib_es6.__assign({ style: lineStyle, className: className && ("".concat(className, "-line ").concat(className, "-line-").concat(index)), d: d }, lineAttributes)); } exports.Ticks = Ticks;