style.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. reset: function (seriesModel, ecModel) {
  128. if (seriesModel.ignoreStyleOnData) {
  129. return;
  130. }
  131. var data = seriesModel.getData();
  132. var stylePath = seriesModel.visualStyleAccessPath || 'itemStyle';
  133. // Set in itemStyle
  134. var getStyle = getStyleMapper(seriesModel, stylePath);
  135. var colorKey = data.getVisual('drawType');
  136. return {
  137. dataEach: data.hasItemOption ? function (data, idx) {
  138. // Not use getItemModel for performance considuration
  139. var rawItem = data.getRawDataItem(idx);
  140. if (rawItem && rawItem[stylePath]) {
  141. sharedModel.option = rawItem[stylePath];
  142. var style = getStyle(sharedModel);
  143. var existsStyle = data.ensureUniqueItemVisual(idx, 'style');
  144. extend(existsStyle, style);
  145. if (sharedModel.option.decal) {
  146. data.setItemVisual(idx, 'decal', sharedModel.option.decal);
  147. sharedModel.option.decal.dirty = true;
  148. }
  149. if (colorKey in style) {
  150. data.setItemVisual(idx, 'colorFromPalette', false);
  151. }
  152. }
  153. } : null
  154. };
  155. }
  156. };
  157. // Pick color from palette for the data which has not been set with color yet.
  158. // Note: do not support stream rendering. No such cases yet.
  159. var dataColorPaletteTask = {
  160. performRawSeries: true,
  161. overallReset: function (ecModel) {
  162. // Each type of series uses one scope.
  163. // Pie and funnel are using different scopes.
  164. var paletteScopeGroupByType = createHashMap();
  165. ecModel.eachSeries(function (seriesModel) {
  166. if (!seriesModel.isColorBySeries()) {
  167. var key = seriesModel.type + '-' + seriesModel.getColorBy();
  168. inner(seriesModel).scope = paletteScopeGroupByType.get(key) || paletteScopeGroupByType.set(key, {});
  169. }
  170. });
  171. ecModel.eachSeries(function (seriesModel) {
  172. if (seriesModel.isColorBySeries()) {
  173. return;
  174. }
  175. var dataAll = seriesModel.getRawData();
  176. var idxMap = {};
  177. var data = seriesModel.getData();
  178. var colorScope = inner(seriesModel).scope;
  179. var stylePath = seriesModel.visualStyleAccessPath || 'itemStyle';
  180. var colorKey = getDefaultColorKey(seriesModel, stylePath);
  181. data.each(function (idx) {
  182. var rawIdx = data.getRawIndex(idx);
  183. idxMap[rawIdx] = idx;
  184. });
  185. // Iterate on data before filtered. To make sure color from palette can be
  186. // Consistent when toggling legend.
  187. dataAll.each(function (rawIdx) {
  188. var idx = idxMap[rawIdx];
  189. var fromPalette = data.getItemVisual(idx, 'colorFromPalette');
  190. // Get color from palette for each data only when the color is inherited from series color, which is
  191. // also picked from color palette. So following situation is not in the case:
  192. // 1. series.itemStyle.color is set
  193. // 2. color is encoded by visualMap
  194. if (fromPalette) {
  195. var itemStyle = data.ensureUniqueItemVisual(idx, 'style');
  196. var name_1 = dataAll.getName(rawIdx) || rawIdx + '';
  197. var dataCount = dataAll.count();
  198. itemStyle[colorKey] = seriesModel.getColorFromPalette(name_1, colorScope, dataCount);
  199. }
  200. });
  201. });
  202. }
  203. };
  204. export { seriesStyleTask, dataStyleTask, dataColorPaletteTask };