VisualMapModel.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 optionSeriesId = this.option.seriesId;
  107. var optionSeriesIndex = this.option.seriesIndex;
  108. if (optionSeriesIndex == null && optionSeriesId == null) {
  109. optionSeriesIndex = 'all';
  110. }
  111. var seriesModels = modelUtil.queryReferringComponents(this.ecModel, 'series', {
  112. index: optionSeriesIndex,
  113. id: optionSeriesId
  114. }, {
  115. useDefault: false,
  116. enableAll: true,
  117. enableNone: false
  118. }).models;
  119. return zrUtil.map(seriesModels, function (seriesModel) {
  120. return seriesModel.componentIndex;
  121. });
  122. };
  123. /**
  124. * @public
  125. */
  126. VisualMapModel.prototype.eachTargetSeries = function (callback, context) {
  127. zrUtil.each(this.getTargetSeriesIndices(), function (seriesIndex) {
  128. var seriesModel = this.ecModel.getSeriesByIndex(seriesIndex);
  129. if (seriesModel) {
  130. callback.call(context, seriesModel);
  131. }
  132. }, this);
  133. };
  134. /**
  135. * @pubilc
  136. */
  137. VisualMapModel.prototype.isTargetSeries = function (seriesModel) {
  138. var is = false;
  139. this.eachTargetSeries(function (model) {
  140. model === seriesModel && (is = true);
  141. });
  142. return is;
  143. };
  144. /**
  145. * @example
  146. * this.formatValueText(someVal); // format single numeric value to text.
  147. * this.formatValueText(someVal, true); // format single category value to text.
  148. * this.formatValueText([min, max]); // format numeric min-max to text.
  149. * this.formatValueText([this.dataBound[0], max]); // using data lower bound.
  150. * this.formatValueText([min, this.dataBound[1]]); // using data upper bound.
  151. *
  152. * @param value Real value, or this.dataBound[0 or 1].
  153. * @param isCategory Only available when value is number.
  154. * @param edgeSymbols Open-close symbol when value is interval.
  155. * @protected
  156. */
  157. VisualMapModel.prototype.formatValueText = function (value, isCategory, edgeSymbols) {
  158. var option = this.option;
  159. var precision = option.precision;
  160. var dataBound = this.dataBound;
  161. var formatter = option.formatter;
  162. var isMinMax;
  163. edgeSymbols = edgeSymbols || ['<', '>'];
  164. if (zrUtil.isArray(value)) {
  165. value = value.slice();
  166. isMinMax = true;
  167. }
  168. var textValue = isCategory ? value // Value is string when isCategory
  169. : isMinMax ? [toFixed(value[0]), toFixed(value[1])] : toFixed(value);
  170. if (zrUtil.isString(formatter)) {
  171. return formatter.replace('{value}', isMinMax ? textValue[0] : textValue).replace('{value2}', isMinMax ? textValue[1] : textValue);
  172. } else if (zrUtil.isFunction(formatter)) {
  173. return isMinMax ? formatter(value[0], value[1]) : formatter(value);
  174. }
  175. if (isMinMax) {
  176. if (value[0] === dataBound[0]) {
  177. return edgeSymbols[0] + ' ' + textValue[1];
  178. } else if (value[1] === dataBound[1]) {
  179. return edgeSymbols[1] + ' ' + textValue[0];
  180. } else {
  181. return textValue[0] + ' - ' + textValue[1];
  182. }
  183. } else {
  184. // Format single value (includes category case).
  185. return textValue;
  186. }
  187. function toFixed(val) {
  188. return val === dataBound[0] ? 'min' : val === dataBound[1] ? 'max' : (+val).toFixed(Math.min(precision, 20));
  189. }
  190. };
  191. /**
  192. * @protected
  193. */
  194. VisualMapModel.prototype.resetExtent = function () {
  195. var thisOption = this.option;
  196. // Can not calculate data extent by data here.
  197. // Because series and data may be modified in processing stage.
  198. // So we do not support the feature "auto min/max".
  199. var extent = asc([thisOption.min, thisOption.max]);
  200. this._dataExtent = extent;
  201. };
  202. /**
  203. * PENDING:
  204. * delete this method if no outer usage.
  205. *
  206. * Return Concrete dimension. If null/undefined is returned, no dimension is used.
  207. */
  208. // getDataDimension(data: SeriesData) {
  209. // const optDim = this.option.dimension;
  210. // if (optDim != null) {
  211. // return data.getDimension(optDim);
  212. // }
  213. // const dimNames = data.dimensions;
  214. // for (let i = dimNames.length - 1; i >= 0; i--) {
  215. // const dimName = dimNames[i];
  216. // const dimInfo = data.getDimensionInfo(dimName);
  217. // if (!dimInfo.isCalculationCoord) {
  218. // return dimName;
  219. // }
  220. // }
  221. // }
  222. VisualMapModel.prototype.getDataDimensionIndex = function (data) {
  223. var optDim = this.option.dimension;
  224. if (optDim != null) {
  225. return data.getDimensionIndex(optDim);
  226. }
  227. var dimNames = data.dimensions;
  228. for (var i = dimNames.length - 1; i >= 0; i--) {
  229. var dimName = dimNames[i];
  230. var dimInfo = data.getDimensionInfo(dimName);
  231. if (!dimInfo.isCalculationCoord) {
  232. return dimInfo.storeDimIndex;
  233. }
  234. }
  235. };
  236. VisualMapModel.prototype.getExtent = function () {
  237. return this._dataExtent.slice();
  238. };
  239. VisualMapModel.prototype.completeVisualOption = function () {
  240. var ecModel = this.ecModel;
  241. var thisOption = this.option;
  242. var base = {
  243. inRange: thisOption.inRange,
  244. outOfRange: thisOption.outOfRange
  245. };
  246. var target = thisOption.target || (thisOption.target = {});
  247. var controller = thisOption.controller || (thisOption.controller = {});
  248. zrUtil.merge(target, base); // Do not override
  249. zrUtil.merge(controller, base); // Do not override
  250. var isCategory = this.isCategory();
  251. completeSingle.call(this, target);
  252. completeSingle.call(this, controller);
  253. completeInactive.call(this, target, 'inRange', 'outOfRange');
  254. // completeInactive.call(this, target, 'outOfRange', 'inRange');
  255. completeController.call(this, controller);
  256. function completeSingle(base) {
  257. // Compatible with ec2 dataRange.color.
  258. // The mapping order of dataRange.color is: [high value, ..., low value]
  259. // whereas inRange.color and outOfRange.color is [low value, ..., high value]
  260. // Notice: ec2 has no inverse.
  261. if (isArray(thisOption.color)
  262. // If there has been inRange: {symbol: ...}, adding color is a mistake.
  263. // So adding color only when no inRange defined.
  264. && !base.inRange) {
  265. base.inRange = {
  266. color: thisOption.color.slice().reverse()
  267. };
  268. }
  269. // Compatible with previous logic, always give a default color, otherwise
  270. // simple config with no inRange and outOfRange will not work.
  271. // Originally we use visualMap.color as the default color, but setOption at
  272. // the second time the default color will be erased. So we change to use
  273. // constant DEFAULT_COLOR.
  274. // If user do not want the default color, set inRange: {color: null}.
  275. base.inRange = base.inRange || {
  276. color: ecModel.get('gradientColor')
  277. };
  278. }
  279. function completeInactive(base, stateExist, stateAbsent) {
  280. var optExist = base[stateExist];
  281. var optAbsent = base[stateAbsent];
  282. if (optExist && !optAbsent) {
  283. optAbsent = base[stateAbsent] = {};
  284. each(optExist, function (visualData, visualType) {
  285. if (!VisualMapping.isValidType(visualType)) {
  286. return;
  287. }
  288. var defa = visualDefault.get(visualType, 'inactive', isCategory);
  289. if (defa != null) {
  290. optAbsent[visualType] = defa;
  291. // Compatibable with ec2:
  292. // Only inactive color to rgba(0,0,0,0) can not
  293. // make label transparent, so use opacity also.
  294. if (visualType === 'color' && !optAbsent.hasOwnProperty('opacity') && !optAbsent.hasOwnProperty('colorAlpha')) {
  295. optAbsent.opacity = [0, 0];
  296. }
  297. }
  298. });
  299. }
  300. }
  301. function completeController(controller) {
  302. var symbolExists = (controller.inRange || {}).symbol || (controller.outOfRange || {}).symbol;
  303. var symbolSizeExists = (controller.inRange || {}).symbolSize || (controller.outOfRange || {}).symbolSize;
  304. var inactiveColor = this.get('inactiveColor');
  305. var itemSymbol = this.getItemSymbol();
  306. var defaultSymbol = itemSymbol || 'roundRect';
  307. each(this.stateList, function (state) {
  308. var itemSize = this.itemSize;
  309. var visuals = controller[state];
  310. // Set inactive color for controller if no other color
  311. // attr (like colorAlpha) specified.
  312. if (!visuals) {
  313. visuals = controller[state] = {
  314. color: isCategory ? inactiveColor : [inactiveColor]
  315. };
  316. }
  317. // Consistent symbol and symbolSize if not specified.
  318. if (visuals.symbol == null) {
  319. visuals.symbol = symbolExists && zrUtil.clone(symbolExists) || (isCategory ? defaultSymbol : [defaultSymbol]);
  320. }
  321. if (visuals.symbolSize == null) {
  322. visuals.symbolSize = symbolSizeExists && zrUtil.clone(symbolSizeExists) || (isCategory ? itemSize[0] : [itemSize[0], itemSize[0]]);
  323. }
  324. // Filter none
  325. visuals.symbol = mapVisual(visuals.symbol, function (symbol) {
  326. return symbol === 'none' ? defaultSymbol : symbol;
  327. });
  328. // Normalize symbolSize
  329. var symbolSize = visuals.symbolSize;
  330. if (symbolSize != null) {
  331. var max_1 = -Infinity;
  332. // symbolSize can be object when categories defined.
  333. eachVisual(symbolSize, function (value) {
  334. value > max_1 && (max_1 = value);
  335. });
  336. visuals.symbolSize = mapVisual(symbolSize, function (value) {
  337. return linearMap(value, [0, max_1], [0, itemSize[0]], true);
  338. });
  339. }
  340. }, this);
  341. }
  342. };
  343. VisualMapModel.prototype.resetItemSize = function () {
  344. this.itemSize = [parseFloat(this.get('itemWidth')), parseFloat(this.get('itemHeight'))];
  345. };
  346. VisualMapModel.prototype.isCategory = function () {
  347. return !!this.option.categories;
  348. };
  349. /**
  350. * @public
  351. * @abstract
  352. */
  353. VisualMapModel.prototype.setSelected = function (selected) {};
  354. VisualMapModel.prototype.getSelected = function () {
  355. return null;
  356. };
  357. /**
  358. * @public
  359. * @abstract
  360. */
  361. VisualMapModel.prototype.getValueState = function (value) {
  362. return null;
  363. };
  364. /**
  365. * FIXME
  366. * Do not publish to thirt-part-dev temporarily
  367. * util the interface is stable. (Should it return
  368. * a function but not visual meta?)
  369. *
  370. * @pubilc
  371. * @abstract
  372. * @param getColorVisual
  373. * params: value, valueState
  374. * return: color
  375. * @return {Object} visualMeta
  376. * should includes {stops, outerColors}
  377. * outerColor means [colorBeyondMinValue, colorBeyondMaxValue]
  378. */
  379. VisualMapModel.prototype.getVisualMeta = function (getColorVisual) {
  380. return null;
  381. };
  382. VisualMapModel.type = 'visualMap';
  383. VisualMapModel.dependencies = ['series'];
  384. VisualMapModel.defaultOption = {
  385. show: true,
  386. // zlevel: 0,
  387. z: 4,
  388. // seriesIndex: 'all',
  389. min: 0,
  390. max: 200,
  391. left: 0,
  392. right: null,
  393. top: null,
  394. bottom: 0,
  395. itemWidth: null,
  396. itemHeight: null,
  397. inverse: false,
  398. orient: 'vertical',
  399. backgroundColor: tokens.color.transparent,
  400. borderColor: tokens.color.borderTint,
  401. contentColor: tokens.color.theme[0],
  402. inactiveColor: tokens.color.disabled,
  403. borderWidth: 0,
  404. padding: tokens.size.m,
  405. // 接受数组分别设定上右下左边距,同css
  406. textGap: 10,
  407. precision: 0,
  408. textStyle: {
  409. color: tokens.color.secondary // 值域文字颜色
  410. }
  411. };
  412. return VisualMapModel;
  413. }(ComponentModel);
  414. export default VisualMapModel;