compatStyle.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 zrUtil = require("zrender/lib/core/util");
  20. var modelUtil = require("../../util/model");
  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. var each = zrUtil.each;
  40. var isObject = zrUtil.isObject;
  41. var POSSIBLE_STYLES = ['areaStyle', 'lineStyle', 'nodeStyle', 'linkStyle', 'chordStyle', 'label', 'labelLine'];
  42. function compatEC2ItemStyle(opt) {
  43. var itemStyleOpt = opt && opt.itemStyle;
  44. if (!itemStyleOpt) {
  45. return;
  46. }
  47. for (var i = 0, len = POSSIBLE_STYLES.length; i < len; i++) {
  48. var styleName = POSSIBLE_STYLES[i];
  49. var normalItemStyleOpt = itemStyleOpt.normal;
  50. var emphasisItemStyleOpt = itemStyleOpt.emphasis;
  51. if (normalItemStyleOpt && normalItemStyleOpt[styleName]) {
  52. opt[styleName] = opt[styleName] || {};
  53. if (!opt[styleName].normal) {
  54. opt[styleName].normal = normalItemStyleOpt[styleName];
  55. } else {
  56. zrUtil.merge(opt[styleName].normal, normalItemStyleOpt[styleName]);
  57. }
  58. normalItemStyleOpt[styleName] = null;
  59. }
  60. if (emphasisItemStyleOpt && emphasisItemStyleOpt[styleName]) {
  61. opt[styleName] = opt[styleName] || {};
  62. if (!opt[styleName].emphasis) {
  63. opt[styleName].emphasis = emphasisItemStyleOpt[styleName];
  64. } else {
  65. zrUtil.merge(opt[styleName].emphasis, emphasisItemStyleOpt[styleName]);
  66. }
  67. emphasisItemStyleOpt[styleName] = null;
  68. }
  69. }
  70. }
  71. function convertNormalEmphasis(opt, optType, useExtend) {
  72. if (opt && opt[optType] && (opt[optType].normal || opt[optType].emphasis)) {
  73. var normalOpt = opt[optType].normal;
  74. var emphasisOpt = opt[optType].emphasis;
  75. if (normalOpt) {
  76. // Timeline controlStyle has other properties besides normal and emphasis
  77. if (useExtend) {
  78. opt[optType].normal = opt[optType].emphasis = null;
  79. zrUtil.defaults(opt[optType], normalOpt);
  80. } else {
  81. opt[optType] = normalOpt;
  82. }
  83. }
  84. if (emphasisOpt) {
  85. opt.emphasis = opt.emphasis || {};
  86. opt.emphasis[optType] = emphasisOpt;
  87. }
  88. }
  89. }
  90. function removeEC3NormalStatus(opt) {
  91. convertNormalEmphasis(opt, 'itemStyle');
  92. convertNormalEmphasis(opt, 'lineStyle');
  93. convertNormalEmphasis(opt, 'areaStyle');
  94. convertNormalEmphasis(opt, 'label');
  95. convertNormalEmphasis(opt, 'labelLine'); // treemap
  96. convertNormalEmphasis(opt, 'upperLabel'); // graph
  97. convertNormalEmphasis(opt, 'edgeLabel');
  98. }
  99. function compatTextStyle(opt, propName) {
  100. // Check whether is not object (string\null\undefined ...)
  101. var labelOptSingle = isObject(opt) && opt[propName];
  102. var textStyle = isObject(labelOptSingle) && labelOptSingle.textStyle;
  103. if (textStyle) {
  104. for (var i = 0, len = modelUtil.TEXT_STYLE_OPTIONS.length; i < len; i++) {
  105. var propName = modelUtil.TEXT_STYLE_OPTIONS[i];
  106. if (textStyle.hasOwnProperty(propName)) {
  107. labelOptSingle[propName] = textStyle[propName];
  108. }
  109. }
  110. }
  111. }
  112. function compatEC3CommonStyles(opt) {
  113. if (opt) {
  114. removeEC3NormalStatus(opt);
  115. compatTextStyle(opt, 'label');
  116. opt.emphasis && compatTextStyle(opt.emphasis, 'label');
  117. }
  118. }
  119. function processSeries(seriesOpt) {
  120. if (!isObject(seriesOpt)) {
  121. return;
  122. }
  123. compatEC2ItemStyle(seriesOpt);
  124. removeEC3NormalStatus(seriesOpt);
  125. compatTextStyle(seriesOpt, 'label'); // treemap
  126. compatTextStyle(seriesOpt, 'upperLabel'); // graph
  127. compatTextStyle(seriesOpt, 'edgeLabel');
  128. if (seriesOpt.emphasis) {
  129. compatTextStyle(seriesOpt.emphasis, 'label'); // treemap
  130. compatTextStyle(seriesOpt.emphasis, 'upperLabel'); // graph
  131. compatTextStyle(seriesOpt.emphasis, 'edgeLabel');
  132. }
  133. var markPoint = seriesOpt.markPoint;
  134. if (markPoint) {
  135. compatEC2ItemStyle(markPoint);
  136. compatEC3CommonStyles(markPoint);
  137. }
  138. var markLine = seriesOpt.markLine;
  139. if (markLine) {
  140. compatEC2ItemStyle(markLine);
  141. compatEC3CommonStyles(markLine);
  142. }
  143. var markArea = seriesOpt.markArea;
  144. if (markArea) {
  145. compatEC3CommonStyles(markArea);
  146. }
  147. var data = seriesOpt.data; // Break with ec3: if `setOption` again, there may be no `type` in option,
  148. // then the backward compat based on option type will not be performed.
  149. if (seriesOpt.type === 'graph') {
  150. data = data || seriesOpt.nodes;
  151. var edgeData = seriesOpt.links || seriesOpt.edges;
  152. if (edgeData && !zrUtil.isTypedArray(edgeData)) {
  153. for (var i = 0; i < edgeData.length; i++) {
  154. compatEC3CommonStyles(edgeData[i]);
  155. }
  156. }
  157. zrUtil.each(seriesOpt.categories, function (opt) {
  158. removeEC3NormalStatus(opt);
  159. });
  160. }
  161. if (data && !zrUtil.isTypedArray(data)) {
  162. for (var i = 0; i < data.length; i++) {
  163. compatEC3CommonStyles(data[i]);
  164. }
  165. } // mark point data
  166. var markPoint = seriesOpt.markPoint;
  167. if (markPoint && markPoint.data) {
  168. var mpData = markPoint.data;
  169. for (var i = 0; i < mpData.length; i++) {
  170. compatEC3CommonStyles(mpData[i]);
  171. }
  172. } // mark line data
  173. var markLine = seriesOpt.markLine;
  174. if (markLine && markLine.data) {
  175. var mlData = markLine.data;
  176. for (var i = 0; i < mlData.length; i++) {
  177. if (zrUtil.isArray(mlData[i])) {
  178. compatEC3CommonStyles(mlData[i][0]);
  179. compatEC3CommonStyles(mlData[i][1]);
  180. } else {
  181. compatEC3CommonStyles(mlData[i]);
  182. }
  183. }
  184. } // Series
  185. if (seriesOpt.type === 'gauge') {
  186. compatTextStyle(seriesOpt, 'axisLabel');
  187. compatTextStyle(seriesOpt, 'title');
  188. compatTextStyle(seriesOpt, 'detail');
  189. } else if (seriesOpt.type === 'treemap') {
  190. convertNormalEmphasis(seriesOpt.breadcrumb, 'itemStyle');
  191. zrUtil.each(seriesOpt.levels, function (opt) {
  192. removeEC3NormalStatus(opt);
  193. });
  194. } else if (seriesOpt.type === 'tree') {
  195. removeEC3NormalStatus(seriesOpt.leaves);
  196. } // sunburst starts from ec4, so it does not need to compat levels.
  197. }
  198. function toArr(o) {
  199. return zrUtil.isArray(o) ? o : o ? [o] : [];
  200. }
  201. function toObj(o) {
  202. return (zrUtil.isArray(o) ? o[0] : o) || {};
  203. }
  204. function _default(option, isTheme) {
  205. each(toArr(option.series), function (seriesOpt) {
  206. isObject(seriesOpt) && processSeries(seriesOpt);
  207. });
  208. var axes = ['xAxis', 'yAxis', 'radiusAxis', 'angleAxis', 'singleAxis', 'parallelAxis', 'radar'];
  209. isTheme && axes.push('valueAxis', 'categoryAxis', 'logAxis', 'timeAxis');
  210. each(axes, function (axisName) {
  211. each(toArr(option[axisName]), function (axisOpt) {
  212. if (axisOpt) {
  213. compatTextStyle(axisOpt, 'axisLabel');
  214. compatTextStyle(axisOpt.axisPointer, 'label');
  215. }
  216. });
  217. });
  218. each(toArr(option.parallel), function (parallelOpt) {
  219. var parallelAxisDefault = parallelOpt && parallelOpt.parallelAxisDefault;
  220. compatTextStyle(parallelAxisDefault, 'axisLabel');
  221. compatTextStyle(parallelAxisDefault && parallelAxisDefault.axisPointer, 'label');
  222. });
  223. each(toArr(option.calendar), function (calendarOpt) {
  224. convertNormalEmphasis(calendarOpt, 'itemStyle');
  225. compatTextStyle(calendarOpt, 'dayLabel');
  226. compatTextStyle(calendarOpt, 'monthLabel');
  227. compatTextStyle(calendarOpt, 'yearLabel');
  228. }); // radar.name.textStyle
  229. each(toArr(option.radar), function (radarOpt) {
  230. compatTextStyle(radarOpt, 'name');
  231. });
  232. each(toArr(option.geo), function (geoOpt) {
  233. if (isObject(geoOpt)) {
  234. compatEC3CommonStyles(geoOpt);
  235. each(toArr(geoOpt.regions), function (regionObj) {
  236. compatEC3CommonStyles(regionObj);
  237. });
  238. }
  239. });
  240. each(toArr(option.timeline), function (timelineOpt) {
  241. compatEC3CommonStyles(timelineOpt);
  242. convertNormalEmphasis(timelineOpt, 'label');
  243. convertNormalEmphasis(timelineOpt, 'itemStyle');
  244. convertNormalEmphasis(timelineOpt, 'controlStyle', true);
  245. var data = timelineOpt.data;
  246. zrUtil.isArray(data) && zrUtil.each(data, function (item) {
  247. if (zrUtil.isObject(item)) {
  248. convertNormalEmphasis(item, 'label');
  249. convertNormalEmphasis(item, 'itemStyle');
  250. }
  251. });
  252. });
  253. each(toArr(option.toolbox), function (toolboxOpt) {
  254. convertNormalEmphasis(toolboxOpt, 'iconStyle');
  255. each(toolboxOpt.feature, function (featureOpt) {
  256. convertNormalEmphasis(featureOpt, 'iconStyle');
  257. });
  258. });
  259. compatTextStyle(toObj(option.axisPointer), 'label');
  260. compatTextStyle(toObj(option.tooltip).axisPointer, 'label');
  261. }
  262. module.exports = _default;