Chart.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 _util = require("zrender/lib/core/util");
  20. var each = _util.each;
  21. var Group = require("zrender/lib/container/Group");
  22. var componentUtil = require("../util/component");
  23. var clazzUtil = require("../util/clazz");
  24. var modelUtil = require("../util/model");
  25. var graphicUtil = require("../util/graphic");
  26. var _task = require("../stream/task");
  27. var createTask = _task.createTask;
  28. var createRenderPlanner = require("../chart/helper/createRenderPlanner");
  29. /*
  30. * Licensed to the Apache Software Foundation (ASF) under one
  31. * or more contributor license agreements. See the NOTICE file
  32. * distributed with this work for additional information
  33. * regarding copyright ownership. The ASF licenses this file
  34. * to you under the Apache License, Version 2.0 (the
  35. * "License"); you may not use this file except in compliance
  36. * with the License. You may obtain a copy of the License at
  37. *
  38. * http://www.apache.org/licenses/LICENSE-2.0
  39. *
  40. * Unless required by applicable law or agreed to in writing,
  41. * software distributed under the License is distributed on an
  42. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  43. * KIND, either express or implied. See the License for the
  44. * specific language governing permissions and limitations
  45. * under the License.
  46. */
  47. var inner = modelUtil.makeInner();
  48. var renderPlanner = createRenderPlanner();
  49. function Chart() {
  50. /**
  51. * @type {module:zrender/container/Group}
  52. * @readOnly
  53. */
  54. this.group = new Group();
  55. /**
  56. * @type {string}
  57. * @readOnly
  58. */
  59. this.uid = componentUtil.getUID('viewChart');
  60. this.renderTask = createTask({
  61. plan: renderTaskPlan,
  62. reset: renderTaskReset
  63. });
  64. this.renderTask.context = {
  65. view: this
  66. };
  67. }
  68. Chart.prototype = {
  69. type: 'chart',
  70. /**
  71. * Init the chart.
  72. * @param {module:echarts/model/Global} ecModel
  73. * @param {module:echarts/ExtensionAPI} api
  74. */
  75. init: function (ecModel, api) {},
  76. /**
  77. * Render the chart.
  78. * @param {module:echarts/model/Series} seriesModel
  79. * @param {module:echarts/model/Global} ecModel
  80. * @param {module:echarts/ExtensionAPI} api
  81. * @param {Object} payload
  82. */
  83. render: function (seriesModel, ecModel, api, payload) {},
  84. /**
  85. * Highlight series or specified data item.
  86. * @param {module:echarts/model/Series} seriesModel
  87. * @param {module:echarts/model/Global} ecModel
  88. * @param {module:echarts/ExtensionAPI} api
  89. * @param {Object} payload
  90. */
  91. highlight: function (seriesModel, ecModel, api, payload) {
  92. toggleHighlight(seriesModel.getData(), payload, 'emphasis');
  93. },
  94. /**
  95. * Downplay series or specified data item.
  96. * @param {module:echarts/model/Series} seriesModel
  97. * @param {module:echarts/model/Global} ecModel
  98. * @param {module:echarts/ExtensionAPI} api
  99. * @param {Object} payload
  100. */
  101. downplay: function (seriesModel, ecModel, api, payload) {
  102. toggleHighlight(seriesModel.getData(), payload, 'normal');
  103. },
  104. /**
  105. * Remove self.
  106. * @param {module:echarts/model/Global} ecModel
  107. * @param {module:echarts/ExtensionAPI} api
  108. */
  109. remove: function (ecModel, api) {
  110. this.group.removeAll();
  111. },
  112. /**
  113. * Dispose self.
  114. * @param {module:echarts/model/Global} ecModel
  115. * @param {module:echarts/ExtensionAPI} api
  116. */
  117. dispose: function () {},
  118. /**
  119. * Rendering preparation in progressive mode.
  120. * @param {module:echarts/model/Series} seriesModel
  121. * @param {module:echarts/model/Global} ecModel
  122. * @param {module:echarts/ExtensionAPI} api
  123. * @param {Object} payload
  124. */
  125. incrementalPrepareRender: null,
  126. /**
  127. * Render in progressive mode.
  128. * @param {Object} params See taskParams in `stream/task.js`
  129. * @param {module:echarts/model/Series} seriesModel
  130. * @param {module:echarts/model/Global} ecModel
  131. * @param {module:echarts/ExtensionAPI} api
  132. * @param {Object} payload
  133. */
  134. incrementalRender: null,
  135. /**
  136. * Update transform directly.
  137. * @param {module:echarts/model/Series} seriesModel
  138. * @param {module:echarts/model/Global} ecModel
  139. * @param {module:echarts/ExtensionAPI} api
  140. * @param {Object} payload
  141. * @return {Object} {update: true}
  142. */
  143. updateTransform: null,
  144. /**
  145. * The view contains the given point.
  146. * @interface
  147. * @param {Array.<number>} point
  148. * @return {boolean}
  149. */
  150. // containPoint: function () {}
  151. /**
  152. * @param {string} eventType
  153. * @param {Object} query
  154. * @param {module:zrender/Element} targetEl
  155. * @param {Object} packedEvent
  156. * @return {boolen} Pass only when return `true`.
  157. */
  158. filterForExposedEvent: null
  159. };
  160. var chartProto = Chart.prototype;
  161. chartProto.updateView = chartProto.updateLayout = chartProto.updateVisual = function (seriesModel, ecModel, api, payload) {
  162. this.render(seriesModel, ecModel, api, payload);
  163. };
  164. /**
  165. * Set state of single element
  166. * @param {module:zrender/Element} el
  167. * @param {string} state 'normal'|'emphasis'
  168. * @param {number} highlightDigit
  169. */
  170. function elSetState(el, state, highlightDigit) {
  171. if (el) {
  172. el.trigger(state, highlightDigit);
  173. if (el.isGroup // Simple optimize.
  174. && !graphicUtil.isHighDownDispatcher(el)) {
  175. for (var i = 0, len = el.childCount(); i < len; i++) {
  176. elSetState(el.childAt(i), state, highlightDigit);
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * @param {module:echarts/data/List} data
  183. * @param {Object} payload
  184. * @param {string} state 'normal'|'emphasis'
  185. */
  186. function toggleHighlight(data, payload, state) {
  187. var dataIndex = modelUtil.queryDataIndex(data, payload);
  188. var highlightDigit = payload && payload.highlightKey != null ? graphicUtil.getHighlightDigit(payload.highlightKey) : null;
  189. if (dataIndex != null) {
  190. each(modelUtil.normalizeToArray(dataIndex), function (dataIdx) {
  191. elSetState(data.getItemGraphicEl(dataIdx), state, highlightDigit);
  192. });
  193. } else {
  194. data.eachItemGraphicEl(function (el) {
  195. elSetState(el, state, highlightDigit);
  196. });
  197. }
  198. } // Enable Chart.extend.
  199. clazzUtil.enableClassExtend(Chart, ['dispose']); // Add capability of registerClass, getClass, hasClass, registerSubTypeDefaulter and so on.
  200. clazzUtil.enableClassManagement(Chart, {
  201. registerWhenExtend: true
  202. });
  203. Chart.markUpdateMethod = function (payload, methodName) {
  204. inner(payload).updateMethod = methodName;
  205. };
  206. function renderTaskPlan(context) {
  207. return renderPlanner(context.model);
  208. }
  209. function renderTaskReset(context) {
  210. var seriesModel = context.model;
  211. var ecModel = context.ecModel;
  212. var api = context.api;
  213. var payload = context.payload; // ???! remove updateView updateVisual
  214. var progressiveRender = seriesModel.pipelineContext.progressiveRender;
  215. var view = context.view;
  216. var updateMethod = payload && inner(payload).updateMethod;
  217. var methodName = progressiveRender ? 'incrementalPrepareRender' : updateMethod && view[updateMethod] ? updateMethod // `appendData` is also supported when data amount
  218. // is less than progressive threshold.
  219. : 'render';
  220. if (methodName !== 'render') {
  221. view[methodName](seriesModel, ecModel, api, payload);
  222. }
  223. return progressMethodMap[methodName];
  224. }
  225. var progressMethodMap = {
  226. incrementalPrepareRender: {
  227. progress: function (params, context) {
  228. context.view.incrementalRender(params, context.model, context.ecModel, context.api, context.payload);
  229. }
  230. },
  231. render: {
  232. // Put view.render in `progress` to support appendData. But in this case
  233. // view.render should not be called in reset, otherwise it will be called
  234. // twise. Use `forceFirstProgress` to make sure that view.render is called
  235. // in any cases.
  236. forceFirstProgress: true,
  237. progress: function (params, context) {
  238. context.view.render(context.model, context.ecModel, context.api, context.payload);
  239. }
  240. }
  241. };
  242. var _default = Chart;
  243. module.exports = _default;