zmp-react
Version:
Build full featured iOS & Android apps using ZMP & React
204 lines (175 loc) • 7.53 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _utils = require("../shared/utils");
var _zmp = require("../shared/zmp");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var PieChart = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
var className = props.className,
id = props.id,
style = props.style,
_props$size = props.size,
size = _props$size === void 0 ? 320 : _props$size,
_props$tooltip = props.tooltip,
tooltip = _props$tooltip === void 0 ? false : _props$tooltip,
_props$datasets = props.datasets,
datasets = _props$datasets === void 0 ? [] : _props$datasets,
formatTooltip = props.formatTooltip,
children = props.children;
var extraAttrs = (0, _utils.getExtraAttrs)(props);
var _useState = (0, _react.useState)(null),
currentIndex = _useState[0],
setCurrentIndex = _useState[1];
var previousIndex = (0, _react.useRef)(null);
var elRef = (0, _react.useRef)(null);
var zmpTooltip = (0, _react.useRef)(null);
(0, _react.useImperativeHandle)(ref, function () {
return {
el: elRef.current
};
});
var getSummValue = function getSummValue() {
var summ = 0;
datasets.map(function (d) {
return d.value || 0;
}).forEach(function (value) {
summ += value;
});
return summ;
};
var getPaths = function getPaths() {
var paths = [];
var cumulativePercentage = 0;
function getCoordinatesForPercentage(percentage) {
var x = Math.cos(2 * Math.PI * percentage) * (size / 3);
var y = Math.sin(2 * Math.PI * percentage) * (size / 3);
return [x, y];
}
datasets.forEach(function (_ref) {
var value = _ref.value,
label = _ref.label,
color = _ref.color;
var percentage = value / getSummValue();
var _getCoordinatesForPer = getCoordinatesForPercentage(cumulativePercentage),
startX = _getCoordinatesForPer[0],
startY = _getCoordinatesForPer[1];
cumulativePercentage += percentage;
var _getCoordinatesForPer2 = getCoordinatesForPercentage(cumulativePercentage),
endX = _getCoordinatesForPer2[0],
endY = _getCoordinatesForPer2[1];
var largeArcFlag = percentage > 0.5 ? 1 : 0;
var points = ["M " + startX + " " + startY, // Move
"A " + size / 3 + " " + size / 3 + " 0 " + largeArcFlag + " 1 " + endX + " " + endY, // Arc
'L 0 0' // Line
].join(' ');
paths.push({
points: points,
label: label,
color: color
});
});
return paths;
};
var formatTooltipText = function formatTooltipText() {
if (currentIndex === null) return '';
var _datasets$currentInde = datasets[currentIndex],
value = _datasets$currentInde.value,
label = _datasets$currentInde.label,
color = _datasets$currentInde.color;
var percentage = value / getSummValue() * 100;
var round = function round(v) {
if (parseInt(v, 10) === v) return v;
return Math.round(v * 100) / 100;
};
if (formatTooltip) {
return formatTooltip({
index: currentIndex,
value: value,
label: label,
color: color,
percentage: percentage
});
}
var tooltipText = "" + (label ? label + ": " : '') + round(value) + " (" + round(percentage) + "%)";
return "\n <div class=\"pie-chart-tooltip-label\">\n <span class=\"pie-chart-tooltip-color\" style=\"background-color: " + color + ";\"></span> " + tooltipText + "\n </div>\n ";
};
var setTooltip = function setTooltip() {
if (currentIndex === null && !zmpTooltip.current) return;
if (!tooltip || !elRef.current || !_zmp.zmp) return;
if (currentIndex !== null && !zmpTooltip.current) {
zmpTooltip.current = _zmp.zmp.tooltip.create({
trigger: 'manual',
containerEl: elRef.current,
targetEl: elRef.current.querySelector("path[data-index=\"" + currentIndex + "\"]"),
text: formatTooltipText(),
cssClass: 'pie-chart-tooltip'
});
zmpTooltip.current.show();
return;
}
if (!zmpTooltip.current) return;
if (currentIndex !== null) {
zmpTooltip.current.setText(formatTooltipText());
zmpTooltip.current.setTargetEl(elRef.current.querySelector("path[data-index=\"" + currentIndex + "\"]"));
zmpTooltip.current.show();
} else {
zmpTooltip.current.hide();
}
};
(0, _react.useEffect)(function () {
if (previousIndex.current === currentIndex) return;
previousIndex.current = currentIndex;
(0, _utils.emit)(props, 'select', currentIndex, datasets[currentIndex]);
setTooltip();
}, [currentIndex]);
(0, _react.useEffect)(function () {
return function () {
if (zmpTooltip.current && zmpTooltip.current.destroy) {
zmpTooltip.current.destroy();
}
zmpTooltip.current = null;
};
}, []);
var classes = (0, _utils.classNames)('pie-chart', className);
var paths = getPaths();
return /*#__PURE__*/_react.default.createElement("div", (0, _extends2.default)({
id: id,
style: style,
className: classes,
ref: elRef
}, extraAttrs), /*#__PURE__*/_react.default.createElement("svg", {
xmlns: "http://www.w3.org/2000/svg",
width: size,
height: size,
viewBox: "-" + size / 3 + " -" + size / 3 + " " + size * 2 / 3 + " " + size * 2 / 3,
style: {
transform: 'rotate(-90deg)'
}
}, paths.map(function (path, index) {
return /*#__PURE__*/_react.default.createElement("path", {
key: path.label || index,
d: path.points,
fill: path.color,
"data-index": index,
className: (0, _utils.classNames)({
'pie-chart-hidden': currentIndex !== null && currentIndex !== index
}),
onClick: function onClick() {
return setCurrentIndex(index);
},
onMouseEnter: function onMouseEnter() {
return setCurrentIndex(index);
},
onMouseLeave: function onMouseLeave() {
return setCurrentIndex(null);
}
});
})), children);
});
PieChart.displayName = 'zmp-pie-chart';
var _default = PieChart;
exports.default = _default;