VisualMapModel.js 15 KB

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