LegendModel.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 Model from '../../model/Model.js';
  43. import { isNameSpecified } from '../../util/model.js';
  44. import ComponentModel from '../../model/Component.js';
  45. import tokens from '../../visual/tokens.js';
  46. var getDefaultSelectorOptions = function (ecModel, type) {
  47. if (type === 'all') {
  48. return {
  49. type: 'all',
  50. title: ecModel.getLocaleModel().get(['legend', 'selector', 'all'])
  51. };
  52. } else if (type === 'inverse') {
  53. return {
  54. type: 'inverse',
  55. title: ecModel.getLocaleModel().get(['legend', 'selector', 'inverse'])
  56. };
  57. }
  58. };
  59. var LegendModel = /** @class */function (_super) {
  60. __extends(LegendModel, _super);
  61. function LegendModel() {
  62. var _this = _super !== null && _super.apply(this, arguments) || this;
  63. _this.type = LegendModel.type;
  64. _this.layoutMode = {
  65. type: 'box',
  66. // legend.width/height are maxWidth/maxHeight actually,
  67. // whereas real width/height is calculated by its content.
  68. // (Setting {left: 10, right: 10} does not make sense).
  69. // So consider the case:
  70. // `setOption({legend: {left: 10});`
  71. // then `setOption({legend: {right: 10});`
  72. // The previous `left` should be cleared by setting `ignoreSize`.
  73. ignoreSize: true
  74. };
  75. return _this;
  76. }
  77. LegendModel.prototype.init = function (option, parentModel, ecModel) {
  78. this.mergeDefaultAndTheme(option, ecModel);
  79. option.selected = option.selected || {};
  80. this._updateSelector(option);
  81. };
  82. LegendModel.prototype.mergeOption = function (option, ecModel) {
  83. _super.prototype.mergeOption.call(this, option, ecModel);
  84. this._updateSelector(option);
  85. };
  86. LegendModel.prototype._updateSelector = function (option) {
  87. var selector = option.selector;
  88. var ecModel = this.ecModel;
  89. if (selector === true) {
  90. selector = option.selector = ['all', 'inverse'];
  91. }
  92. if (zrUtil.isArray(selector)) {
  93. zrUtil.each(selector, function (item, index) {
  94. zrUtil.isString(item) && (item = {
  95. type: item
  96. });
  97. selector[index] = zrUtil.merge(item, getDefaultSelectorOptions(ecModel, item.type));
  98. });
  99. }
  100. };
  101. LegendModel.prototype.optionUpdated = function () {
  102. this._updateData(this.ecModel);
  103. var legendData = this._data;
  104. // If selectedMode is single, try to select one
  105. if (legendData[0] && this.get('selectedMode') === 'single') {
  106. var hasSelected = false;
  107. // If has any selected in option.selected
  108. for (var i = 0; i < legendData.length; i++) {
  109. var name_1 = legendData[i].get('name');
  110. if (this.isSelected(name_1)) {
  111. // Force to unselect others
  112. this.select(name_1);
  113. hasSelected = true;
  114. break;
  115. }
  116. }
  117. // Try select the first if selectedMode is single
  118. !hasSelected && this.select(legendData[0].get('name'));
  119. }
  120. };
  121. LegendModel.prototype._updateData = function (ecModel) {
  122. var potentialData = [];
  123. var availableNames = [];
  124. ecModel.eachRawSeries(function (seriesModel) {
  125. var seriesName = seriesModel.name;
  126. availableNames.push(seriesName);
  127. var isPotential;
  128. if (seriesModel.legendVisualProvider) {
  129. var provider = seriesModel.legendVisualProvider;
  130. var names = provider.getAllNames();
  131. if (!ecModel.isSeriesFiltered(seriesModel)) {
  132. availableNames = availableNames.concat(names);
  133. }
  134. if (names.length) {
  135. potentialData = potentialData.concat(names);
  136. } else {
  137. isPotential = true;
  138. }
  139. } else {
  140. isPotential = true;
  141. }
  142. if (isPotential && isNameSpecified(seriesModel)) {
  143. potentialData.push(seriesModel.name);
  144. }
  145. });
  146. /**
  147. * @type {Array.<string>}
  148. * @private
  149. */
  150. this._availableNames = availableNames;
  151. // If legend.data is not specified in option, use availableNames as data,
  152. // which is convenient for user preparing option.
  153. var rawData = this.get('data') || potentialData;
  154. var legendNameMap = zrUtil.createHashMap();
  155. var legendData = zrUtil.map(rawData, function (dataItem) {
  156. // Can be string or number
  157. if (zrUtil.isString(dataItem) || zrUtil.isNumber(dataItem)) {
  158. dataItem = {
  159. name: dataItem
  160. };
  161. }
  162. if (legendNameMap.get(dataItem.name)) {
  163. // remove legend name duplicate
  164. return null;
  165. }
  166. legendNameMap.set(dataItem.name, true);
  167. return new Model(dataItem, this, this.ecModel);
  168. }, this);
  169. /**
  170. * @type {Array.<module:echarts/model/Model>}
  171. * @private
  172. */
  173. this._data = zrUtil.filter(legendData, function (item) {
  174. return !!item;
  175. });
  176. };
  177. LegendModel.prototype.getData = function () {
  178. return this._data;
  179. };
  180. LegendModel.prototype.select = function (name) {
  181. var selected = this.option.selected;
  182. var selectedMode = this.get('selectedMode');
  183. if (selectedMode === 'single') {
  184. var data = this._data;
  185. zrUtil.each(data, function (dataItem) {
  186. selected[dataItem.get('name')] = false;
  187. });
  188. }
  189. selected[name] = true;
  190. };
  191. LegendModel.prototype.unSelect = function (name) {
  192. if (this.get('selectedMode') !== 'single') {
  193. this.option.selected[name] = false;
  194. }
  195. };
  196. LegendModel.prototype.toggleSelected = function (name) {
  197. var selected = this.option.selected;
  198. // Default is true
  199. if (!selected.hasOwnProperty(name)) {
  200. selected[name] = true;
  201. }
  202. this[selected[name] ? 'unSelect' : 'select'](name);
  203. };
  204. LegendModel.prototype.allSelect = function () {
  205. var data = this._data;
  206. var selected = this.option.selected;
  207. zrUtil.each(data, function (dataItem) {
  208. selected[dataItem.get('name', true)] = true;
  209. });
  210. };
  211. LegendModel.prototype.inverseSelect = function () {
  212. var data = this._data;
  213. var selected = this.option.selected;
  214. zrUtil.each(data, function (dataItem) {
  215. var name = dataItem.get('name', true);
  216. // Initially, default value is true
  217. if (!selected.hasOwnProperty(name)) {
  218. selected[name] = true;
  219. }
  220. selected[name] = !selected[name];
  221. });
  222. };
  223. LegendModel.prototype.isSelected = function (name) {
  224. var selected = this.option.selected;
  225. return !(selected.hasOwnProperty(name) && !selected[name]) && zrUtil.indexOf(this._availableNames, name) >= 0;
  226. };
  227. LegendModel.prototype.getOrient = function () {
  228. return this.get('orient') === 'vertical' ? {
  229. index: 1,
  230. name: 'vertical'
  231. } : {
  232. index: 0,
  233. name: 'horizontal'
  234. };
  235. };
  236. LegendModel.type = 'legend.plain';
  237. LegendModel.dependencies = ['series'];
  238. LegendModel.defaultOption = {
  239. // zlevel: 0,
  240. z: 4,
  241. show: true,
  242. orient: 'horizontal',
  243. left: 'center',
  244. // right: 'center',
  245. // top: 0,
  246. bottom: tokens.size.m,
  247. align: 'auto',
  248. backgroundColor: tokens.color.transparent,
  249. borderColor: tokens.color.border,
  250. borderRadius: 0,
  251. borderWidth: 0,
  252. padding: 5,
  253. itemGap: 8,
  254. itemWidth: 25,
  255. itemHeight: 14,
  256. symbolRotate: 'inherit',
  257. symbolKeepAspect: true,
  258. inactiveColor: tokens.color.disabled,
  259. inactiveBorderColor: tokens.color.disabled,
  260. inactiveBorderWidth: 'auto',
  261. itemStyle: {
  262. color: 'inherit',
  263. opacity: 'inherit',
  264. borderColor: 'inherit',
  265. borderWidth: 'auto',
  266. borderCap: 'inherit',
  267. borderJoin: 'inherit',
  268. borderDashOffset: 'inherit',
  269. borderMiterLimit: 'inherit'
  270. },
  271. lineStyle: {
  272. width: 'auto',
  273. color: 'inherit',
  274. inactiveColor: tokens.color.disabled,
  275. inactiveWidth: 2,
  276. opacity: 'inherit',
  277. type: 'inherit',
  278. cap: 'inherit',
  279. join: 'inherit',
  280. dashOffset: 'inherit',
  281. miterLimit: 'inherit'
  282. },
  283. textStyle: {
  284. color: tokens.color.secondary
  285. },
  286. selectedMode: true,
  287. selector: false,
  288. selectorLabel: {
  289. show: true,
  290. borderRadius: 10,
  291. padding: [3, 5, 3, 5],
  292. fontSize: 12,
  293. fontFamily: 'sans-serif',
  294. color: tokens.color.tertiary,
  295. borderWidth: 1,
  296. borderColor: tokens.color.border
  297. },
  298. emphasis: {
  299. selectorLabel: {
  300. show: true,
  301. color: tokens.color.quaternary
  302. }
  303. },
  304. selectorPosition: 'auto',
  305. selectorItemGap: 7,
  306. selectorButtonGap: 10,
  307. tooltip: {
  308. show: false
  309. },
  310. triggerEvent: false
  311. };
  312. return LegendModel;
  313. }(ComponentModel);
  314. export default LegendModel;