style.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 { isFunction, extend, createHashMap } from 'zrender/lib/core/util.js';
  41. import makeStyleMapper from '../model/mixin/makeStyleMapper.js';
  42. import { ITEM_STYLE_KEY_MAP } from '../model/mixin/itemStyle.js';
  43. import { LINE_STYLE_KEY_MAP } from '../model/mixin/lineStyle.js';
  44. import Model from '../model/Model.js';
  45. import { makeInner } from '../util/model.js';
  46. var inner = makeInner();
  47. var defaultStyleMappers = {
  48. itemStyle: makeStyleMapper(ITEM_STYLE_KEY_MAP, true),
  49. lineStyle: makeStyleMapper(LINE_STYLE_KEY_MAP, true)
  50. };
  51. var defaultColorKey = {
  52. lineStyle: 'stroke',
  53. itemStyle: 'fill'
  54. };
  55. function getStyleMapper(seriesModel, stylePath) {
  56. var styleMapper = seriesModel.visualStyleMapper || defaultStyleMappers[stylePath];
  57. if (!styleMapper) {
  58. console.warn("Unknown style type '" + stylePath + "'.");
  59. return defaultStyleMappers.itemStyle;
  60. }
  61. return styleMapper;
  62. }
  63. function getDefaultColorKey(seriesModel, stylePath) {
  64. // return defaultColorKey[stylePath] ||
  65. var colorKey = seriesModel.visualDrawType || defaultColorKey[stylePath];
  66. if (!colorKey) {
  67. console.warn("Unknown style type '" + stylePath + "'.");
  68. return 'fill';
  69. }
  70. return colorKey;
  71. }
  72. var seriesStyleTask = {
  73. createOnAllSeries: true,
  74. performRawSeries: true,
  75. reset: function (seriesModel, ecModel) {
  76. var data = seriesModel.getData();
  77. var stylePath = seriesModel.visualStyleAccessPath || 'itemStyle';
  78. // Set in itemStyle
  79. var styleModel = seriesModel.getModel(stylePath);
  80. var getStyle = getStyleMapper(seriesModel, stylePath);
  81. var globalStyle = getStyle(styleModel);
  82. var decalOption = styleModel.getShallow('decal');
  83. if (decalOption) {
  84. data.setVisual('decal', decalOption);
  85. decalOption.dirty = true;
  86. }
  87. // TODO
  88. var colorKey = getDefaultColorKey(seriesModel, stylePath);
  89. var color = globalStyle[colorKey];
  90. // TODO style callback
  91. var colorCallback = isFunction(color) ? color : null;
  92. var hasAutoColor = globalStyle.fill === 'auto' || globalStyle.stroke === 'auto';
  93. // Get from color palette by default.
  94. if (!globalStyle[colorKey] || colorCallback || hasAutoColor) {
  95. // Note: If some series has color specified (e.g., by itemStyle.color), we DO NOT
  96. // make it effect palette. Because some scenarios users need to make some series
  97. // transparent or as background, which should better not effect the palette.
  98. var colorPalette = seriesModel.getColorFromPalette(
  99. // TODO series count changed.
  100. seriesModel.name, null, ecModel.getSeriesCount());
  101. if (!globalStyle[colorKey]) {
  102. globalStyle[colorKey] = colorPalette;
  103. data.setVisual('colorFromPalette', true);
  104. }
  105. globalStyle.fill = globalStyle.fill === 'auto' || isFunction(globalStyle.fill) ? colorPalette : globalStyle.fill;
  106. globalStyle.stroke = globalStyle.stroke === 'auto' || isFunction(globalStyle.stroke) ? colorPalette : globalStyle.stroke;
  107. }
  108. data.setVisual('style', globalStyle);
  109. data.setVisual('drawType', colorKey);
  110. // Only visible series has each data be visual encoded
  111. if (!ecModel.isSeriesFiltered(seriesModel) && colorCallback) {
  112. data.setVisual('colorFromPalette', false);
  113. return {
  114. dataEach: function (data, idx) {
  115. var dataParams = seriesModel.getDataParams(idx);
  116. var itemStyle = extend({}, globalStyle);
  117. itemStyle[colorKey] = colorCallback(dataParams);
  118. data.setItemVisual(idx, 'style', itemStyle);
  119. }
  120. };
  121. }
  122. }
  123. };
  124. var sharedModel = new Model();
  125. var dataStyleTask = {
  126. createOnAllSeries: true,
  127. performRawSeries: true,
  128. reset: function (seriesModel, ecModel) {
  129. if (seriesModel.ignoreStyleOnData || ecModel.isSeriesFiltered(seriesModel)) {
  130. return;
  131. }
  132. var data = seriesModel.getData();
  133. var stylePath = seriesModel.visualStyleAccessPath || 'itemStyle';
  134. // Set in itemStyle
  135. var getStyle = getStyleMapper(seriesModel, stylePath);
  136. var colorKey = data.getVisual('drawType');
  137. return {
  138. dataEach: data.hasItemOption ? function (data, idx) {
  139. // Not use getItemModel for performance considuration
  140. var rawItem = data.getRawDataItem(idx);
  141. if (rawItem && rawItem[stylePath]) {
  142. sharedModel.option = rawItem[stylePath];
  143. var style = getStyle(sharedModel);
  144. var existsStyle = data.ensureUniqueItemVisual(idx, 'style');
  145. extend(existsStyle, style);
  146. if (sharedModel.option.decal) {
  147. data.setItemVisual(idx, 'decal', sharedModel.option.decal);
  148. sharedModel.option.decal.dirty = true;
  149. }
  150. if (colorKey in style) {
  151. data.setItemVisual(idx, 'colorFromPalette', false);
  152. }
  153. }
  154. } : null
  155. };
  156. }
  157. };
  158. // Pick color from palette for the data which has not been set with color yet.
  159. // Note: do not support stream rendering. No such cases yet.
  160. var dataColorPaletteTask = {
  161. performRawSeries: true,
  162. overallReset: function (ecModel) {
  163. // Each type of series uses one scope.
  164. // Pie and funnel are using different scopes.
  165. var paletteScopeGroupByType = createHashMap();
  166. ecModel.eachSeries(function (seriesModel) {
  167. var colorBy = seriesModel.getColorBy();
  168. if (seriesModel.isColorBySeries()) {
  169. return;
  170. }
  171. var key = seriesModel.type + '-' + colorBy;
  172. var colorScope = paletteScopeGroupByType.get(key);
  173. if (!colorScope) {
  174. colorScope = {};
  175. paletteScopeGroupByType.set(key, colorScope);
  176. }
  177. inner(seriesModel).scope = colorScope;
  178. });
  179. ecModel.eachSeries(function (seriesModel) {
  180. if (seriesModel.isColorBySeries() || ecModel.isSeriesFiltered(seriesModel)) {
  181. return;
  182. }
  183. var dataAll = seriesModel.getRawData();
  184. var idxMap = {};
  185. var data = seriesModel.getData();
  186. var colorScope = inner(seriesModel).scope;
  187. var stylePath = seriesModel.visualStyleAccessPath || 'itemStyle';
  188. var colorKey = getDefaultColorKey(seriesModel, stylePath);
  189. data.each(function (idx) {
  190. var rawIdx = data.getRawIndex(idx);
  191. idxMap[rawIdx] = idx;
  192. });
  193. // Iterate on data before filtered. To make sure color from palette can be
  194. // Consistent when toggling legend.
  195. dataAll.each(function (rawIdx) {
  196. var idx = idxMap[rawIdx];
  197. var fromPalette = data.getItemVisual(idx, 'colorFromPalette');
  198. // Get color from palette for each data only when the color is inherited from series color, which is
  199. // also picked from color palette. So following situation is not in the case:
  200. // 1. series.itemStyle.color is set
  201. // 2. color is encoded by visualMap
  202. if (fromPalette) {
  203. var itemStyle = data.ensureUniqueItemVisual(idx, 'style');
  204. var name_1 = dataAll.getName(rawIdx) || rawIdx + '';
  205. var dataCount = dataAll.count();
  206. itemStyle[colorKey] = seriesModel.getColorFromPalette(name_1, colorScope, dataCount);
  207. }
  208. });
  209. });
  210. }
  211. };
  212. export { seriesStyleTask, dataStyleTask, dataColorPaletteTask };