UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

221 lines (220 loc) 7.82 kB
import { map, isFunction, each } from "../../../zrender/lib/core/util.mjs"; import { getBoundingRect } from "../../../zrender/lib/contain/text.mjs"; import { makeInner } from "../util/model.mjs"; import { getOptionCategoryInterval, makeLabelFormatter, shouldShowAllLabels } from "./axisHelper.mjs"; var inner = makeInner(); function createAxisLabels(axis) { return axis.type === "category" ? makeCategoryLabels(axis) : makeRealNumberLabels(axis); } function createAxisTicks(axis, tickModel) { return axis.type === "category" ? makeCategoryTicks(axis, tickModel) : { ticks: map(axis.scale.getTicks(), function(tick) { return tick.value; }) }; } function makeCategoryLabels(axis) { var labelModel = axis.getLabelModel(); var result = makeCategoryLabelsActually(axis, labelModel); return !labelModel.get("show") || axis.scale.isBlank() ? { labels: [], labelCategoryInterval: result.labelCategoryInterval } : result; } function makeCategoryLabelsActually(axis, labelModel) { var labelsCache = getListCache(axis, "labels"); var optionLabelInterval = getOptionCategoryInterval(labelModel); var result = listCacheGet(labelsCache, optionLabelInterval); if (result) { return result; } var labels; var numericLabelInterval; if (isFunction(optionLabelInterval)) { labels = makeLabelsByCustomizedCategoryInterval(axis, optionLabelInterval); } else { numericLabelInterval = optionLabelInterval === "auto" ? makeAutoCategoryInterval(axis) : optionLabelInterval; labels = makeLabelsByNumericCategoryInterval(axis, numericLabelInterval); } return listCacheSet(labelsCache, optionLabelInterval, { labels, labelCategoryInterval: numericLabelInterval }); } function makeCategoryTicks(axis, tickModel) { var ticksCache = getListCache(axis, "ticks"); var optionTickInterval = getOptionCategoryInterval(tickModel); var result = listCacheGet(ticksCache, optionTickInterval); if (result) { return result; } var ticks; var tickCategoryInterval; if (!tickModel.get("show") || axis.scale.isBlank()) { ticks = []; } if (isFunction(optionTickInterval)) { ticks = makeLabelsByCustomizedCategoryInterval(axis, optionTickInterval, true); } else if (optionTickInterval === "auto") { var labelsResult = makeCategoryLabelsActually(axis, axis.getLabelModel()); tickCategoryInterval = labelsResult.labelCategoryInterval; ticks = map(labelsResult.labels, function(labelItem) { return labelItem.tickValue; }); } else { tickCategoryInterval = optionTickInterval; ticks = makeLabelsByNumericCategoryInterval(axis, tickCategoryInterval, true); } return listCacheSet(ticksCache, optionTickInterval, { ticks, tickCategoryInterval }); } function makeRealNumberLabels(axis) { var ticks = axis.scale.getTicks(); var labelFormatter = makeLabelFormatter(axis); return { labels: map(ticks, function(tick, idx) { return { level: tick.level, formattedLabel: labelFormatter(tick, idx), rawLabel: axis.scale.getLabel(tick), tickValue: tick.value }; }) }; } function getListCache(axis, prop) { return inner(axis)[prop] || (inner(axis)[prop] = []); } function listCacheGet(cache, key) { for (var i = 0; i < cache.length; i++) { if (cache[i].key === key) { return cache[i].value; } } } function listCacheSet(cache, key, value) { cache.push({ key, value }); return value; } function makeAutoCategoryInterval(axis) { var result = inner(axis).autoInterval; return result != null ? result : inner(axis).autoInterval = axis.calculateCategoryInterval(); } function calculateCategoryInterval(axis) { var params = fetchAutoCategoryIntervalCalculationParams(axis); var labelFormatter = makeLabelFormatter(axis); var rotation = (params.axisRotate - params.labelRotate) / 180 * Math.PI; var ordinalScale = axis.scale; var ordinalExtent = ordinalScale.getExtent(); var tickCount = ordinalScale.count(); if (ordinalExtent[1] - ordinalExtent[0] < 1) { return 0; } var step = 1; if (tickCount > 40) { step = Math.max(1, Math.floor(tickCount / 40)); } var tickValue = ordinalExtent[0]; var unitSpan = axis.dataToCoord(tickValue + 1) - axis.dataToCoord(tickValue); var unitW = Math.abs(unitSpan * Math.cos(rotation)); var unitH = Math.abs(unitSpan * Math.sin(rotation)); var maxW = 0; var maxH = 0; for (; tickValue <= ordinalExtent[1]; tickValue += step) { var width = 0; var height = 0; var rect = getBoundingRect(labelFormatter({ value: tickValue }), params.font, "center", "top"); width = rect.width * 1.3; height = rect.height * 1.3; maxW = Math.max(maxW, width, 7); maxH = Math.max(maxH, height, 7); } var dw = maxW / unitW; var dh = maxH / unitH; isNaN(dw) && (dw = Infinity); isNaN(dh) && (dh = Infinity); var interval = Math.max(0, Math.floor(Math.min(dw, dh))); var cache = inner(axis.model); var axisExtent = axis.getExtent(); var lastAutoInterval = cache.lastAutoInterval; var lastTickCount = cache.lastTickCount; if (lastAutoInterval != null && lastTickCount != null && Math.abs(lastAutoInterval - interval) <= 1 && Math.abs(lastTickCount - tickCount) <= 1 && lastAutoInterval > interval && cache.axisExtent0 === axisExtent[0] && cache.axisExtent1 === axisExtent[1]) { interval = lastAutoInterval; } else { cache.lastTickCount = tickCount; cache.lastAutoInterval = interval; cache.axisExtent0 = axisExtent[0]; cache.axisExtent1 = axisExtent[1]; } return interval; } function fetchAutoCategoryIntervalCalculationParams(axis) { var labelModel = axis.getLabelModel(); return { axisRotate: axis.getRotate ? axis.getRotate() : axis.isHorizontal && !axis.isHorizontal() ? 90 : 0, labelRotate: labelModel.get("rotate") || 0, font: labelModel.getFont() }; } function makeLabelsByNumericCategoryInterval(axis, categoryInterval, onlyTick) { var labelFormatter = makeLabelFormatter(axis); var ordinalScale = axis.scale; var ordinalExtent = ordinalScale.getExtent(); var labelModel = axis.getLabelModel(); var result = []; var step = Math.max((categoryInterval || 0) + 1, 1); var startTick = ordinalExtent[0]; var tickCount = ordinalScale.count(); if (startTick !== 0 && step > 1 && tickCount / step > 2) { startTick = Math.round(Math.ceil(startTick / step) * step); } var showAllLabel = shouldShowAllLabels(axis); var includeMinLabel = labelModel.get("showMinLabel") || showAllLabel; var includeMaxLabel = labelModel.get("showMaxLabel") || showAllLabel; if (includeMinLabel && startTick !== ordinalExtent[0]) { addItem(ordinalExtent[0]); } var tickValue = startTick; for (; tickValue <= ordinalExtent[1]; tickValue += step) { addItem(tickValue); } if (includeMaxLabel && tickValue - step !== ordinalExtent[1]) { addItem(ordinalExtent[1]); } function addItem(tickValue2) { var tickObj = { value: tickValue2 }; result.push(onlyTick ? tickValue2 : { formattedLabel: labelFormatter(tickObj), rawLabel: ordinalScale.getLabel(tickObj), tickValue: tickValue2 }); } return result; } function makeLabelsByCustomizedCategoryInterval(axis, categoryInterval, onlyTick) { var ordinalScale = axis.scale; var labelFormatter = makeLabelFormatter(axis); var result = []; each(ordinalScale.getTicks(), function(tick) { var rawLabel = ordinalScale.getLabel(tick); var tickValue = tick.value; if (categoryInterval(tick.value, rawLabel)) { result.push(onlyTick ? tickValue : { formattedLabel: labelFormatter(tick), rawLabel, tickValue }); } }); return result; } export { calculateCategoryInterval, createAxisLabels, createAxisTicks };