VisualMapModel.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import visualDefault from '../../visual/visualDefault.js';
  43. import VisualMapping from '../../visual/VisualMapping.js';
  44. import * as visualSolution from '../../visual/visualSolution.js';
  45. import * as modelUtil from '../../util/model.js';
  46. import * as numberUtil from '../../util/number.js';
  47. import ComponentModel from '../../model/Component.js';
  48. import tokens from '../../visual/tokens.js';
  49. var mapVisual = VisualMapping.mapVisual;
  50. var eachVisual = VisualMapping.eachVisual;
  51. var isArray = zrUtil.isArray;
  52. var each = zrUtil.each;
  53. var asc = numberUtil.asc;
  54. var linearMap = numberUtil.linearMap;
  55. var VisualMapModel = /** @class */function (_super) {
  56. __extends(VisualMapModel, _super);
  57. function VisualMapModel() {
  58. var _this = _super !== null && _super.apply(this, arguments) || this;
  59. _this.type = VisualMapModel.type;
  60. _this.stateList = ['inRange', 'outOfRange'];
  61. _this.replacableOptionKeys = ['inRange', 'outOfRange', 'target', 'controller', 'color'];
  62. _this.layoutMode = {
  63. type: 'box',
  64. ignoreSize: true
  65. };
  66. /**
  67. * [lowerBound, upperBound]
  68. */
  69. _this.dataBound = [-Infinity, Infinity];
  70. _this.targetVisuals = {};
  71. _this.controllerVisuals = {};
  72. return _this;
  73. }
  74. VisualMapModel.prototype.init = function (option, parentModel, ecModel) {
  75. this.mergeDefaultAndTheme(option, ecModel);
  76. };
  77. /**
  78. * @protected
  79. */
  80. VisualMapModel.prototype.optionUpdated = function (newOption, isInit) {
  81. var thisOption = this.option;
  82. !isInit && visualSolution.replaceVisualOption(thisOption, newOption, this.replacableOptionKeys);
  83. this.textStyleModel = this.getModel('textStyle');
  84. this.resetItemSize();
  85. this.completeVisualOption();
  86. };
  87. /**
  88. * @protected
  89. */
  90. VisualMapModel.prototype.resetVisual = function (supplementVisualOption) {
  91. var stateList = this.stateList;
  92. supplementVisualOption = zrUtil.bind(supplementVisualOption, this);
  93. this.controllerVisuals = visualSolution.createVisualMappings(this.option.controller, stateList, supplementVisualOption);
  94. this.targetVisuals = visualSolution.createVisualMappings(this.option.target, stateList, supplementVisualOption);
  95. };
  96. /**
  97. * @public
  98. */
  99. VisualMapModel.prototype.getItemSymbol = function () {
  100. return null;
  101. };
  102. /**
  103. * @return An array of series indices.
  104. */
  105. VisualMapModel.prototype.getTargetSeriesIndices = function () {
  106. var _this = this;
  107. var seriesTargets = this.option.seriesTargets;
  108. if (seriesTargets) {
  109. // When seriesTargets is provided, collect all target series indices
  110. var indices_1 = [];
  111. each(seriesTargets, function (target) {
  112. if (target.seriesIndex != null) {
  113. indices_1.push(target.seriesIndex);
  114. } else if (target.seriesId != null) {
  115. // Find series by ID
  116. var seriesModel_1;
  117. _this.ecModel.eachSeries(function (series) {
  118. if (series.id === target.seriesId) {
  119. seriesModel_1 = series;
  120. }
  121. });
  122. if (seriesModel_1) {
  123. indices_1.push(seriesModel_1.componentIndex);
  124. }
  125. }
  126. });
  127. return indices_1;
  128. }
  129. var optionSeriesId = this.option.seriesId;
  130. var optionSeriesIndex = this.option.seriesIndex;
  131. if (optionSeriesIndex == null && optionSeriesId == null) {
  132. optionSeriesIndex = 'all';
  133. }
  134. var seriesModels = modelUtil.queryReferringComponents(this.ecModel, 'series', {
  135. index: optionSeriesIndex,
  136. id: optionSeriesId
  137. }, {
  138. useDefault: false,
  139. enableAll: true,
  140. enableNone: false
  141. }).models;
  142. return zrUtil.map(seriesModels, function (seriesModel) {
  143. return seriesModel.componentIndex;
  144. });
  145. };
  146. /**
  147. * @public
  148. */
  149. VisualMapModel.prototype.eachTargetSeries = function (callback, context) {
  150. zrUtil.each(this.getTargetSeriesIndices(), function (seriesIndex) {
  151. var seriesModel = this.ecModel.getSeriesByIndex(seriesIndex);
  152. if (seriesModel) {
  153. callback.call(context, seriesModel);
  154. }
  155. }, this);
  156. };
  157. /**
  158. * @pubilc
  159. */
  160. VisualMapModel.prototype.isTargetSeries = function (seriesModel) {
  161. var is = false;
  162. this.eachTargetSeries(function (model) {
  163. model === seriesModel && (is = true);
  164. });
  165. return is;
  166. };
  167. /**
  168. * @example
  169. * this.formatValueText(someVal); // format single numeric value to text.
  170. * this.formatValueText(someVal, true); // format single category value to text.
  171. * this.formatValueText([min, max]); // format numeric min-max to text.
  172. * this.formatValueText([this.dataBound[0], max]); // using data lower bound.
  173. * this.formatValueText([min, this.dataBound[1]]); // using data upper bound.
  174. *
  175. * @param value Real value, or this.dataBound[0 or 1].
  176. * @param isCategory Only available when value is number.
  177. * @param edgeSymbols Open-close symbol when value is interval.
  178. * @protected
  179. */
  180. VisualMapModel.prototype.formatValueText = function (value, isCategory, edgeSymbols) {
  181. var option = this.option;
  182. var precision = option.precision;
  183. var dataBound = this.dataBound;
  184. var formatter = option.formatter;
  185. var isMinMax;
  186. edgeSymbols = edgeSymbols || ['<', '>'];
  187. if (zrUtil.isArray(value)) {
  188. value = value.slice();
  189. isMinMax = true;
  190. }
  191. var textValue = isCategory ? value // Value is string when isCategory
  192. : isMinMax ? [toFixed(value[0]), toFixed(value[1])] : toFixed(value);
  193. if (zrUtil.isString(formatter)) {
  194. return formatter.replace('{value}', isMinMax ? textValue[0] : textValue).replace('{value2}', isMinMax ? textValue[1] : textValue);
  195. } else if (zrUtil.isFunction(formatter)) {
  196. return isMinMax ? formatter(value[0], value[1]) : formatter(value);
  197. }
  198. if (isMinMax) {
  199. if (value[0] === dataBound[0]) {
  200. return edgeSymbols[0] + ' ' + textValue[1];
  201. } else if (value[1] === dataBound[1]) {
  202. return edgeSymbols[1] + ' ' + textValue[0];
  203. } else {
  204. return textValue[0] + ' - ' + textValue[1];
  205. }
  206. } else {
  207. // Format single value (includes category case).
  208. return textValue;
  209. }
  210. function toFixed(val) {
  211. return val === dataBound[0] ? 'min' : val === dataBound[1] ? 'max' : (+val).toFixed(Math.min(precision, 20));
  212. }
  213. };
  214. /**
  215. * @protected
  216. */
  217. VisualMapModel.prototype.resetExtent = function () {
  218. var thisOption = this.option;
  219. // Can not calculate data extent by data here.
  220. // Because series and data may be modified in processing stage.
  221. // So we do not support the feature "auto min/max".
  222. var extent = asc([thisOption.min, thisOption.max]);
  223. this._dataExtent = extent;
  224. };
  225. /**
  226. * PENDING:
  227. * delete this method if no outer usage.
  228. *
  229. * Return Concrete dimension. If null/undefined is returned, no dimension is used.
  230. */
  231. // getDataDimension(data: SeriesData) {
  232. // const optDim = this.option.dimension;
  233. // if (optDim != null) {
  234. // return data.getDimension(optDim);
  235. // }
  236. // const dimNames = data.dimensions;
  237. // for (let i = dimNames.length - 1; i >= 0; i--) {
  238. // const dimName = dimNames[i];
  239. // const dimInfo = data.getDimensionInfo(dimName);
  240. // if (!dimInfo.isCalculationCoord) {
  241. // return dimName;
  242. // }
  243. // }
  244. // }
  245. VisualMapModel.prototype.getDimension = function (seriesIndex) {
  246. var _this = this;
  247. var seriesTargets = this.option.seriesTargets;
  248. if (seriesTargets) {
  249. var target = zrUtil.find(seriesTargets, function (target) {
  250. return target.seriesIndex != null && target.seriesIndex === seriesIndex || target.seriesId != null && target.seriesId === _this.ecModel.getSeriesByIndex(seriesIndex).id;
  251. });
  252. if (target) {
  253. return target.dimension;
  254. }
  255. }
  256. return this.option.dimension;
  257. };
  258. VisualMapModel.prototype.getDataDimensionIndex = function (data) {
  259. var seriesIndex = data.hostModel.seriesIndex;
  260. var optDim = this.getDimension(seriesIndex);
  261. if (optDim != null) {
  262. return data.getDimensionIndex(optDim);
  263. }
  264. var dimNames = data.dimensions;
  265. for (var i = dimNames.length - 1; i >= 0; i--) {
  266. var dimName = dimNames[i];
  267. var dimInfo = data.getDimensionInfo(dimName);
  268. if (!dimInfo.isCalculationCoord) {
  269. return dimInfo.storeDimIndex;
  270. }
  271. }
  272. };
  273. VisualMapModel.prototype.getExtent = function () {
  274. return this._dataExtent.slice();
  275. };
  276. VisualMapModel.prototype.completeVisualOption = function () {
  277. var ecModel = this.ecModel;
  278. var thisOption = this.option;
  279. var base = {
  280. inRange: thisOption.inRange,
  281. outOfRange: thisOption.outOfRange
  282. };
  283. var target = thisOption.target || (thisOption.target = {});
  284. var controller = thisOption.controller || (thisOption.controller = {});
  285. zrUtil.merge(target, base); // Do not override
  286. zrUtil.merge(controller, base); // Do not override
  287. var isCategory = this.isCategory();
  288. completeSingle.call(this, target);
  289. completeSingle.call(this, controller);
  290. completeInactive.call(this, target, 'inRange', 'outOfRange');
  291. // completeInactive.call(this, target, 'outOfRange', 'inRange');
  292. completeController.call(this, controller);
  293. function completeSingle(base) {
  294. // Compatible with ec2 dataRange.color.
  295. // The mapping order of dataRange.color is: [high value, ..., low value]
  296. // whereas inRange.color and outOfRange.color is [low value, ..., high value]
  297. // Notice: ec2 has no inverse.
  298. if (isArray(thisOption.color)
  299. // If there has been inRange: {symbol: ...}, adding color is a mistake.
  300. // So adding color only when no inRange defined.
  301. && !base.inRange) {
  302. base.inRange = {
  303. color: thisOption.color.slice().reverse()
  304. };
  305. }
  306. // Compatible with previous logic, always give a default color, otherwise
  307. // simple config with no inRange and outOfRange will not work.
  308. // Originally we use visualMap.color as the default color, but setOption at
  309. // the second time the default color will be erased. So we change to use
  310. // constant DEFAULT_COLOR.
  311. // If user do not want the default color, set inRange: {color: null}.
  312. base.inRange = base.inRange || {
  313. color: ecModel.get('gradientColor')
  314. };
  315. }
  316. function completeInactive(base, stateExist, stateAbsent) {
  317. var optExist = base[stateExist];
  318. var optAbsent = base[stateAbsent];
  319. if (optExist && !optAbsent) {
  320. optAbsent = base[stateAbsent] = {};
  321. each(optExist, function (visualData, visualType) {
  322. if (!VisualMapping.isValidType(visualType)) {
  323. return;
  324. }
  325. var defa = visualDefault.get(visualType, 'inactive', isCategory);
  326. if (defa != null) {
  327. optAbsent[visualType] = defa;
  328. // Compatibable with ec2:
  329. // Only inactive color to rgba(0,0,0,0) can not
  330. // make label transparent, so use opacity also.
  331. if (visualType === 'color' && !optAbsent.hasOwnProperty('opacity') && !optAbsent.hasOwnProperty('colorAlpha')) {
  332. optAbsent.opacity = [0, 0];
  333. }
  334. }
  335. });
  336. }
  337. }
  338. function completeController(controller) {
  339. var symbolExists = (controller.inRange || {}).symbol || (controller.outOfRange || {}).symbol;
  340. var symbolSizeExists = (controller.inRange || {}).symbolSize || (controller.outOfRange || {}).symbolSize;
  341. var inactiveColor = this.get('inactiveColor');
  342. var itemSymbol = this.getItemSymbol();
  343. var defaultSymbol = itemSymbol || 'roundRect';
  344. each(this.stateList, function (state) {
  345. var itemSize = this.itemSize;
  346. var visuals = controller[state];
  347. // Set inactive color for controller if no other color
  348. // attr (like colorAlpha) specified.
  349. if (!visuals) {
  350. visuals = controller[state] = {
  351. color: isCategory ? inactiveColor : [inactiveColor]
  352. };
  353. }
  354. // Consistent symbol and symbolSize if not specified.
  355. if (visuals.symbol == null) {
  356. visuals.symbol = symbolExists && zrUtil.clone(symbolExists) || (isCategory ? defaultSymbol : [defaultSymbol]);
  357. }
  358. if (visuals.symbolSize == null) {
  359. visuals.symbolSize = symbolSizeExists && zrUtil.clone(symbolSizeExists) || (isCategory ? itemSize[0] : [itemSize[0], itemSize[0]]);
  360. }
  361. // Filter none
  362. visuals.symbol = mapVisual(visuals.symbol, function (symbol) {
  363. return symbol === 'none' ? defaultSymbol : symbol;
  364. });
  365. // Normalize symbolSize
  366. var symbolSize = visuals.symbolSize;
  367. if (symbolSize != null) {
  368. var max_1 = -Infinity;
  369. // symbolSize can be object when categories defined.
  370. eachVisual(symbolSize, function (value) {
  371. value > max_1 && (max_1 = value);
  372. });
  373. visuals.symbolSize = mapVisual(symbolSize, function (value) {
  374. return linearMap(value, [0, max_1], [0, itemSize[0]], true);
  375. });
  376. }
  377. }, this);
  378. }
  379. };
  380. VisualMapModel.prototype.resetItemSize = function () {
  381. this.itemSize = [parseFloat(this.get('itemWidth')), parseFloat(this.get('itemHeight'))];
  382. };
  383. VisualMapModel.prototype.isCategory = function () {
  384. return !!this.option.categories;
  385. };
  386. /**
  387. * @public
  388. * @abstract
  389. */
  390. VisualMapModel.prototype.setSelected = function (selected) {};
  391. VisualMapModel.prototype.getSelected = function () {
  392. return null;
  393. };
  394. /**
  395. * @public
  396. * @abstract
  397. */
  398. VisualMapModel.prototype.getValueState = function (value) {
  399. return null;
  400. };
  401. /**
  402. * FIXME
  403. * Do not publish to thirt-part-dev temporarily
  404. * util the interface is stable. (Should it return
  405. * a function but not visual meta?)
  406. *
  407. * @pubilc
  408. * @abstract
  409. * @param getColorVisual
  410. * params: value, valueState
  411. * return: color
  412. * @return {Object} visualMeta
  413. * should includes {stops, outerColors}
  414. * outerColor means [colorBeyondMinValue, colorBeyondMaxValue]
  415. */
  416. VisualMapModel.prototype.getVisualMeta = function (getColorVisual) {
  417. return null;
  418. };
  419. VisualMapModel.type = 'visualMap';
  420. VisualMapModel.dependencies = ['series'];
  421. VisualMapModel.defaultOption = {
  422. show: true,
  423. // zlevel: 0,
  424. z: 4,
  425. // seriesIndex: 'all',
  426. min: 0,
  427. max: 200,
  428. left: 0,
  429. right: null,
  430. top: null,
  431. bottom: 0,
  432. itemWidth: null,
  433. itemHeight: null,
  434. inverse: false,
  435. orient: 'vertical',
  436. backgroundColor: tokens.color.transparent,
  437. borderColor: tokens.color.borderTint,
  438. contentColor: tokens.color.theme[0],
  439. inactiveColor: tokens.color.disabled,
  440. borderWidth: 0,
  441. padding: tokens.size.m,
  442. // 接受数组分别设定上右下左边距,同css
  443. textGap: 10,
  444. precision: 0,
  445. textStyle: {
  446. color: tokens.color.secondary // 值域文字颜色
  447. }
  448. };
  449. return VisualMapModel;
  450. }(ComponentModel);
  451. export default VisualMapModel;