LegendModel.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 realy 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 not specified in option, use availableNames as data,
  149. // which is convinient for user preparing option.
  150. var rawData = this.get('data') || potentialData;
  151. var legendData = zrUtil.map(rawData, function (dataItem) {
  152. // Can be string or number
  153. if (zrUtil.isString(dataItem) || zrUtil.isNumber(dataItem)) {
  154. dataItem = {
  155. name: dataItem
  156. };
  157. }
  158. return new Model(dataItem, this, this.ecModel);
  159. }, this);
  160. /**
  161. * @type {Array.<module:echarts/model/Model>}
  162. * @private
  163. */
  164. this._data = legendData;
  165. };
  166. LegendModel.prototype.getData = function () {
  167. return this._data;
  168. };
  169. LegendModel.prototype.select = function (name) {
  170. var selected = this.option.selected;
  171. var selectedMode = this.get('selectedMode');
  172. if (selectedMode === 'single') {
  173. var data = this._data;
  174. zrUtil.each(data, function (dataItem) {
  175. selected[dataItem.get('name')] = false;
  176. });
  177. }
  178. selected[name] = true;
  179. };
  180. LegendModel.prototype.unSelect = function (name) {
  181. if (this.get('selectedMode') !== 'single') {
  182. this.option.selected[name] = false;
  183. }
  184. };
  185. LegendModel.prototype.toggleSelected = function (name) {
  186. var selected = this.option.selected; // Default is true
  187. if (!selected.hasOwnProperty(name)) {
  188. selected[name] = true;
  189. }
  190. this[selected[name] ? 'unSelect' : 'select'](name);
  191. };
  192. LegendModel.prototype.allSelect = function () {
  193. var data = this._data;
  194. var selected = this.option.selected;
  195. zrUtil.each(data, function (dataItem) {
  196. selected[dataItem.get('name', true)] = true;
  197. });
  198. };
  199. LegendModel.prototype.inverseSelect = function () {
  200. var data = this._data;
  201. var selected = this.option.selected;
  202. zrUtil.each(data, function (dataItem) {
  203. var name = dataItem.get('name', true); // Initially, default value is true
  204. if (!selected.hasOwnProperty(name)) {
  205. selected[name] = true;
  206. }
  207. selected[name] = !selected[name];
  208. });
  209. };
  210. LegendModel.prototype.isSelected = function (name) {
  211. var selected = this.option.selected;
  212. return !(selected.hasOwnProperty(name) && !selected[name]) && zrUtil.indexOf(this._availableNames, name) >= 0;
  213. };
  214. LegendModel.prototype.getOrient = function () {
  215. return this.get('orient') === 'vertical' ? {
  216. index: 1,
  217. name: 'vertical'
  218. } : {
  219. index: 0,
  220. name: 'horizontal'
  221. };
  222. };
  223. LegendModel.type = 'legend.plain';
  224. LegendModel.dependencies = ['series'];
  225. LegendModel.defaultOption = {
  226. // zlevel: 0,
  227. z: 4,
  228. show: true,
  229. orient: 'horizontal',
  230. left: 'center',
  231. // right: 'center',
  232. top: 0,
  233. // bottom: null,
  234. align: 'auto',
  235. backgroundColor: 'rgba(0,0,0,0)',
  236. borderColor: '#ccc',
  237. borderRadius: 0,
  238. borderWidth: 0,
  239. padding: 5,
  240. itemGap: 10,
  241. itemWidth: 25,
  242. itemHeight: 14,
  243. symbolRotate: 'inherit',
  244. symbolKeepAspect: true,
  245. inactiveColor: '#ccc',
  246. inactiveBorderColor: '#ccc',
  247. inactiveBorderWidth: 'auto',
  248. itemStyle: {
  249. color: 'inherit',
  250. opacity: 'inherit',
  251. borderColor: 'inherit',
  252. borderWidth: 'auto',
  253. borderCap: 'inherit',
  254. borderJoin: 'inherit',
  255. borderDashOffset: 'inherit',
  256. borderMiterLimit: 'inherit'
  257. },
  258. lineStyle: {
  259. width: 'auto',
  260. color: 'inherit',
  261. inactiveColor: '#ccc',
  262. inactiveWidth: 2,
  263. opacity: 'inherit',
  264. type: 'inherit',
  265. cap: 'inherit',
  266. join: 'inherit',
  267. dashOffset: 'inherit',
  268. miterLimit: 'inherit'
  269. },
  270. textStyle: {
  271. color: '#333'
  272. },
  273. selectedMode: true,
  274. selector: false,
  275. selectorLabel: {
  276. show: true,
  277. borderRadius: 10,
  278. padding: [3, 5, 3, 5],
  279. fontSize: 12,
  280. fontFamily: 'sans-serif',
  281. color: '#666',
  282. borderWidth: 1,
  283. borderColor: '#666'
  284. },
  285. emphasis: {
  286. selectorLabel: {
  287. show: true,
  288. color: '#eee',
  289. backgroundColor: '#666'
  290. }
  291. },
  292. selectorPosition: 'auto',
  293. selectorItemGap: 7,
  294. selectorButtonGap: 10,
  295. tooltip: {
  296. show: false
  297. }
  298. };
  299. return LegendModel;
  300. }(ComponentModel);
  301. export default LegendModel;