MarkerModel.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var echarts = require("../../echarts");
  22. var zrUtil = require("zrender/lib/core/util");
  23. var env = require("zrender/lib/core/env");
  24. var modelUtil = require("../../util/model");
  25. var formatUtil = require("../../util/format");
  26. var dataFormatMixin = require("../../model/mixin/dataFormat");
  27. /*
  28. * Licensed to the Apache Software Foundation (ASF) under one
  29. * or more contributor license agreements. See the NOTICE file
  30. * distributed with this work for additional information
  31. * regarding copyright ownership. The ASF licenses this file
  32. * to you under the Apache License, Version 2.0 (the
  33. * "License"); you may not use this file except in compliance
  34. * with the License. You may obtain a copy of the License at
  35. *
  36. * http://www.apache.org/licenses/LICENSE-2.0
  37. *
  38. * Unless required by applicable law or agreed to in writing,
  39. * software distributed under the License is distributed on an
  40. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  41. * KIND, either express or implied. See the License for the
  42. * specific language governing permissions and limitations
  43. * under the License.
  44. */
  45. var addCommas = formatUtil.addCommas;
  46. var encodeHTML = formatUtil.encodeHTML;
  47. function fillLabel(opt) {
  48. modelUtil.defaultEmphasis(opt, 'label', ['show']);
  49. }
  50. var MarkerModel = echarts.extendComponentModel({
  51. type: 'marker',
  52. dependencies: ['series', 'grid', 'polar', 'geo'],
  53. /**
  54. * @overrite
  55. */
  56. init: function (option, parentModel, ecModel) {
  57. this.mergeDefaultAndTheme(option, ecModel);
  58. this._mergeOption(option, ecModel, false, true);
  59. },
  60. /**
  61. * @return {boolean}
  62. */
  63. isAnimationEnabled: function () {
  64. if (env.node) {
  65. return false;
  66. }
  67. var hostSeries = this.__hostSeries;
  68. return this.getShallow('animation') && hostSeries && hostSeries.isAnimationEnabled();
  69. },
  70. /**
  71. * @overrite
  72. */
  73. mergeOption: function (newOpt, ecModel) {
  74. this._mergeOption(newOpt, ecModel, false, false);
  75. },
  76. _mergeOption: function (newOpt, ecModel, createdBySelf, isInit) {
  77. var MarkerModel = this.constructor;
  78. var modelPropName = this.mainType + 'Model';
  79. if (!createdBySelf) {
  80. ecModel.eachSeries(function (seriesModel) {
  81. var markerOpt = seriesModel.get(this.mainType, true);
  82. var markerModel = seriesModel[modelPropName];
  83. if (!markerOpt || !markerOpt.data) {
  84. seriesModel[modelPropName] = null;
  85. return;
  86. }
  87. if (!markerModel) {
  88. if (isInit) {
  89. // Default label emphasis `position` and `show`
  90. fillLabel(markerOpt);
  91. }
  92. zrUtil.each(markerOpt.data, function (item) {
  93. // FIXME Overwrite fillLabel method ?
  94. if (item instanceof Array) {
  95. fillLabel(item[0]);
  96. fillLabel(item[1]);
  97. } else {
  98. fillLabel(item);
  99. }
  100. });
  101. markerModel = new MarkerModel(markerOpt, this, ecModel);
  102. zrUtil.extend(markerModel, {
  103. mainType: this.mainType,
  104. // Use the same series index and name
  105. seriesIndex: seriesModel.seriesIndex,
  106. name: seriesModel.name,
  107. createdBySelf: true
  108. });
  109. markerModel.__hostSeries = seriesModel;
  110. } else {
  111. markerModel._mergeOption(markerOpt, ecModel, true);
  112. }
  113. seriesModel[modelPropName] = markerModel;
  114. }, this);
  115. }
  116. },
  117. formatTooltip: function (dataIndex, multipleSeries, dataType, renderMode) {
  118. var data = this.getData();
  119. var value = this.getRawValue(dataIndex);
  120. var formattedValue = zrUtil.isArray(value) ? zrUtil.map(value, addCommas).join(', ') : addCommas(value);
  121. var name = data.getName(dataIndex);
  122. var html = encodeHTML(this.name);
  123. var newLine = renderMode === 'html' ? '<br/>' : '\n';
  124. if (value != null || name) {
  125. html += newLine;
  126. }
  127. if (name) {
  128. html += encodeHTML(name);
  129. if (value != null) {
  130. html += ' : ';
  131. }
  132. }
  133. if (value != null) {
  134. html += encodeHTML(formattedValue);
  135. }
  136. return html;
  137. },
  138. getData: function () {
  139. return this._data;
  140. },
  141. setData: function (data) {
  142. this._data = data;
  143. }
  144. });
  145. zrUtil.mixin(MarkerModel, dataFormatMixin);
  146. var _default = MarkerModel;
  147. module.exports = _default;