legendAction.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. var echarts = require("../../echarts");
  20. var zrUtil = require("zrender/lib/core/util");
  21. /*
  22. * Licensed to the Apache Software Foundation (ASF) under one
  23. * or more contributor license agreements. See the NOTICE file
  24. * distributed with this work for additional information
  25. * regarding copyright ownership. The ASF licenses this file
  26. * to you under the Apache License, Version 2.0 (the
  27. * "License"); you may not use this file except in compliance
  28. * with the License. You may obtain a copy of the License at
  29. *
  30. * http://www.apache.org/licenses/LICENSE-2.0
  31. *
  32. * Unless required by applicable law or agreed to in writing,
  33. * software distributed under the License is distributed on an
  34. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  35. * KIND, either express or implied. See the License for the
  36. * specific language governing permissions and limitations
  37. * under the License.
  38. */
  39. function legendSelectActionHandler(methodName, payload, ecModel) {
  40. var selectedMap = {};
  41. var isToggleSelect = methodName === 'toggleSelected';
  42. var isSelected; // Update all legend components
  43. ecModel.eachComponent('legend', function (legendModel) {
  44. if (isToggleSelect && isSelected != null) {
  45. // Force other legend has same selected status
  46. // Or the first is toggled to true and other are toggled to false
  47. // In the case one legend has some item unSelected in option. And if other legend
  48. // doesn't has the item, they will assume it is selected.
  49. legendModel[isSelected ? 'select' : 'unSelect'](payload.name);
  50. } else if (methodName === 'allSelect' || methodName === 'inverseSelect') {
  51. legendModel[methodName]();
  52. } else {
  53. legendModel[methodName](payload.name);
  54. isSelected = legendModel.isSelected(payload.name);
  55. }
  56. var legendData = legendModel.getData();
  57. zrUtil.each(legendData, function (model) {
  58. var name = model.get('name'); // Wrap element
  59. if (name === '\n' || name === '') {
  60. return;
  61. }
  62. var isItemSelected = legendModel.isSelected(name);
  63. if (selectedMap.hasOwnProperty(name)) {
  64. // Unselected if any legend is unselected
  65. selectedMap[name] = selectedMap[name] && isItemSelected;
  66. } else {
  67. selectedMap[name] = isItemSelected;
  68. }
  69. });
  70. }); // Return the event explicitly
  71. return methodName === 'allSelect' || methodName === 'inverseSelect' ? {
  72. selected: selectedMap
  73. } : {
  74. name: payload.name,
  75. selected: selectedMap
  76. };
  77. }
  78. /**
  79. * @event legendToggleSelect
  80. * @type {Object}
  81. * @property {string} type 'legendToggleSelect'
  82. * @property {string} [from]
  83. * @property {string} name Series name or data item name
  84. */
  85. echarts.registerAction('legendToggleSelect', 'legendselectchanged', zrUtil.curry(legendSelectActionHandler, 'toggleSelected'));
  86. echarts.registerAction('legendAllSelect', 'legendselectall', zrUtil.curry(legendSelectActionHandler, 'allSelect'));
  87. echarts.registerAction('legendInverseSelect', 'legendinverseselect', zrUtil.curry(legendSelectActionHandler, 'inverseSelect'));
  88. /**
  89. * @event legendSelect
  90. * @type {Object}
  91. * @property {string} type 'legendSelect'
  92. * @property {string} name Series name or data item name
  93. */
  94. echarts.registerAction('legendSelect', 'legendselected', zrUtil.curry(legendSelectActionHandler, 'select'));
  95. /**
  96. * @event legendUnSelect
  97. * @type {Object}
  98. * @property {string} type 'legendUnSelect'
  99. * @property {string} name Series name or data item name
  100. */
  101. echarts.registerAction('legendUnSelect', 'legendunselected', zrUtil.curry(legendSelectActionHandler, 'unSelect'));