project/node_modules/echarts/lib/scale/Ordinal.js

214 lines
7.6 KiB
JavaScript
Raw Normal View History

2024-07-14 15:48:34 +08:00
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* AUTO-GENERATED FILE. DO NOT MODIFY.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { __extends } from "tslib";
/**
* Linear continuous scale
* http://en.wikipedia.org/wiki/Level_of_measurement
*/
// FIXME only one data
import Scale from './Scale.js';
import OrdinalMeta from '../data/OrdinalMeta.js';
import * as scaleHelper from './helper.js';
import { isArray, map, isObject, isString } from 'zrender/lib/core/util.js';
var OrdinalScale = /** @class */function (_super) {
__extends(OrdinalScale, _super);
function OrdinalScale(setting) {
var _this = _super.call(this, setting) || this;
_this.type = 'ordinal';
var ordinalMeta = _this.getSetting('ordinalMeta');
// Caution: Should not use instanceof, consider ec-extensions using
// import approach to get OrdinalMeta class.
if (!ordinalMeta) {
ordinalMeta = new OrdinalMeta({});
}
if (isArray(ordinalMeta)) {
ordinalMeta = new OrdinalMeta({
categories: map(ordinalMeta, function (item) {
return isObject(item) ? item.value : item;
})
});
}
_this._ordinalMeta = ordinalMeta;
_this._extent = _this.getSetting('extent') || [0, ordinalMeta.categories.length - 1];
return _this;
}
OrdinalScale.prototype.parse = function (val) {
// Caution: Math.round(null) will return `0` rather than `NaN`
if (val == null) {
return NaN;
}
return isString(val) ? this._ordinalMeta.getOrdinal(val)
// val might be float.
: Math.round(val);
};
OrdinalScale.prototype.contain = function (rank) {
rank = this.parse(rank);
return scaleHelper.contain(rank, this._extent) && this._ordinalMeta.categories[rank] != null;
};
/**
* Normalize given rank or name to linear [0, 1]
* @param val raw ordinal number.
* @return normalized value in [0, 1].
*/
OrdinalScale.prototype.normalize = function (val) {
val = this._getTickNumber(this.parse(val));
return scaleHelper.normalize(val, this._extent);
};
/**
* @param val normalized value in [0, 1].
* @return raw ordinal number.
*/
OrdinalScale.prototype.scale = function (val) {
val = Math.round(scaleHelper.scale(val, this._extent));
return this.getRawOrdinalNumber(val);
};
OrdinalScale.prototype.getTicks = function () {
var ticks = [];
var extent = this._extent;
var rank = extent[0];
while (rank <= extent[1]) {
ticks.push({
value: rank
});
rank++;
}
return ticks;
};
OrdinalScale.prototype.getMinorTicks = function (splitNumber) {
// Not support.
return;
};
/**
* @see `Ordinal['_ordinalNumbersByTick']`
*/
OrdinalScale.prototype.setSortInfo = function (info) {
if (info == null) {
this._ordinalNumbersByTick = this._ticksByOrdinalNumber = null;
return;
}
var infoOrdinalNumbers = info.ordinalNumbers;
var ordinalsByTick = this._ordinalNumbersByTick = [];
var ticksByOrdinal = this._ticksByOrdinalNumber = [];
// Unnecessary support negative tick in `realtimeSort`.
var tickNum = 0;
var allCategoryLen = this._ordinalMeta.categories.length;
for (var len = Math.min(allCategoryLen, infoOrdinalNumbers.length); tickNum < len; ++tickNum) {
var ordinalNumber = infoOrdinalNumbers[tickNum];
ordinalsByTick[tickNum] = ordinalNumber;
ticksByOrdinal[ordinalNumber] = tickNum;
}
// Handle that `series.data` only covers part of the `axis.category.data`.
var unusedOrdinal = 0;
for (; tickNum < allCategoryLen; ++tickNum) {
while (ticksByOrdinal[unusedOrdinal] != null) {
unusedOrdinal++;
}
;
ordinalsByTick.push(unusedOrdinal);
ticksByOrdinal[unusedOrdinal] = tickNum;
}
};
OrdinalScale.prototype._getTickNumber = function (ordinal) {
var ticksByOrdinalNumber = this._ticksByOrdinalNumber;
// also support ordinal out of range of `ordinalMeta.categories.length`,
// where ordinal numbers are used as tick value directly.
return ticksByOrdinalNumber && ordinal >= 0 && ordinal < ticksByOrdinalNumber.length ? ticksByOrdinalNumber[ordinal] : ordinal;
};
/**
* @usage
* ```js
* const ordinalNumber = ordinalScale.getRawOrdinalNumber(tickVal);
*
* // case0
* const rawOrdinalValue = axisModel.getCategories()[ordinalNumber];
* // case1
* const rawOrdinalValue = this._ordinalMeta.categories[ordinalNumber];
* // case2
* const coord = axis.dataToCoord(ordinalNumber);
* ```
*
* @param {OrdinalNumber} tickNumber index of display
*/
OrdinalScale.prototype.getRawOrdinalNumber = function (tickNumber) {
var ordinalNumbersByTick = this._ordinalNumbersByTick;
// tickNumber may be out of range, e.g., when axis max is larger than `ordinalMeta.categories.length`.,
// where ordinal numbers are used as tick value directly.
return ordinalNumbersByTick && tickNumber >= 0 && tickNumber < ordinalNumbersByTick.length ? ordinalNumbersByTick[tickNumber] : tickNumber;
};
/**
* Get item on tick
*/
OrdinalScale.prototype.getLabel = function (tick) {
if (!this.isBlank()) {
var ordinalNumber = this.getRawOrdinalNumber(tick.value);
var cateogry = this._ordinalMeta.categories[ordinalNumber];
// Note that if no data, ordinalMeta.categories is an empty array.
// Return empty if it's not exist.
return cateogry == null ? '' : cateogry + '';
}
};
OrdinalScale.prototype.count = function () {
return this._extent[1] - this._extent[0] + 1;
};
OrdinalScale.prototype.unionExtentFromData = function (data, dim) {
this.unionExtent(data.getApproximateExtent(dim));
};
/**
* @override
* If value is in extent range
*/
OrdinalScale.prototype.isInExtentRange = function (value) {
value = this._getTickNumber(value);
return this._extent[0] <= value && this._extent[1] >= value;
};
OrdinalScale.prototype.getOrdinalMeta = function () {
return this._ordinalMeta;
};
OrdinalScale.prototype.calcNiceTicks = function () {};
OrdinalScale.prototype.calcNiceExtent = function () {};
OrdinalScale.type = 'ordinal';
return OrdinalScale;
}(Scale);
Scale.registerClass(OrdinalScale);
export default OrdinalScale;