@semcore/chart
Version:
Semrush Chart Component
124 lines • 5.8 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import { Bar } from 'recharts';
import { parseChildIndex } from "recharts/es6/util/ReactUtils";
import generateCategoricalChartRecharts from "recharts/es6/chart/generateCategoricalChart";
import { getBarSizeList, getStackedDataOfItem, getBandSizeOfAxis, getTicksOfAxis, getBarPosition, combineEventHandlers } from "recharts/es6/util/ChartUtils";
import XAxis from "recharts/es6/cartesian/XAxis";
import YAxis from "recharts/es6/cartesian/YAxis";
import { formatAxisMap } from "recharts/es6/util/CartesianUtils";
var axisComponents = [{
axisType: 'xAxis',
AxisComp: XAxis
}, {
axisType: 'yAxis',
AxisComp: YAxis
}];
var generateCategoricalChart = function generateCategoricalChart(params) {
var ReactComponent = generateCategoricalChartRecharts(params);
ReactComponent.prototype.getFormatItems = function (props, currentState) {
var _this = this;
var graphicalItems = currentState.graphicalItems,
stackGroups = currentState.stackGroups,
offset = currentState.offset,
updateId = currentState.updateId,
dataStartIndex = currentState.dataStartIndex,
dataEndIndex = currentState.dataEndIndex;
var barSize = props.barSize,
layout = props.layout,
barGap = props.barGap,
barCategoryGap = props.barCategoryGap,
globalMaxBarSize = props.maxBarSize;
var _this$constructor$get = this.constructor.getAxisNameByLayout(layout),
numericAxisName = _this$constructor$get.numericAxisName,
cateAxisName = _this$constructor$get.cateAxisName;
var hasBar = this.constructor.hasBar(graphicalItems);
var sizeList = hasBar && getBarSizeList({
barSize: barSize,
stackGroups: stackGroups
});
var formatedItems = [];
graphicalItems.forEach(function (item, index) {
var _stackGroups$numericA, _item$type;
var displayedData = _this.constructor.getDisplayedData(props, {
dataStartIndex: dataStartIndex,
dataEndIndex: dataEndIndex
}, item);
var _item$props = item.props,
dataKey = _item$props.dataKey,
childMaxBarSize = _item$props.maxBarSize;
var numericAxisId = item.props["".concat(numericAxisName, "Id")];
var cateAxisId = item.props["".concat(cateAxisName, "Id")];
var axisObj = axisComponents.reduce(function (result, entry) {
var _objectSpread2;
var axisMap = currentState["".concat(entry.axisType, "Map")];
var id = item.props["".concat(entry.axisType, "Id")];
var axis = axisMap === null || axisMap === void 0 ? void 0 : axisMap[id];
return _objectSpread(_objectSpread({}, result), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, entry.axisType, axis), _defineProperty(_objectSpread2, "".concat(entry.axisType, "Ticks"), getTicksOfAxis(axis)), _objectSpread2));
}, {});
var cateAxis = axisObj[cateAxisName];
var cateTicks = axisObj["".concat(cateAxisName, "Ticks")];
var stackedData = (stackGroups === null || stackGroups === void 0 ? void 0 : (_stackGroups$numericA = stackGroups[numericAxisId]) === null || _stackGroups$numericA === void 0 ? void 0 : _stackGroups$numericA.hasStack) && getStackedDataOfItem(item, stackGroups[numericAxisId].stackGroups);
var bandSize = getBandSizeOfAxis(cateAxis, cateTicks);
var maxBarSize = childMaxBarSize == null ? globalMaxBarSize : childMaxBarSize;
var barPosition = hasBar && getBarPosition({
barGap: barGap,
barCategoryGap: barCategoryGap,
bandSize: bandSize,
sizeList: sizeList[cateAxisId],
maxBarSize: maxBarSize
});
var componsedFn = item === null || item === void 0 ? void 0 : (_item$type = item.type) === null || _item$type === void 0 ? void 0 : _item$type.getComposedData;
barPosition = barPosition.map(function (bar) {
var position = bar.position;
var offsetBar = position.offset,
size = position.size;
return _objectSpread(_objectSpread({}, bar), {}, {
position: {
offset: barGap,
size: size + offsetBar * 2 - 2 * barGap
}
});
});
if (componsedFn) {
var _objectSpread3;
formatedItems.push({
props: _objectSpread(_objectSpread({}, componsedFn(_objectSpread(_objectSpread({}, axisObj), {}, {
displayedData: displayedData,
props: props,
dataKey: dataKey,
item: item,
bandSize: bandSize,
barPosition: barPosition,
offset: offset,
stackedData: stackedData,
layout: layout,
dataStartIndex: dataStartIndex,
dataEndIndex: dataEndIndex,
onItemMouseLeave: combineEventHandlers(_this.handleItemMouseLeave, null, item.props.onMouseLeave),
onItemMouseEnter: combineEventHandlers(_this.handleItemMouseEnter, null, item.props.onMouseEnter)
}))), {}, (_objectSpread3 = {
key: item.key || "item-".concat(index)
}, _defineProperty(_objectSpread3, numericAxisName, axisObj[numericAxisName]), _defineProperty(_objectSpread3, cateAxisName, axisObj[cateAxisName]), _defineProperty(_objectSpread3, "animationId", updateId), _objectSpread3)),
childIndex: parseChildIndex(item, props.children),
item: item
});
}
});
return formatedItems;
};
return ReactComponent;
};
export default generateCategoricalChart({
chartName: 'BarChart',
GraphicalChild: Bar,
axisComponents: [{
axisType: 'xAxis',
AxisComp: XAxis
}, {
axisType: 'yAxis',
AxisComp: YAxis
}],
formatAxisMap: formatAxisMap
});
//# sourceMappingURL=HistogramChart.js.map