LegendModel.js 9.8 KB

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