123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- var zrUtil = require("zrender/lib/core/util");
- var textContain = require("zrender/lib/contain/text");
- var _model = require("../util/model");
- var makeInner = _model.makeInner;
- var _axisHelper = require("./axisHelper");
- var makeLabelFormatter = _axisHelper.makeLabelFormatter;
- var getOptionCategoryInterval = _axisHelper.getOptionCategoryInterval;
- var shouldShowAllLabels = _axisHelper.shouldShowAllLabels;
- 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: axis.scale.getTicks()
- };
- }
- 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 (zrUtil.isFunction(optionLabelInterval)) {
- labels = makeLabelsByCustomizedCategoryInterval(axis, optionLabelInterval);
- } else {
- numericLabelInterval = optionLabelInterval === 'auto' ? makeAutoCategoryInterval(axis) : optionLabelInterval;
- labels = makeLabelsByNumericCategoryInterval(axis, numericLabelInterval);
- }
- return listCacheSet(labelsCache, optionLabelInterval, {
- labels: 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 (zrUtil.isFunction(optionTickInterval)) {
- ticks = makeLabelsByCustomizedCategoryInterval(axis, optionTickInterval, true);
- }
-
-
- else if (optionTickInterval === 'auto') {
- var labelsResult = makeCategoryLabelsActually(axis, axis.getLabelModel());
- tickCategoryInterval = labelsResult.labelCategoryInterval;
- ticks = zrUtil.map(labelsResult.labels, function (labelItem) {
- return labelItem.tickValue;
- });
- } else {
- tickCategoryInterval = optionTickInterval;
- ticks = makeLabelsByNumericCategoryInterval(axis, tickCategoryInterval, true);
- }
- return listCacheSet(ticksCache, optionTickInterval, {
- ticks: ticks,
- tickCategoryInterval: tickCategoryInterval
- });
- }
- function makeRealNumberLabels(axis) {
- var ticks = axis.scale.getTicks();
- var labelFormatter = makeLabelFormatter(axis);
- return {
- labels: zrUtil.map(ticks, function (tickValue, idx) {
- return {
- formattedLabel: labelFormatter(tickValue, idx),
- rawLabel: axis.scale.getLabel(tickValue),
- tickValue: tickValue
- };
- })
- };
- }
- 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: key,
- value: 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 = textContain.getBoundingRect(labelFormatter(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.axisExtend0 === axisExtent[0] && cache.axisExtend1 === axisExtent[1]) {
- interval = lastAutoInterval;
- }
-
- else {
- cache.lastTickCount = tickCount;
- cache.lastAutoInterval = interval;
- cache.axisExtend0 = axisExtent[0];
- cache.axisExtend1 = 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(tVal) {
- result.push(onlyTick ? tVal : {
- formattedLabel: labelFormatter(tVal),
- rawLabel: ordinalScale.getLabel(tVal),
- tickValue: tVal
- });
- }
- return result;
- }
- function makeLabelsByCustomizedCategoryInterval(axis, categoryInterval, onlyTick) {
- var ordinalScale = axis.scale;
- var labelFormatter = makeLabelFormatter(axis);
- var result = [];
- zrUtil.each(ordinalScale.getTicks(), function (tickValue) {
- var rawLabel = ordinalScale.getLabel(tickValue);
- if (categoryInterval(tickValue, rawLabel)) {
- result.push(onlyTick ? tickValue : {
- formattedLabel: labelFormatter(tickValue),
- rawLabel: rawLabel,
- tickValue: tickValue
- });
- }
- });
- return result;
- }
- exports.createAxisLabels = createAxisLabels;
- exports.createAxisTicks = createAxisTicks;
- exports.calculateCategoryInterval = calculateCategoryInterval;
|