LegendModel.js 9.7 KB

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