VisualMapModel.js 15 KB

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