VisualMapModel.js 15 KB

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